Genius: Left$ Function, not selecting left: PLEASE HELP :-)

Genius: Left$ Function, not selecting left: PLEASE HELP :-)

Post by Mle » Thu, 11 Jan 2007 11:31:46


Absolutely flawless, Steve, many thanks...

You are a life saver:

Private Sub Command1_Click()
Dim nPosition As Long
Dim nPositionO As Long


nPosition = InStr(1, Text6(0).Text, " " & Text1.Text & " ",
vbTextCompare)
nPositionO = InStr(1, Text6(0).Text, " " & Text2.Text & " ",
vbTextCompare)


If nPosition = 0 Then
Text1.Text = "?"
Else
Text6(0).SetFocus
Text6(0).SelStart = nPosition
Text6(0).SelLength = Len(Text1.Text)
End If
If nPositionO = 0 Then
Text2.Text = "!"

Text6(0).SetFocus
Text6(0).SelStart = nPositionO
Text6(0).SelLength = Len(Text2.Text)
End If



End Sub

Question:

Why is Left$() even important if it cannot only record chars as needed,
what instance would we use Left$, Right$, or Mid$, or should be
combined somehow with Len to make something concrete happen?

Have a great week, well talk soon :-)

Mles
 
 
 

Genius: Left$ Function, not selecting left: PLEASE HELP :-)

Post by Steve Gerr » Thu, 11 Jan 2007 13:20:46


Question:





They are used to return parts of strings, which is useful in various situations
for manipulating text.

Examples to show the what they do:
?Left$("This is a test", 6)
This i
?Right$("This is a test", 6)
a test
?Mid$("This is a test", 6, 6)
is a t

For instance, you might use them to separate someone's first and last name:

Private Sub Command1_Click()
Dim n As Long
Dim sName As String
Dim sFirst As String
Dim sLast As String

sName = InputBox("Enter name")

n = InStr(1, sName, " ")

If n > 0 Then
sFirst = Left$(sName, n - 1)
sLast = Mid$(sName, n + 1)
MsgBox "Recorded as " & sLast & ", " & sFirst
Else
MsgBox "Recorded as " & sName
End If

End Sub