By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,979 Members | 951 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,979 IT Pros & Developers. It's quick & easy.

Random alphanumeric string

P: n/a
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! =)

Nov 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 28 Mar 2005 00:05:36 -0800, "ironcito" <co****@ironcito.com> wrote:
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! =)


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
Nov 13 '05 #2

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

Nov 13 '05 #3

P: n/a
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" <co****@ironcito.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Hi, and thanks for your answer. You must excuse my ignorance, but where
should I enter all that? =/
Thanks again.

Nov 13 '05 #4

P: n/a
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.

Nov 13 '05 #5

P: n/a
ironcito wrote:
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.


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
rw*@robcom.com

Nov 13 '05 #6

P: n/a
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! =)

Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.