You can make it work with alpha, dates etc by using an underlying numeric range.
Here's one for random alpha string of X characters
Code:
'---------------------------------------------------------------------------------------
' Procedure : randomalpha
' Author : jack
' Date : 12/10/2015
' Purpose : Returns random alpha characters based on your HowManyChars value
'---------------------------------------------------------------------------------------
'
Function randomalpha(HowManyChars As Integer) As String
Dim A As Long
Dim Z As Long, i As Long
Dim str As String
Dim ralpha As Variant
Dim Result As String
10 On Error GoTo randomalpha_Error
20 A = 1
30 Z = 26
40 str = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
50 ralpha = Split(str, " ")
60 For i = 1 To HowManyChars
70 Result = Result & ralpha(randomNumber(A, Z) - 1)
'Debug.Print Result
80 Next i
90 randomalpha = Result
100 On Error GoTo 0
110 Exit Function
randomalpha_Error:
120 MsgBox "In Line " & Erl & " Error " & Err.Number & " (" & Err.Description & ") in procedure randomalpha of Module ABC"
End Function
A Random number function:
Code:
'---------------------------------------------------------------------------------------
' Procedure : randomNumber
' Author : Jack
' Created : 11/18/2010
' Purpose : To Generate Random numbers between and including a range of numbers.
'Lo and Hi are the lowest and highest random numbers you wish to generate.
'
'The Randomize keyword is critical to getting different results for each Access session.
'=======================================================================================
'---------------------------------------------------------------------------------------
' Last Modified:
'
' Inputs: N/A
' Dependency: N/A
'------------------------------------------------------------------------------
'
Function randomNumber(Lo As Long, Hi As Long) As Long
10 On Error GoTo randomNumber_Error
20 Randomize
30 randomNumber = Int((Hi - Lo + 1) * Rnd + Lo)
40 On Error GoTo 0
randomNumber_Exit:
50 Exit Function
randomNumber_Error:
60 MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure randomNumber of Module AccessMonster"
70 GoTo randomNumber_Exit
End Function
Here is a test routine.
Code:
Sub testRandomAlpha()
Debug.Print randomalpha(20)
End Sub