2015年4月5日 星期日

Access generate random letter A to Z using Rnd

This Access tutorial explains how to generate random letter A to Z / a to z or a mix using Rnd Function.


You may also want to read:


Excel generate random letter A to Z using RANDBETWEEN


Access generate random letter A to Z using Rnd


In order to generate random letter, we have to make use of two Functions.


Rnd


Chr


In the below sections, I will briefly go going through these two Functions. For details, click on the link above.


Access generate random number using Rnd


Access Rnd Function is for use in Access, Access VBA, Excel VBA, it is used to generate random number larger than 0 and smaller than 1 (1 and 0 exclusive).


Syntax of Access Rnd Function


Rnd[(number)]









NumberRnd generates
Not supplied or any positive numberGenerate a new random number
0Return the last generated number
Any negative numberGenerate a new random number and use it every time

Use the below formula to set lowerbound and upperbound for the random number.


Int ((upperbound - lowerbound + 1) * Rnd + lowerbound)

Access Chr Function to convert ASCII value to character


Access Chr Function converts ASCII value to a character. ASCII (American Standard Code for Information Interchange) uses 8-bit code units, an old encoding system which stores mainly numbers, lowercase letters a to z, uppercase letters A to Z, basic punctuation symbols, control codes. Many old systems still use this encoding system. 8 bit means the computer memory uses “8” digits with 1 and 0 combination (binary) to represent a character, 8 bits memory is equal to 1 byte.


Below is the ASCII value to character conversion for letter. For full conversion table, click here.












































































































ASCIIChar
65A
66B
67C
68D
69E
70F
71G
72H
73I
74J
75K
76L
77M
78N
79O
80P
81Q
82R
83S
84T
85U
86V
87W
88X
89Y
90Z
97a
98b
99c
100d
101e
102f
103g
104h
105i
106j
107k
108l
109m
110n
111o
112p
113q
114r
115s
116t
117u
118v
119w
120x
121y
122z

Access generate random letter A to Z / a to Z


To generate random letter, first decide whether you need lower case only, upper case only, or a mix of both.


If you want to generate lower case only, use ASCII value 97 to 122.


CHR(Int ((122- 97+ 1) * Rnd + 97))

If you want to generate upper case only, use ASCII value 65 to 90.


CHR(Int ((90- 65+ 1) * Rnd + 65))

If you want to generate a mix of lower case and upper case, use the VBA custom Function in the following section.


Access VBA Code – generate random letter A to Z / a to z / a to Z


Public Function wRandomLetter(Optional rndType = 1) As String
    Randomize
    Select Case rndType
    Case 1
        randVariable = Int((122 - 65 + 1) * Rnd + 65)
        Do While randVariable > 90 And randVariable < 97
            randVariable = Int((122 - 65 + 1) * Rnd + 65)
        Loop
        wRandomLetter = Chr(randVariable)
    Case 2
        wRandomLetter = Chr(Int((122 - 97 + 1) * Rnd + 97))
    Case 3
        wRandomLetter = Chr(Int((90 - 65 + 1) * Rnd + 65))
    End Select
End Function

Access VBA Syntax – generate random letter A to Z / a to z / a to Z


wRandomLetter(randType)



Access VBA Example – generate random letter A to Z / a to z / a to Z


randTypeOptional. Choose whether you want to generate lower case, upper case or a mix of both.

1 or no input – a mix of both

2 – lower case

3 – upper case










FormulaExplanation
wRandomLetter()Generate a letter which can be lower case or upper case
wRandomLetter(1)Generate a letter which can be lower case or upper case
wRandomLetter(2)Generate a letter which is lower case
wRandomLetter(3)Generate a letter which is upper case

Outbound References


https://support.office.com/en-us/article/Rnd-Function-503CD2E4-3949-413F-980A-ED8FB35C1D80



Access generate random letter A to Z using Rnd

沒有留言:

張貼留言