Solved

need 0 to always show in text box

Posted on 2004-08-26
5
189 Views
Last Modified: 2010-05-02
num = Int(Rnd * 10) & Int(Rnd * 10) & Int(Rnd * 10) & Int(Rnd * 10) & Int(Rnd * 10)
Text1 = num

with the above code, my 0's do not always appear

i am not sure why

i need five numbers everytime from 0 to 9

any ideas?

thanks for any help
0
Comment
Question by:rinkydink
5 Comments
 
LVL 1

Author Comment

by:rinkydink
ID: 11910131
just thought i'd add that num is declared as Long

and it seems like i am always missing 0's at the end of my number

i need them to show so i have a 5 digit number everytime

thanks
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11910150
Text1 = chr$(Rnd*10+48)+chr$(Rnd*10+48)+chr$(Rnd*10+48)+chr$(Rnd*10+48)+chr$(Rnd*10+48)
0
 
LVL 18

Accepted Solution

by:
JR2003 earned 100 total points
ID: 11910166
num is probably a variant assuming itself as a number type variable and missing off leading zeros.
You need to explicitly define your variable 'num' to be a String.
i.e.
Dim num as String
0
 
LVL 18

Expert Comment

by:JR2003
ID: 11910170
Alternativley just change the line:
Text1 = num
to
Text1 = Format(num,"00000")
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11910185
Option Explicit

Private Sub Form_Load()
    Randomize Timer
End Sub

Private Sub Command1_Click()
    ' Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
    Dim lngNum As Long
    Dim strNum
   
    lngNum = CLng((99999 - 0 + 1) * Rnd + 0)
    strNum = Format(lngNum, "00000")
    Label1.Caption = strNum
End Sub

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

821 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