Connecting Tech Pros Worldwide Help | Site Map

Random alphanumeric string

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 13th, 2005, 08:29 AM
ironcito
Guest
 
Posts: n/a
Default Random alphanumeric string

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! =)


  #2  
Old November 13th, 2005, 08:29 AM
Steve Jorgensen
Guest
 
Posts: n/a
Default 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
  #3  
Old November 13th, 2005, 08:29 AM
ironcito
Guest
 
Posts: n/a
Default Re: Random alphanumeric string

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

  #4  
Old November 13th, 2005, 08:30 AM
Alan Webb
Guest
 
Posts: n/a
Default 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]


  #5  
Old November 13th, 2005, 08:31 AM
ironcito
Guest
 
Posts: n/a
Default 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.

  #6  
Old November 13th, 2005, 08:32 AM
Ronald W. Roberts
Guest
 
Posts: n/a
Default 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

  #7  
Old November 13th, 2005, 08:33 AM
ironcito
Guest
 
Posts: n/a
Default 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! =)

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.