Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 131,634 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,976 people online right now. Registration is fast and FREE... Join Now!




how to find first letter in last name

 
Reply to this topicStart new topic

how to find first letter in last name

powellm
post 15 May, 2008 - 12:29 PM
Post #1


New D.I.C Head

*
Joined: 30 Mar, 2008
Posts: 20

Private Sub cmdGroup_Click()
Dim strName As String
Dim strLast As String
Dim intLastPosition As Integer

intLastPosition = InStr(1, strName, "") + 1
strLast = (Mid(strName, intLastPosition, 1))
strName = txtName.Text

If strLast = "[A-I]" Then
lblGroup.Caption = "Group 1"
ElseIf strLast = "[J-S]" Then
lblGroup.Caption = "Group 2"
ElseIf strLast = "[T-Z]" Then
lblGroup.Caption = "Group 3"
End If

when i run the program nothing shows up in the caption can someone help me figure out how to when the name is entered in the text box the first letter of the last name if it begins with letters ranging from A-I it is in group 1 how do i get that to show up in the caption
User is offlineProfile CardPM

Go to the top of the page


sam_benne
post 15 May, 2008 - 12:59 PM
Post #2


D.I.C Regular

Group Icon
Joined: 16 Jan, 2008
Posts: 285



Thanked 1 times

Dream Kudos: 400
My Contributions


Had ago at this manged to get it the last letter by getting rid of the rest of the text and put it into another variable but can't get it to change the caption in the label as no matter what i put in it says group 1. But this is what i have
CODE

Private Sub cmdGroup_Click()
Dim strName As String

strName = Right(txtName.Text, 1)
strName2 = Val(strName)

If strName2 = [A - I] Then
lblGroup.Caption = "Group 1"
ElseIf strName2 = [J - S] Then
lblGroup.Caption = "Group 2"
ElseIf strName2 = [T - Z] Then
lblGroup.Caption = "Group 3"
End If
End Sub


the right bit deletes everything except the right most part.

got it working here you go
CODE

Private Sub cmdGroup_Click()
Dim strName As String
Dim strName2 As String

strName = Right$(txtName.Text, 1)
strName2 = Val(strName)

If Val(strName2) = a - i Then
lblGroup.Caption = "Group 2"
ElseIf Val(strName2) = j - s Then
lblGroup.Caption = "Group 2"
ElseIf Val(strName2) = t - z Then
lblGroup.Caption = "Group 3"
End If
End Sub

User is offlineProfile CardPM

Go to the top of the page

powellm
post 15 May, 2008 - 04:08 PM
Post #3


New D.I.C Head

*
Joined: 30 Mar, 2008
Posts: 20

how do i find the position of the first letter in the last name
User is offlineProfile CardPM

Go to the top of the page

Zhalix
post 15 May, 2008 - 06:18 PM
Post #4


D.I.C Head

**
Joined: 7 May, 2008
Posts: 218



Thanked 9 times
My Contributions


Try this..

CODE

Private Sub cmdGroup_Click()

Dim strName As String
Dim intFirstLetterPos As Integer
Dim strFirstLetter As String

strName = txtName.Text
intFirstLetterPos = InStr(strName, " ") + 1

strFirstLetter = UCase(Mid(strName, intFirstLetterPos, 1))

If Asc(strFirstLetter) >= 65 And Asc(strFirstLetter) <= 73 Then
    lblGroup.Caption = "Group 1"
ElseIf Asc(strFirstLetter) >= 74 And Asc(strFirstLetter) <= 83 Then
    lblGroup.Caption = "Group 2"
ElseIf Asc(strFirstLetter) >= 84 And Asc(strFirstLetter) <= 90 Then
    lblGroup.Caption = "Group 3"
End If

End Sub


This code is assuming that whoever has typed their name in has separated their first name and last name by a space. It first finds the location of the space, and then adds one to that, which will be the location of the first letter of the last name.

It then uses the mid function combined with the UCase function to extract that first letter into the string "strFirstLetter" as well as ensure that it is in capital form (because capitals and lowercase have different number equivalents).

It then uses the Asc function to convert that single letter into its number equivalent and checks to see which range it's in. 65 is "A", 73 is "I", and all the numbers in between have letter equivalents that would be between "A" and "I" in the alphabet.

This post has been edited by Zhalix: 15 May, 2008 - 06:26 PM
User is offlineProfile CardPM

Go to the top of the page

powellm
post 15 May, 2008 - 06:27 PM
Post #5


New D.I.C Head

*
Joined: 30 Mar, 2008
Posts: 20

thanks
User is offlineProfile CardPM

Go to the top of the page

Zhalix
post 15 May, 2008 - 06:29 PM
Post #6


D.I.C Head

**
Joined: 7 May, 2008
Posts: 218



Thanked 9 times
My Contributions


No problem.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 05:47AM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month