Hello. This my second post this afternoon. Sorry to be a bother, but I am really trying to learn.
I have two text boxes. I am trying to learn some VBA, and am working with strings in the book I am in. This code, asking for user input to decide how many characters to retrieve from the right works just fine:
Private Sub cmdRight2_Click()
Dim sAmount As String
Dim iAmount As Integer
Dim iStrLen As Integer
iStrLen = Len(txtInput.Value)
sAmount = InputBox("How many characters would you like to " & _
" retrieve?", "How Many")
If sAmount <> "" Then
iAmount = sAmount
If iAmount > iStrLen Then
MsgBox "There are not that many characters"
Else
txtOutput.Value = Right(txtInput.Value, iAmount)
End If
End If
End Sub
This code, to get user input to decide how many characters to use with the Mid command does not work:
Private Sub cmdMid2_Click()
Dim sAmount1 As Integer
Dim sAmount2 As Integer
Dim iAmount1 As Integer
Dim iAmount2 As Integer
Dim iStrLen1 As Integer
Dim iStrLen2 As Integer
iStrLen1 = Len(Me.txtInput.Value)
sAmount1 = InputBox("Which character do you want to" & _
" start with?", "Which Character?")
If sAmount1 <> "" Then
iAmount1 = sAmount1
If iAmount1 > iStrLen1 Then
MsgBox "There are not that many characters"
Else
iStrLen2 = iStrLen1 - iAmount1
sAmount2 = InputBox("How many characters would you like to" & _
" retrieve?", "How Many?")
If sAmount2 <> "" Then
iAmount2 = sAmount2
If iAmount2 > iStrLen2 Then
MsgBox "There are not that many characters left"
Else
Me.txtOutput.Value = Mid(Me.txtInput.Value, iAmount1, iAmount2)
End If
End If
End If
End If
End Sub
Well, it actually works fine if I comment out
If sAmount1 <> "" Then
and
If sAmount2 <> "" Then
and two of the End If statements. Unless, of course, I hit cancel. If I do that, or if I do not comment them out, I get Type Mismatch errors.
Can someone tell me what the heck I am doing wrong?
Thanks!