[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
7
Medium Priority
?
274 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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

649 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