duplicated values in generating password

A

Ann

I have a module that generates password. At first I
generate password for 50 users at one time, there is no
duplicated password, but then later if there is a new
user comes in, we need to generate a password
individually, but I found every time I do this, it
duplicates with another user's password in the table,
don't know why.
Here is the code:
--------------------
pwLength = 8

For i = 1 To pwLength

ChooseType = Round(Rnd)

If ChooseType = 1 Then
'Number'
pwStore = Chr(Int((57 - 48 + 1) * Rnd + 48))
Else
'Alphabet'
'Lower Case'
pwStore = Chr(Int((122 - 97 + 1) * Rnd + 97))

End If

pw = pw & pwStore

Next i

MsgBox "Password generated = " & pw

'append to tblLobUsers table username and password


username = Me.ComLobReg

DoCmd.RunSQL "Insert into UserPass (Username, Password)
Values(" & username & ", '" & pw & "')"
 
L

losmac

Before You add new password for new user, try to find this
password in the UserPass table. Loop your sub to generate
passwords while password not exist.

'example: function to detect password exist
Function PasswdExist(strPasswd) as Boolean
Dim rst as Recordset
Dim qwe as String

qwe = "SELECT * FROM UserPass WHERE ((UserPass.Password)
= """ & & "");"
set rst = Currentdb.OpenRecordset(qwe)
If rst.RecordCount > 0 then
PasswdExist = True
Else
PasswdExist = False
End If
rst.Close
set rst = Nothing

End Function


You can use DCount() function too.
 
T

TC

You need to "seed" the random number generator by executing the Randomize
statement (once) before you start using Rnd(). Otherise, Rnd() can/will
return the same sequence of numbers, the next time.

But even when you have done that, "radom" does not mean "unique"! There is a
non-zero chance that the best random number generator in the world, could
generate the random numbers: 1, 1, 1 and 1!

So you will still need to add some code to check that a proposed new
password has not already been used. Do a DLookup() on the UserPass table.
Check online help for how to use DLookup().

HTH,
TC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top