Random-character Generation

G

Guest

I have a list of exhibitors for whom I would like to generate random
passwords for a web site. Does Access have any way of generating a random
string of letters and numbers of a specified length (say 6)?
 
A

Al Campagna

Amy,
Ascii numbers have an ascii value from 48 to 57
Ascii characters have a range of 65 to 90
Use the RND function to generate random values within those ranges using
this syntax....
Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

Then use that random value with the CHR() function to randomly generate
a number, or a letter.
We'll do that six times, and concatenate the results one Password
string.

Let's say you want all your passwords to have a format of 3 letters and
3 numbers. (AAANNN)
In this example, a button called GenPW is "Clicked"

Private Sub cmdGenPW_Click()
Dim Ctr As Integer
Dim PW As String
Randomize
PW = ""
Test = ""
For Ctr = 1 To 3
PW = PW & Chr(Int((57 - 48 + 1) * Rnd + 48))
Next Ctr
For Ctr = 1 To 3
PW = PW & Chr(Int((90 - 65 + 1) * Rnd + 65))
Next Ctr
[YourPasswordFieldName] = PW
End Sub
--
hth
Al Campagna
Access MVP 2007
http://home.comcast.net/~cccsolutions/index.html

"Find a job that you love, and you'll never work a day in your life."



Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
 
R

Roger Carlson

No built in method, but here's a little function you can use. It will
create a password of numbers, uppercase letters and lowercase letters. Put
it in a global module and call it like this:

strPassword = CreateRandomPassword(10)

-----------------------------
Function CreateRandomPassword(strlen) As String

On Error GoTo Err_RandomizeCharacterField
Dim i As Integer, j As Integer
Dim strSource As String
Dim strTarget As String
Randomize
strSource = "12345678901234567890" & _
"abcdefghijklmnopqrstuvwxyz" & _
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
strTarget = ""
For i = 1 To strlen
'*** select a character position at random
j = Int((Len(strSource) - 1) * Rnd + 1)
strTarget = strTarget & Mid(strSource, j, 1)
Next
'*** when the Target String is complete, pass it back
CreateRandomPassword = strTarget
Exit_RandomizeCharacterField:
Exit Function
Err_RandomizeCharacterField:
MsgBox Err.Description
Resume Exit_RandomizeCharacterField
End Function
--------------------------

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L
 
G

Guest

Three questions:

1. What if I don't want a pattern?
2. What if I want to use this in a query (to update existing records) or
call it as a subroutine (in the code that generates the new record in the
table)?
3. Can this be modified to make sure each one is unique?

TNX
--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda Grayson


Al Campagna said:
Amy,
Ascii numbers have an ascii value from 48 to 57
Ascii characters have a range of 65 to 90
Use the RND function to generate random values within those ranges using
this syntax....
Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

Then use that random value with the CHR() function to randomly generate
a number, or a letter.
We'll do that six times, and concatenate the results one Password
string.

Let's say you want all your passwords to have a format of 3 letters and
3 numbers. (AAANNN)
In this example, a button called GenPW is "Clicked"

Private Sub cmdGenPW_Click()
Dim Ctr As Integer
Dim PW As String
Randomize
PW = ""
Test = ""
For Ctr = 1 To 3
PW = PW & Chr(Int((57 - 48 + 1) * Rnd + 48))
Next Ctr
For Ctr = 1 To 3
PW = PW & Chr(Int((90 - 65 + 1) * Rnd + 65))
Next Ctr
[YourPasswordFieldName] = PW
End Sub
--
hth
Al Campagna
Access MVP 2007
http://home.comcast.net/~cccsolutions/index.html

"Find a job that you love, and you'll never work a day in your life."



Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
 
G

Guest

When I use it in an update query, it's generating the same password every
time. I need a unique password for each exhibitor.

TNX
--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda Grayson


Roger Carlson said:
No built in method, but here's a little function you can use. It will
create a password of numbers, uppercase letters and lowercase letters. Put
it in a global module and call it like this:

strPassword = CreateRandomPassword(10)

-----------------------------
Function CreateRandomPassword(strlen) As String

On Error GoTo Err_RandomizeCharacterField
Dim i As Integer, j As Integer
Dim strSource As String
Dim strTarget As String
Randomize
strSource = "12345678901234567890" & _
"abcdefghijklmnopqrstuvwxyz" & _
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
strTarget = ""
For i = 1 To strlen
'*** select a character position at random
j = Int((Len(strSource) - 1) * Rnd + 1)
strTarget = strTarget & Mid(strSource, j, 1)
Next
'*** when the Target String is complete, pass it back
CreateRandomPassword = strTarget
Exit_RandomizeCharacterField:
Exit Function
Err_RandomizeCharacterField:
MsgBox Err.Description
Resume Exit_RandomizeCharacterField
End Function
--------------------------

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L
 
R

Roger Carlson

Okay. I didn't realize that you needed to use it in a query. For a query,
you have to make just a couple of changes:

Change the Function line to this:
Function CreateRandomPassword(strlen As Integer, Optional seed as Variant)
As String

Oddly, you don't actually have to USE seed anywhere in the function. Then
when you call the function in the Update Query, just pass the Primary Key
value (and autonumber is best) to the function as the seed value, like this:

Update Customer Set CustPassword = CreateRandomPassword(10,[CustID])

I've put a small sample on my website (www.rogersaccesslibrary.com) called
"PasswordGenerator" which illustrates this.
--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L


Amy E. Baggott said:
When I use it in an update query, it's generating the same password every
time. I need a unique password for each exhibitor.

TNX
 
G

Guest

Would this also work if I call it from a code procedure that adds the record
for new exhibitors?

--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda Grayson


Roger Carlson said:
Okay. I didn't realize that you needed to use it in a query. For a query,
you have to make just a couple of changes:

Change the Function line to this:
Function CreateRandomPassword(strlen As Integer, Optional seed as Variant)
As String

Oddly, you don't actually have to USE seed anywhere in the function. Then
when you call the function in the Update Query, just pass the Primary Key
value (and autonumber is best) to the function as the seed value, like this:

Update Customer Set CustPassword = CreateRandomPassword(10,[CustID])

I've put a small sample on my website (www.rogersaccesslibrary.com) called
"PasswordGenerator" which illustrates this.
--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L


Amy E. Baggott said:
When I use it in an update query, it's generating the same password every
time. I need a unique password for each exhibitor.

TNX
 
G

Guest

And which version would I use if I am calling it from a code procedure (for
adding future records)?

--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda Grayson


Roger Carlson said:
Okay. I didn't realize that you needed to use it in a query. For a query,
you have to make just a couple of changes:

Change the Function line to this:
Function CreateRandomPassword(strlen As Integer, Optional seed as Variant)
As String

Oddly, you don't actually have to USE seed anywhere in the function. Then
when you call the function in the Update Query, just pass the Primary Key
value (and autonumber is best) to the function as the seed value, like this:

Update Customer Set CustPassword = CreateRandomPassword(10,[CustID])

I've put a small sample on my website (www.rogersaccesslibrary.com) called
"PasswordGenerator" which illustrates this.
--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L


Amy E. Baggott said:
When I use it in an update query, it's generating the same password every
time. I need a unique password for each exhibitor.

TNX
 
R

Roger Carlson

Depends what kind of code procedure. Is it a bound form? Unbound?

You'd use the second, modified function:

Function CreateRandomPassword(strlen As Integer, Optional seed as Variant)
As String

and call it like any other function, assigning it's value to a variable or
control:

strPassword = CreateRandomPassword(10)

Me.txtPassword = CreateRandomPassword(10)

Because the second argument (seed) is optional, you don't have to send it
when using the function in code.

I have an example of its use in a form in the sample I mentioned.

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L


Amy E. Baggott said:
Would this also work if I call it from a code procedure that adds the
record
for new exhibitors?

--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda Grayson


Roger Carlson said:
Okay. I didn't realize that you needed to use it in a query. For a
query,
you have to make just a couple of changes:

Change the Function line to this:
Function CreateRandomPassword(strlen As Integer, Optional seed as
Variant)
As String

Oddly, you don't actually have to USE seed anywhere in the function.
Then
when you call the function in the Update Query, just pass the Primary Key
value (and autonumber is best) to the function as the seed value, like
this:

Update Customer Set CustPassword = CreateRandomPassword(10,[CustID])

I've put a small sample on my website (www.rogersaccesslibrary.com)
called
"PasswordGenerator" which illustrates this.
--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L


Amy E. Baggott said:
When I use it in an update query, it's generating the same password
every
time. I need a unique password for each exhibitor.

TNX
--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda Grayson


:

No built in method, but here's a little function you can use. It will
create a password of numbers, uppercase letters and lowercase letters.
Put
it in a global module and call it like this:

strPassword = CreateRandomPassword(10)

-----------------------------
Function CreateRandomPassword(strlen) As String

On Error GoTo Err_RandomizeCharacterField
Dim i As Integer, j As Integer
Dim strSource As String
Dim strTarget As String
Randomize
strSource = "12345678901234567890" & _
"abcdefghijklmnopqrstuvwxyz" & _
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
strTarget = ""
For i = 1 To strlen
'*** select a character position at random
j = Int((Len(strSource) - 1) * Rnd + 1)
strTarget = strTarget & Mid(strSource, j, 1)
Next
'*** when the Target String is complete, pass it back
CreateRandomPassword = strTarget
Exit_RandomizeCharacterField:
Exit Function
Err_RandomizeCharacterField:
MsgBox Err.Description
Resume Exit_RandomizeCharacterField
End Function
--------------------------

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L



message
I have a list of exhibitors for whom I would like to generate random
passwords for a web site. Does Access have any way of generating a
random
string of letters and numbers of a specified length (say 6)?
--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda
Grayson
 
G

Guest

It's another public function that runs as part of a massive set of functions
I run to update my Access database from another, not-very-compatible
database. Part of the process is to check for new exhibit records and create
a tracking record (which includes the password) for each of them.

--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda Grayson


Roger Carlson said:
Depends what kind of code procedure. Is it a bound form? Unbound?

You'd use the second, modified function:

Function CreateRandomPassword(strlen As Integer, Optional seed as Variant)
As String

and call it like any other function, assigning it's value to a variable or
control:

strPassword = CreateRandomPassword(10)

Me.txtPassword = CreateRandomPassword(10)

Because the second argument (seed) is optional, you don't have to send it
when using the function in code.

I have an example of its use in a form in the sample I mentioned.

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L


Amy E. Baggott said:
Would this also work if I call it from a code procedure that adds the
record
for new exhibitors?

--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda Grayson


Roger Carlson said:
Okay. I didn't realize that you needed to use it in a query. For a
query,
you have to make just a couple of changes:

Change the Function line to this:
Function CreateRandomPassword(strlen As Integer, Optional seed as
Variant)
As String

Oddly, you don't actually have to USE seed anywhere in the function.
Then
when you call the function in the Update Query, just pass the Primary Key
value (and autonumber is best) to the function as the seed value, like
this:

Update Customer Set CustPassword = CreateRandomPassword(10,[CustID])

I've put a small sample on my website (www.rogersaccesslibrary.com)
called
"PasswordGenerator" which illustrates this.
--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L


When I use it in an update query, it's generating the same password
every
time. I need a unique password for each exhibitor.

TNX
--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda Grayson


:

No built in method, but here's a little function you can use. It will
create a password of numbers, uppercase letters and lowercase letters.
Put
it in a global module and call it like this:

strPassword = CreateRandomPassword(10)

-----------------------------
Function CreateRandomPassword(strlen) As String

On Error GoTo Err_RandomizeCharacterField
Dim i As Integer, j As Integer
Dim strSource As String
Dim strTarget As String
Randomize
strSource = "12345678901234567890" & _
"abcdefghijklmnopqrstuvwxyz" & _
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
strTarget = ""
For i = 1 To strlen
'*** select a character position at random
j = Int((Len(strSource) - 1) * Rnd + 1)
strTarget = strTarget & Mid(strSource, j, 1)
Next
'*** when the Target String is complete, pass it back
CreateRandomPassword = strTarget
Exit_RandomizeCharacterField:
Exit Function
Err_RandomizeCharacterField:
MsgBox Err.Description
Resume Exit_RandomizeCharacterField
End Function
--------------------------

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L



message
I have a list of exhibitors for whom I would like to generate random
passwords for a web site. Does Access have any way of generating a
random
string of letters and numbers of a specified length (say 6)?
--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda
Grayson
 
G

Guest

Another quick (I hope) question: How do I make sure it's unique?
--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda Grayson


Roger Carlson said:
Depends what kind of code procedure. Is it a bound form? Unbound?

You'd use the second, modified function:

Function CreateRandomPassword(strlen As Integer, Optional seed as Variant)
As String

and call it like any other function, assigning it's value to a variable or
control:

strPassword = CreateRandomPassword(10)

Me.txtPassword = CreateRandomPassword(10)

Because the second argument (seed) is optional, you don't have to send it
when using the function in code.

I have an example of its use in a form in the sample I mentioned.

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L


Amy E. Baggott said:
Would this also work if I call it from a code procedure that adds the
record
for new exhibitors?

--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda Grayson


Roger Carlson said:
Okay. I didn't realize that you needed to use it in a query. For a
query,
you have to make just a couple of changes:

Change the Function line to this:
Function CreateRandomPassword(strlen As Integer, Optional seed as
Variant)
As String

Oddly, you don't actually have to USE seed anywhere in the function.
Then
when you call the function in the Update Query, just pass the Primary Key
value (and autonumber is best) to the function as the seed value, like
this:

Update Customer Set CustPassword = CreateRandomPassword(10,[CustID])

I've put a small sample on my website (www.rogersaccesslibrary.com)
called
"PasswordGenerator" which illustrates this.
--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L


When I use it in an update query, it's generating the same password
every
time. I need a unique password for each exhibitor.

TNX
--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda Grayson


:

No built in method, but here's a little function you can use. It will
create a password of numbers, uppercase letters and lowercase letters.
Put
it in a global module and call it like this:

strPassword = CreateRandomPassword(10)

-----------------------------
Function CreateRandomPassword(strlen) As String

On Error GoTo Err_RandomizeCharacterField
Dim i As Integer, j As Integer
Dim strSource As String
Dim strTarget As String
Randomize
strSource = "12345678901234567890" & _
"abcdefghijklmnopqrstuvwxyz" & _
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
strTarget = ""
For i = 1 To strlen
'*** select a character position at random
j = Int((Len(strSource) - 1) * Rnd + 1)
strTarget = strTarget & Mid(strSource, j, 1)
Next
'*** when the Target String is complete, pass it back
CreateRandomPassword = strTarget
Exit_RandomizeCharacterField:
Exit Function
Err_RandomizeCharacterField:
MsgBox Err.Description
Resume Exit_RandomizeCharacterField
End Function
--------------------------

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L



message
I have a list of exhibitors for whom I would like to generate random
passwords for a web site. Does Access have any way of generating a
random
string of letters and numbers of a specified length (say 6)?
--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda
Grayson
 
R

Roger Carlson

Chances are very, very good that each will be unique. I don't remember my
probabilities and statistics well enough to give you a number, but it is
hugely against creating duplicates. What's more, why is it important?
Isn't it okay if two people happen to get the same password?

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L

Amy E. Baggott said:
Another quick (I hope) question: How do I make sure it's unique?
--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda Grayson


Roger Carlson said:
Depends what kind of code procedure. Is it a bound form? Unbound?

You'd use the second, modified function:

Function CreateRandomPassword(strlen As Integer, Optional seed as
Variant)
As String

and call it like any other function, assigning it's value to a variable
or
control:

strPassword = CreateRandomPassword(10)

Me.txtPassword = CreateRandomPassword(10)

Because the second argument (seed) is optional, you don't have to send it
when using the function in code.

I have an example of its use in a form in the sample I mentioned.

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L


Amy E. Baggott said:
Would this also work if I call it from a code procedure that adds the
record
for new exhibitors?

--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda Grayson


:

Okay. I didn't realize that you needed to use it in a query. For a
query,
you have to make just a couple of changes:

Change the Function line to this:
Function CreateRandomPassword(strlen As Integer, Optional seed as
Variant)
As String

Oddly, you don't actually have to USE seed anywhere in the function.
Then
when you call the function in the Update Query, just pass the Primary
Key
value (and autonumber is best) to the function as the seed value, like
this:

Update Customer Set CustPassword = CreateRandomPassword(10,[CustID])

I've put a small sample on my website (www.rogersaccesslibrary.com)
called
"PasswordGenerator" which illustrates this.
--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L


message
When I use it in an update query, it's generating the same password
every
time. I need a unique password for each exhibitor.

TNX
--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda
Grayson


:

No built in method, but here's a little function you can use. It
will
create a password of numbers, uppercase letters and lowercase
letters.
Put
it in a global module and call it like this:

strPassword = CreateRandomPassword(10)

-----------------------------
Function CreateRandomPassword(strlen) As String

On Error GoTo Err_RandomizeCharacterField
Dim i As Integer, j As Integer
Dim strSource As String
Dim strTarget As String
Randomize
strSource = "12345678901234567890" & _
"abcdefghijklmnopqrstuvwxyz" & _
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
strTarget = ""
For i = 1 To strlen
'*** select a character position at random
j = Int((Len(strSource) - 1) * Rnd + 1)
strTarget = strTarget & Mid(strSource, j, 1)
Next
'*** when the Target String is complete, pass it back
CreateRandomPassword = strTarget
Exit_RandomizeCharacterField:
Exit Function
Err_RandomizeCharacterField:
MsgBox Err.Description
Resume Exit_RandomizeCharacterField
End Function
--------------------------

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L



message
I have a list of exhibitors for whom I would like to generate
random
passwords for a web site. Does Access have any way of generating
a
random
string of letters and numbers of a specified length (say 6)?
--
Amy E. Baggott

"I''m going crazy and I''m taking all of you with me!" -- Linda
Grayson
 

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