Solved

Trimming the last character in a string

Posted on 2004-10-15
7
263 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
  • 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 85

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 85

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 85

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

758 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now