Connecting Tech Pros Worldwide Forums | Help | Site Map

Random alphanumeric string

ironcito
Guest
 
Posts: n/a
#1: Nov 13 '05
Hello!

I'm looking for a way to have a field in my database that will
automatically be filled with a random 4-character alphanumeric string
every time I enter a new record. Like an autonumber field, but
alphanumeric and random. Could anyone tell me how to do this? Please
explain it so that a newbie can do it. Thanks a lot, and have a nice
day! =)


Steve Jorgensen
Guest
 
Posts: n/a
#2: Nov 13 '05

re: Random alphanumeric string


On 28 Mar 2005 00:05:36 -0800, "ironcito" <correo@ironcito.com> wrote:
[color=blue]
>Hello!
>
> I'm looking for a way to have a field in my database that will
>automatically be filled with a random 4-character alphanumeric string
>every time I enter a new record. Like an autonumber field, but
>alphanumeric and random. Could anyone tell me how to do this? Please
>explain it so that a newbie can do it. Thanks a lot, and have a nice
>day! =)[/color]

Something like this, perhaps? This is untested, so it might need some
debugging.

Public function Random AlphaNumString(StringLen As Long) As String
Dim lngCharNum As Long
Dim strResult as String

strResult = Space(StringLen)
For lngCharNum = 1 to StringLen
Mid$(StringLen, lngCharNum, 1) = RandomAlphaNumChar()
Next

AlphaNumString = strResult
End Function

Public function RandomAlphaNumChar() As String
Dim lngBase36Num As Long

Randomize
lngBase36Num = Int(Rnd(1)*36)

If lngBase36Num < 10 Then
RandomAlphaNumChar = Str(lngBase36Num)
Else
RandomAlphaNumChar = Chr(Asc("A") + lngBase36Num - 10)
Endif

End Function
ironcito
Guest
 
Posts: n/a
#3: Nov 13 '05

re: Random alphanumeric string


Hi, and thanks for your answer. You must excuse my ignorance, but where
should I enter all that? =/
Thanks again.

Alan Webb
Guest
 
Posts: n/a
#4: Nov 13 '05

re: Random alphanumeric string


Ironcito,
If you can click on the Modules tab of the database window of an Access
database and the New | Module button works, then add a new Module to your
database and type all that there.

"ironcito" <correo@ironcito.com> wrote in message
news:1112002362.798048.42520@g14g2000cwa.googlegro ups.com...[color=blue]
> Hi, and thanks for your answer. You must excuse my ignorance, but where
> should I enter all that? =/
> Thanks again.
>[/color]


ironcito
Guest
 
Posts: n/a
#5: Nov 13 '05

re: Random alphanumeric string


OK I've added the module! (Should I remove "Option compare database"
from the begining?) Now what do I do with the module, so as to
accomplish my initial objective? Thanks a lot.

Ronald W. Roberts
Guest
 
Posts: n/a
#6: Nov 13 '05

re: Random alphanumeric string


ironcito wrote:
[color=blue]
>OK I've added the module! (Should I remove "Option compare database"
>from the begining?) Now what do I do with the module, so as to
>accomplish my initial objective? Thanks a lot.
>
>
>[/color]

Now that you have the function in a module, you can call it from any
place in your application... such as a form or query or report or
another subroutine in a module section. Remember functions return an
answer so you need to define a string to hold the answer.

Dim MyRandomFourCharacters as string
This is the call:
MyRandomFourCharacters = AlphaNumString(4)

You can also return any number of random characters using the function
given to you.
Dim MyRandomTenCharacters as string
MyRandomTenCharacters = AlphaNumString(10)

You should read help or purchase a book on Access.

Ron

--
Ronald W. Roberts
Roberts Communication
rwr@robcom.com

ironcito
Guest
 
Posts: n/a
#7: Nov 13 '05

re: Random alphanumeric string


Hi!

You must excuse me for my ignorance and persistency, but I'm not
experienced with functions and I do not work much with Access regularly
so as to justify learning everything. Just a final question, once I
have created a module, how do I call it? Suppose I want to create a
form that will automatically fill the field with the 4-character
alphanumeric string everytime I create a new record, but not create a
new string if I edit one... what do I type and where? Thanks! =)

Closed Thread