Randomized Alphanumeric Autonumber-substitute

  • Thread starter Thread starter Rohan McCarthy
  • Start date Start date
R

Rohan McCarthy

I am seeking to change my unique Autonumber field to a Text field that
automatically generates a unique, six-letter alphanumeric sequence, such as:

Y3D8RK
H6GJ9P

(similar to that used by airlines).

Is this possible in Access?

Rohan
 
Rohan McCarthy said:
I am seeking to change my unique Autonumber field to a Text field that
automatically generates a unique, six-letter alphanumeric sequence, such
as:

Y3D8RK
H6GJ9P

(similar to that used by airlines).

Is this possible in Access?

Rohan


The following code will return a string of random uppercase characters and
digits, of the lengh specified in the length argument. In other words, to
return a six-character string, call it like so ...

Something=RandomAlphaNumeric(6)

I'm not aware of any way to ensure that a function like this doesn't return
the same result twice (although it wouldn't happen very often). You'll need
to check for any existing record with the same code, and call the function
again if you find one.

Public Function RandomAlphaNumeric(ByVal length As Long) As String

Const strcAlphaNum As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

Dim lngLoop As Long
Dim strChar As String
Dim strWork As String
Dim lngPos As Long

Randomize
For lngLoop = 1 To length
lngPos = Int((Len(strcAlphaNum) * Rnd) + 1)
strChar = Mid$(strcAlphaNum, lngPos, 1)
strWork = strWork & strChar
Next lngLoop

RandomAlphaNumeric = strWork

End Function
 

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

Back
Top