Solved

Add string X times

Posted on 2004-09-09
6
205 Views
Last Modified: 2010-05-02
what im trying to do is have a string added to a string a certian amount of times

this is just an exmple i need to do simlar things, with text strings as well.

for example, The string TestString has to be 5 numbers,
(say the number is 5) add four zeros to the start, (teststring = "00005")
(say the number is 35) add three zeros to the start , (teststring = "00035")
i would like to be able to do somthing like
'            StartVars      Add string "0", 5 - Len((StartCount + ((i * 30) + x))) times & (StartCount + ((i * 30) + x))
Ref(x) = StartCode & Add0times(5 - Len((StartCount + ((i * 30) + x)))) & (StartCount + ((i * 30) + x))

this is part of my code i already have, but it is really chunky for what it does

    For x = 0 To 29
   
        If Len((StartCount + ((i * 30) + x))) = 1 Then
        Ref(x) = StartCode & "0000" & (StartCount + ((i * 30) + x))
        Num(x) = "0000" & (StartCount + ((i * 30) + x))
        ElseIf Len((StartCount + ((i * 30) + x))) = 2 Then
        Ref(x) = StartCode & "000" & (StartCount + ((i * 30) + x))
        Num(x) = "000" & (StartCount + ((i * 30) + x))
        ElseIf Len((StartCount + ((i * 30) + x))) = 3 Then
        Ref(x) = StartCode & "00" & (StartCount + ((i * 30) + x))
        Num(x) = "00" & (StartCount + ((i * 30) + x))
        ElseIf Len((StartCount + ((i * 30) + x))) = 4 Then
        Ref(x) = StartCode & "0" & (StartCount + ((i * 30) + x))
        Num(x) = "0" & (StartCount + ((i * 30) + x))
        ElseIf Len((StartCount + ((i * 30) + x))) = 5 Then
        Ref(x) = StartCode & (StartCount + ((i * 30) + x))
        Num(x) = (StartCount + ((i * 30) + x))
        End If
       
    Next
0
Comment
Question by:Y2Kingswood
[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
  • 2
6 Comments
 
LVL 19

Assisted Solution

by:Shauli
Shauli earned 50 total points
ID: 12022407
All you have to do is simply to format your string. Try this:

MsgBox Format("5", "00000")

...and

MsgBox Format("35", "00000")

As you can see. That si all. It will format the string with the leading zeros as you wish.

Am I missing something, or is it what you are looking for?

S
0
 

Author Comment

by:Y2Kingswood
ID: 12022418
it needs kind of be like String$(length, string) but not just take the first character....

If i have   5 My end value needs to be "00005"
if i have 35 my end value needs to be "00035"
0
 
LVL 19

Expert Comment

by:Shauli
ID: 12022420
Try my solution. It does exactly that.

S
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.

 

Author Comment

by:Y2Kingswood
ID: 12022426
which works in this case, i will try a few others,
but just to explain more simply

just say i have a var with a value of 5
and string with the value of "Hello"
i want to be able to end up with "HelloHelloHelloHelloHello"
0
 
LVL 19

Accepted Solution

by:
Shauli earned 50 total points
ID: 12022452
Oh, OK, I see what you mean:

Private Sub Command1_Click()
MsgBox RepeatVar("Hello", 5)
End Sub


Public Function RepeatVar(ByVal fnString As String, fnTimes As Integer) As String
Dim fnLoop As Integer, tmpString As String
For fnLoop = 1 To fnTimes
    tmpString = tmpString & fnString
Next fnLoop
RepeatVar = tmpString
End Function

S
0
 
LVL 18

Expert Comment

by:JR2003
ID: 12022465
Private Function RepeatStringXTimes(sInString, iNumberOfTimes) As String

Dim i as long
For i = 1 to iNumberOfTimes
    RepeatStringXTimes = RepeatStringXTimes & sInString
Next i

End Function


To call the function just do this:

sMyString = RepeatStringXTimes ("Hello", 5)
Debug.Print sMyString
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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 Month8 days, 3 hours left to enroll

617 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