?
Solved

Trimming the last character in a string

Posted on 2004-10-15
7
Medium Priority
?
272 Views
Last Modified: 2012-05-05
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
Comment
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
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 12

Assisted Solution

by:Bob Lamberson
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
   txtAnswer.Text = txtAnswer.Text & Base
Else
   txtAnswer.Text = txtAnswer.Text & Base & "X"
End If
Next x

End Sub



Bob
0
 
LVL 86

Accepted Solution

by:
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 answer As String
    Dim temp As String

    base = CInt(txtBase.Text)
    exp = CInt(txtExp.Text)
   
    answer = CStr(base)
    If exp > 1 Then
        temp = String((exp - 1), "-")
        temp = Replace(temp, "-", "X" & base)
        answer = answer & temp
    End If
    txtAnswer.Text = answer
   
    Exit Sub
invalidValue:
    MsgBox "Please try again", vbInformation, "Invalid Values"
End Sub
0
 

Author Comment

by:ucla11
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:Mike Tomlinson
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
   
    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
0
 

Author Comment

by:ucla11
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

by:Mike Tomlinson
ID: 12338801
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
0
 

Author Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 3 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question