By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,726 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Visual Basic Tip of the Week #3

Expert 5K+
P: 8,434
Originally posted by sashi

Generate Random Password

Expand|Select|Wrap|Line Numbers
  1. ' Form code - frmPasswordGenerate
  2.  
  3. Private Declare Function GetTickCount Lib "kernel32" () As Long
  4.  
  5. Public Function PassGen(nLen As Long) As String
  6.   Dim range As Collection
  7.   Dim ivalue As Long, icount As Long, iLen As Long
  8.   Dim pass As String
  9.  
  10.   Set range = New Collection
  11.   range.Add ("0")
  12.   range.Add ("1")
  13.   range.Add ("2")
  14.   range.Add ("3")
  15.   range.Add ("4")
  16.   range.Add ("5")
  17.   range.Add ("6")
  18.   range.Add ("7")
  19.   range.Add ("8")
  20.   range.Add ("9")
  21.  
  22.   iLen = range.Count
  23.  
  24.   Do Until icount = nLen
  25.     Randomize
  26.     ivalue = CByte(Mid(CStr(Rnd(GetTickCount)), 3, 2))
  27.     If ivalue > 0 And ivalue <= iLen Then
  28.       icount = icount + 1
  29.       pass = pass & range(ivalue)
  30.     End If
  31.   Loop
  32.  
  33.   PassGen = pass
  34. End Function
  35.  
  36. Private Sub cmdGeneratePassword_Click()
  37.   MsgBox PassGen(8)
  38. End Sub
Apr 25 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.