Random text and numbers

A

Andrew Turner

I want to place a hidden field in a form which inserts data into an Access
database backend. I am using ASP.

Can anyone tell me how I can formulate the value of the hidden field so that
what is entered in the database is a string of say 6 random letters and 2
numbers?

Many thanks
Andrew Turner
 
A

Andrew Turner

I got an answer yesterday to a slightly different question but I can not see
how the answer yesterday applies to this question. Maybe it is just that I
am missing something. How can I put the 'function' that you suggested
yesterday into this code

<input name="Random" type="hidden" id="Random" value="Random text and
numbers need to be generated here">

Sorry for being so slow on the uptake but I have limited experience with
this kind of thing.
 
R

Roger Carlson

It's been a long time since I've done any ASP programming, and I've never
done ASP.NET, so it may need some fiddling. Three possibilities come to
mind:

1) Re-write it as a function (or subroutine) in VBScript. I have no idea if
the Randomize or RND functions exist in VBS, so this may not be an option.

2) Create a SQL statement that will use the function and create a recordset
from that. The SQL statement would look something like this:
SELECT DISTINCT CreateRandomPassword(8) AS NewPassword
FROM AnyTable;

Note: Replace "AnyTable" with the name of a table (any table, it doesn't
matter which although the smaller the better).

3) Create the above SQL statement as a saved query and create a recordset
object using the saved query as the table name.

For the specifics of implementing it in ASP, you should probably ask on the
ASP newsgroup.

Oh, and the function I gave won't necessarily guarentee 6 characters and 2
numbers. It will create a string of 8 characters composed of a random
assortment of upper case letters, lower case letters, and numbers, although
it could be modified to do so.

--
--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
 
A

Andrew Turner

If anyone is interested I have found a solution. Set this code as the value
of a hidden form field and it will insert a random string of characters.

<%
'Our Function generatePassword accepts one parameter 'passwordLength'
'passwordLength will obviously determine the password length.
'The aplhanumeric character set is assigned to the variable sDefaultChars
Function generatePassword(passwordLength)
'Declare variables
Dim sDefaultChars
Dim iCounter
Dim sMyPassword
Dim iPickedChar
Dim iDefaultCharactersLength
Dim iPasswordLength
'Initialize variables
sDefaultChars="abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789"
iPasswordLength=passwordLength
iDefaultCharactersLength = Len(sDefaultChars)
Randomize'initialize the random number generator
'Loop for the number of characters password is to have
For iCounter = 1 To iPasswordLength
'Next pick a number from 1 to length of character set
iPickedChar = Int((iDefaultCharactersLength * Rnd) + 1)
'Next pick a character from the character set using the random number
iPickedChar
'and Mid function
sMyPassword = sMyPassword & Mid(sDefaultChars,iPickedChar,1)
Next
generatePassword = sMyPassword
End Function
Response.write generatePassword(6) 'Call the function & pass in 6 as the
parameter
%>

Thanks for everyones help.
 

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