MS-Access / Getting Started

Registering an Application

In order to unlock a limited-use application, or even to use an application for the first time, you may require users to register with you. Let's take a look at ways you can generate registration keys to be used by your application and then how to validate one of the keys.

Generating Registration Keys

A registration key can be as simple or as complex as you decide to make it. Registry keys can be formed with numbers, characters, or a mixture of the two. Using code, we can generate registration keys to be used by an application. In the next section, we take a look at how to verify whether a key is valid.

Our registration keys will use randomly generated ASCII characters within a range of valid characters. There are also certain characters that we don't want included in the key. Start by creating a new module and add the following constants to the module. These constants define the range of valid characters for the key. To exclude additional characters, change the EXCLUSIONS constant shown here:

' character constants
Private Const ASCII_0 As Long = 48 ' numeric 0
Private Const ASCII_9 As Long = 57 ' numeric 9
Private Const ASCII_A As Long = 65 ' upper-case A
Private Const ASCII_z As Long = 122 ' lower-case z
Private Const EXCLUSIONS As String = "[\]^_`?<>,.;:'{}|/+=()*&%$#@!~"""

Numeric Registration Keys

To generate a numeric registration key, we need to generate a string of numbers between the characters for 0 to 9. Add the following routine to the module to generate a random string of digits:

Public Function GetNumericKey(bLength As Byte) As String
    Dim i As Byte
    Dim s As String
    Dim ch As Integer

    For i = 1 To bLength
	' get a random character
	ch = Int((ASCII_9 - ASCII_0 + 1) * Rnd + ASCII_0)

	' append it to the string
	s = s & Chr(ch)
    Next

    GetNumericKey = s
End Function

Alpha-Numeric Registration Keys

An alpha-numeric key will contain both letters and numbers, but not symbols defined in the exclusion list. Start by creating a new routine called GetAlphaNumericKey with the following declarations:

Public Function GetAlphaNumericKey(bLength As Byte) As String
    Dim i As Byte
    Dim s As String
    Dim ch As Integer
    For i = 1 To bLength

Next, we need to get a random character that is not in the list. To do this, we'll get a character as shown in the previous example, but check it against the EXCLUSIONS constant using the InStr function:

	' get a random character that is not in the exclusion list
	' make sure it is not an excluded character
	Do
	    ch = Int((ASCII_z - ASCII_0 + 1) * Rnd + ASCII_0)
	Loop Until (InStr(EXCLUSIONS, Chr(ch)) = 0)

	' append it to the string
	s = s & Chr(ch)
    Next

    GetAlphaNumericKey = s
End Function

Testing the Key Generation

To test these functions, add the following routine to the module. Replace the Debug.Print statement shown here to add the random keys to a file or table to use them later:

Public Sub TestRandomKeys()
    Dim i As Integer

    For i = 1 To 3
	Debug.Print GetNumericKey(10)
    Next

    For i = 1 To 3
	Debug.Print GetAlphaNumericKey(25)
    Next
End Sub

When you run this code, you should see output similar to the following in the Immediate window:

3814078245
0690709966
1196811203
sAdn6ml75RvFBG70wgPRRpNeL
MAA5kmHOzdTnIsVkIxKFjs8sr
hHXmkPX54ny1aw9LTsm3tTb41

By modifying the list of exclusions, these functions can also be used to create randomly generated strong passwords.

[Previous] [Contents] [Next]