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