Solved

Trimming the last character in a string

Posted on 2004-10-15
7
269 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:BobLamberson
BobLamberson earned 125 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 125 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…
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…

734 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