Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating Random 6 Character String? 1

Status
Not open for further replies.

knovak

Programmer
Dec 18, 2001
17
US
I need to generate a random 6 character string? Any suggestions on how to do this?

Thanks,

Kyle
 
Do you want all characters, only printables, only alphas? JHall
 
Hi Kyle,

This will give you a string of all ascii values from 33 to 126 inclusive:

Randomize
For intR = 0 To 5
intA = Int(Rnd * 93) + 33
str = str & Chr(intA)
Next
This will give you alphanumeric only(upper and lower case)

Randomize
intR = 0
While (intR <= 5)
intA = Int(Rnd * 93) + 33
Select Case intA
Case 48 To 57
str = str & Chr(intA)
intR = intR + 1
Case 65 To 90
str = str & Chr(intA)
intR = intR + 1
Case 97 To 122
str = str & Chr(intA)
intR = intR + 1
End Select
Wend

Jon
 
If you want a string which is not likely to be repeated in the future then try this one

MySixString = Hex$(CLng(Mid(Replace((CStr(CDbl(Now))), &quot;.&quot;, &quot;&quot;, 1, , vbTextCompare), 3, 9)))

It converts the current time into a hexadecimal value - I use it sometimes for primary keys and the like, but it is a bit messy

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top