[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# Trimming the last character in a string

Posted on 2004-10-15
Medium Priority
274 Views
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 ....?
0
Question by:ucla11
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 3

LVL 12

Assisted Solution

Bob Lamberson earned 500 total points
ID: 12326109
Hi ucla11,
revising your code like this will get rid of the extra X.
Option Explicit
Dim Base As Integer
Dim Exp As Integer
Dim x As Integer

Private Sub Command1_Click()
Base = Val(txtBase.Text)
Exp = Val(txtExp.Text)

For x = 1 To Exp
'third textbox on form to display the expanded version
If x = Exp Then
Else
End If
Next x

End Sub

Bob
0

LVL 86

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 12326194
Hi ucla11,

I thought you might like this completely different approach that doesn't loop:

Private Sub Command1_Click()
On Error GoTo invalidValue

Dim base As Integer
Dim exp As Integer
Dim temp As String

base = CInt(txtBase.Text)
exp = CInt(txtExp.Text)

If exp > 1 Then
temp = String((exp - 1), "-")
temp = Replace(temp, "-", "X" & base)
End If

Exit Sub
invalidValue:
MsgBox "Please try again", vbInformation, "Invalid Values"
End Sub
0

Author Comment

ID: 12326406
Both worked great How about doing the opposite lets say thet type in 3X3X3X3 in the  txtAnswer.Text  textbox and in  txtBase.Text shows a 3 and in
txtExp.Text shows a  4

0

LVL 86

Expert Comment

ID: 12327608
Private Sub Command2_Click()
Dim temp As Variant
Dim base As String
Dim exp As String
Dim i As Integer
Dim tempBase As String

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
0

Author Comment

ID: 12332979
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)
0

LVL 86

Expert Comment

ID: 12338801
Make sure you are using an uppercase "X" and that there are no spaces.  You could repace this line:

with this to make it a little safer:

The code works perfectly on my system though.

~IM
0

Author Comment

ID: 12342329
Thanks, I was typing in lowercase x.............
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
###### Suggested Courses
Course of the Month14 days, 11 hours left to enroll