UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Generate a Random AlphaNumeric String    
 
   
argeedblu
post Nov 26 2003, 04:48 PM
Post #1

UA Forum + Wiki Administrator
Posts: 11,959
From: Sudbury, Ontario, Canada



This code will generate a random alphanumeric string with the specified number of letters and digits.

Put the code in a standard module and call the function with the first argument as the number of alphas and the second argument as the number of digits required.


CODE
Public Function RandomPwd(ialph As Integer, inum As Integer) As String

'********************************************************************************
*******

' Procedure : RandomPwd

' DateTime  : 2003-11-26 16:02

' Author    : Glenn Lloyd - Argee Services

' Purpose   : generate a random alphanumeric password with ialph letters and inum digits

' Returns   : String

'********************************************************************************
*******

'





   On Error GoTo RandomPwd_Error

    Const clow = "A"            'generate only uppercase 'A' to 'Z'

    Const chigh = "Z"

    Const llow = 1              'we want lowest numeric to be 1

    

    Dim lhigh As Long           'maximum value of numeric digits

    

    Dim spword As String        'for our results

    

    Dim cchr As String          'individual characters generated

    Dim lnum As Long            'individual digits generated

    

    Dim ictr As Integer

    

    Dim sfmt As String

    

    spword = ""                 'start with an empty string

    

    Randomize                   'initialize the randome number generator

    

    For ictr = 1 To ialph       'generate specified number of letters

        cchr = Chr(Int((Asc(chigh) - Asc(clow) + 1) * Rnd() + Asc(clow)))



        spword = spword & cchr  'add letters to result string

    Next ictr

    

    lhigh = 10 ^ inum - 1       'maximum numeric value of digits

                                'concatenate result string with specified number of digits

                              

    sfmt = Right(str(lhigh), Len(str(lhigh) - 1))   'remove the leading character

    

    For ictr = 1 To Len(sfmt)

        Mid(sfmt, ictr, 1) = "0"                    'convert format to zeros

    Next ictr

                                                    'generate and format the numeric part

                                                    

    spword = spword + Format((Int(lhigh - llow + 1) * Rnd() + llow), sfmt)

    

    RandomPwd = spword



RandomPwd_Exit:

   On Error GoTo 0

   Exit Function



RandomPwd_Error:



    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure RandomPwd of Module Module4"





Resume RandomPwd_Exit





End Function


Glenn
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 24th May 2013 - 01:04 AM