Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2005
    Posts
    220

    Unanswered: VBA Password generator

    I found this function on the internet and I need it to make sure the 1st character is a letter.

    Any ideas?

    Code:
    Function CreatePassword(nLen As Long) As String
        
        Dim i As Long
        Dim nRnd As Double
        Dim sPW As String
        Dim bAdd As Boolean
        
        Dim strTemp As String
    
        Randomize
    
        While Len(sPW) < nLen
            
            nRnd = Int(Rnd * 75) + 48
            bAdd = False
            Select Case nRnd
                Case 48 To 57    ' Numeric characters
                    bAdd = True
                Case 65 To 90    ' Upper case characters
                    bAdd = True
                'Case 97 To 122  ' Lower case characters
                '    bAdd = True
                Case Else        ' Useless characters
                    bAdd = False
            End Select
            
            If bAdd Then
                sPW = sPW & Chr(nRnd)
            End If
            
        Wend
    
        CreatePassword = sPW
    
    End Function

  2. #2
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    What do you want done if the 1st isn't a character?
    Back to Access ... ADO is not the way to go for speed ...

  3. #3
    Join Date
    Sep 2005
    Posts
    220
    All I require is that the 1st character be alphabetic.

  4. #4
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    Quote Originally Posted by ssmith001
    All I require is that the 1st character be alphabetic.
    Ok ...

    Now: You've got to be kidding me ... (Sorry but this is too damned easy to even post a question on)

    dim firstchar as string

    FirstChar =left(sPW,1)
    If (ucase(firstchar)<'A')) or (ucase(firstchar)>'Z')) then
    'Figure out what you want to do ...
    ' - Add a random char to the start of string
    ' - Replace the 1st char with a random char
    ' - do whatever you feel like doing ...
    endif
    Back to Access ... ADO is not the way to go for speed ...

  5. #5
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    maybe you should set the loop that constructs your password to run for nLen - 1, then generate a random alpha character and append it to your output string...
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  6. #6
    Join Date
    Sep 2005
    Posts
    220
    That's a good idea, but I can't seem to figure out how to generate the letter between A and Z.

  7. #7
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    Well, you have two case statements in that function, one generates a-z, the other generates A-Z, I would use one of those.
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  8. #8
    Join Date
    Nov 2003
    Posts
    1,487
    Code:
    Function CreatePassword(nLen As Long) As String
       Dim nRnd As Double
       Dim sPW As String
       Dim bAdd As Boolean
     
       Randomize
       While Len(sPW) < nLen
          nRnd = Int(Rnd * 75) + 48
          bAdd = False
          Select Case nRnd
             Case 48 To 57 ' Numeric characters
                bAdd = True
             Case 65 To 90 ' Upper case characters
                bAdd = True
             'Case 97 To 122 ' Lower case characters
                ' bAdd = True
             Case Else ' Useless characters
                bAdd = False
          End Select
     
          If bAdd Then
             sPW = sPW & Chr(nRnd)
             If (Len(sPW) = nLen - 1) And (Asc(Left$(sPW, 1)) < 65) Then
                sPW = Right$(sPW, Len(sPW) - 1)
             End If
          End If
       Wend
     
       CreatePassword = sPW
    End Function
    .
    Last edited by CyberLynx; 08-17-06 at 06:56.
    Environment:
    Self Taught In ALL Environments.....And It Shows!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •