I need to extract the first word from data in a text box (up to the first space) to use in an sql statement. What is the best way to do this?
Printable View
I need to extract the first word from data in a text box (up to the first space) to use in an sql statement. What is the best way to do this?
Add a command button, a text box and try this code.
Private Sub Command1_Click()
Dim mytext As String
Dim counter As Integer
mytext = Text1.Text
For counter = 1 To Len(Text1.Text)
If Mid$(mytext, counter, 1) = " " Then
GoTo NextPart
End If
Next counter
NextPart:
MsgBox "'" + Mid$(mytext, 1, counter - 1) + "'"
End Sub
Good Luck,
dmuir
Try this...
Dim sFirstWord As String
Dim nPosition As Integer
nPosition = InStr(1, Text1.Text, " ")
If nPosition <> 0 Then
sFirstWord = Left(Text1.Text, nPosition)
Else
sFirstWord = Text1.Text
End If
MsgBox sFirstWord
Two text boxes. A command button.
Add the following code.
Code:Private Sub Command1_Click()
Dim sword As String
Dim sspace As String
sword = Trim$(Text2.Text)
sspace = InStr(sword, " ")
Text1.Text = Left$(sword, sspace - 1)
End Sub
Ruchi