I have found some very useful vba code to generate a random password that is going into one field in a table. The issue is that Access is generating a random password only once per query and hence using the same password for every record created. Is there
a way to force Access to run the function code once for each record instead of once per query?
Here is the SQL from the query:
SELECT DISTINCT BASE_USER_TABLE.ID, BASE_USER_TABLE.LAST_NAME, BBASE_USER_TABLE.FIRST_NAME, [BASE_USER_TABLE]![FIRST_NAME] & " " & [BASE_USER_TABLE]![LAST_NAME] AS FULL_NAME, UCase([BASE_USER_TABLE]![FIRST_NAME] & "." & [BASE_USER_TABLE]![LAST_NAME]) AS
USERNAME, Random([«Rlength»]) AS [PASSWORD], INTO USER_ACCT_TABLE
FROM BASE_USER_TABLE;
Here is the code I found for the password generation (with credit to the person that generated it)
Public Function Random(Rlength As Integer) As String
' This function creates a string of random characters, both numbers
' and alpha, with a length of RLength. It uses Timer to seed the Rnd
' function.
' Random() Version 1.0.0
' Copyright © 2009 Extra Mile Data, www.extramiledata.com.
' For questions or issues, please contact *** Email address is removed for privacy ***.
' Use (at your own risk) and modify freely as long as proper credit is given.
On Error GoTo Err_Random
Dim strTemp As String
Dim intLoop As Integer
Dim strCharBase As String
Dim intPos As Integer
Dim intLen As Integer
' Build the base.
strCharBase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" _
& "abcdefghijklmnopqrstuvwxyz" _
& "0123456789"
' Get it's length.
intLen = Len(strCharBase)
' Initialize the results.
strTemp = String(Rlength, "A")
' Reset the random seed.
Rnd -1
' Initialize the seed using Timer.
Randomize (Timer)
' Loop until you hit the end of strTemp. Replace each character
' with a character selected at random from strCharBase.
For intLoop = 1 To Len(strTemp)
' Use the Rnd function to pick a position number in strCharBase.
' If the result exceeds the length of strCharBase, subtract one.
intPos = CInt(Rnd() * intLen + 1)
If intPos > intLen Then intPos = intPos - 1
' Now assign the character at that position in the base to the
' next strTemp position.
Mid$(strTemp, intLoop, 1) = Mid$(strCharBase, intPos, 1)
Next
' Return the results.
Random = strTemp
Exit_Random:
On Error Resume Next
Exit Function
Err_Random:
MsgBox Err.Number & " " & Err.Description, vbCritical, "Random"
Random = ""
Resume Exit_Random
End Function
On another very vba noob note, I have also been looking for a way to set the RLength to a static 12 characters, rather than having to specify is whenever I run the query. I have tried to do that, but I am clearly using the wrong syntax/commands, because
the function completely breaks when I do.
Thanks for any info or hints,
The Neophyte