ucla11
asked on
Trimming the last character in a string
I have two text boxes on a form ...one for the base number and the other for the exponent. If the user enters 3 intxtBase,text and 3 in txtExp.text that is 3^3 I am using a for next loop to convert that into its exapaned form...3X3X3
Dim base as Integer
DimBase As Interger
Dim x As Integer
Base= Val(txtBase.Text)
Exp = Val(txtExp.Text)
For x = 1 to Exp
'third textbox on form to display the expanded version
txtAnwser.Text = txtAnwser.Text & Base & " X"
Next x
I know I will have on extra "X" how to I trim this extra "X" off and will this loop even work and give the result in the textBox:
3X3X3 ....?
Dim base as Integer
DimBase As Interger
Dim x As Integer
Base= Val(txtBase.Text)
Exp = Val(txtExp.Text)
For x = 1 to Exp
'third textbox on form to display the expanded version
txtAnwser.Text = txtAnwser.Text & Base & " X"
Next x
I know I will have on extra "X" how to I trim this extra "X" off and will this loop even work and give the result in the textBox:
3X3X3 ....?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Private Sub Command2_Click()
Dim temp As Variant
Dim base As String
Dim exp As String
Dim i As Integer
Dim tempBase As String
temp = Split(txtAnswer.Text, "X")
For i = LBound(temp) To UBound(temp)
If temp(i) = "" Then
MsgBox "Please try again", vbInformation, "Invalid Format"
Exit Sub
ElseIf tempBase = "" Then
tempBase = temp(i)
Else
If temp(i) <> tempBase Then
MsgBox "Please try again", vbInformation, "Invalid Format"
Exit Sub
End If
End If
Next i
txtBase = tempBase
txtExp = UBound(temp) + 1
End Sub
Dim temp As Variant
Dim base As String
Dim exp As String
Dim i As Integer
Dim tempBase As String
temp = Split(txtAnswer.Text, "X")
For i = LBound(temp) To UBound(temp)
If temp(i) = "" Then
MsgBox "Please try again", vbInformation, "Invalid Format"
Exit Sub
ElseIf tempBase = "" Then
tempBase = temp(i)
Else
If temp(i) <> tempBase Then
MsgBox "Please try again", vbInformation, "Invalid Format"
Exit Sub
End If
End If
Next i
txtBase = tempBase
txtExp = UBound(temp) + 1
End Sub
ASKER
If I enter 3X3X3 in the txtAnswer.Text
The code does not work quite right inthe txtBase it diplys 3X3X3 and in txtExp it displays 1(Always 1)
The code does not work quite right inthe txtBase it diplys 3X3X3 and in txtExp it displays 1(Always 1)
Make sure you are using an uppercase "X" and that there are no spaces. You could repace this line:
temp = Split(txtAnswer.Text, "X")
with this to make it a little safer:
temp = Split(UCase(Trim(txtAnswer .Text)), "X")
The code works perfectly on my system though.
~IM
temp = Split(txtAnswer.Text, "X")
with this to make it a little safer:
temp = Split(UCase(Trim(txtAnswer
The code works perfectly on my system though.
~IM
ASKER
Thanks, I was typing in lowercase x.............
ASKER
txtExp.Text shows a 4