Public Function GenerateKey() As String
' i - this is the loop counter for looping through each letter
' of one part of the key e.g. 'djwhf'
Dim i As Integer
' ii - this is the loop counter for looping through each part of the key
' it loops 5 times (0 based), loop 5 times to generate 5 sections
' e.g. sec1-sec2-sec3-sec4-sec5
Dim ii As Integer
' strParts - this is a 0 based array, so it has 5 elements. Each element is
' one of the 5 sections of the key
Dim strParts(4) As String
' strChar - this string (length: 1) will contain the random numbers Chr() value. E.g. 97 value is 'a'
Dim strChar As String * 1
' intRandom - this will store the random number that was generated between 97-122 (a-z)
Dim intRandom As Integer
' blnVowel - this boolean (true/false) will be used when checking if the generated
' letter is a vowel or not.
Dim blnVowel As Boolean
' strResult - this string will contain the final key before it is returned
Dim strResult As String
'ASCII Codes letters
'a = 97
'e = 101
'i = 105
'o = 111
'u = 117
'z = 122
For ii = 0 To 4
'set ii = 0 and loop until it reaches 4, loop 5 times to generate
' 5 sections e.g. sec1-sec2-sec3-sec4-sec5. ii will increment (+1)
' each time it loops
For i = 1 To 5
' This loop generates 1 random letter that is not a vowel,
' each time it runs. Loop 5 times to generate 5 letters
blnVowel = True
' Set the vowel boolean to true so it doesn't skip the loop below
While blnVowel = True
' This loop runs until we generate a character that is not a vowel
intRandom = Int((122 - 97 + 1) * Rnd + 97)
'generate the random number between 97-122 (a-z)
If (intRandom = 97) Or (intRandom = 101) Or (intRandom = 105) Or (intRandom = 111) Or (intRandom = 117) Then
' This If checks if the generated number matched the ASCII value of any vowel
' If it does find a match, it needs to generate a new number and check again
' It does this over and over until it finds a number that does not match
' the ASCII of any vowel
blnVowel = True
' It matches a vowel so set the boolean to TRUE
Else
' its not a vowel so use it
strChar = Chr(intRandom)
' Convert the number to its corresponding letter ans store in strChar
strParts(ii) = strParts(ii) & strChar ' add the new letter onto the section
blnVowel = False 'it was not a vowel so we dont want to run that loop again
End If
Wend ' keep looping until the While condition is met
Next i ' loop with the next i
If ii = 4 Then
' This if statement simply checks if the section it generated is the final section or not
' If it is the final section, add the section the the strResult but dont add
' a - on the end. Without this If, the key would turn out xxxxx-xxxxx-xxxxx-xxxxx-xxxxx-
strResult = strResult & strParts(ii)
Else
' Its not the final section so add a - on the end of the result
' To seperate it from the next section coming up
strResult = strResult & strParts(ii) & "-"
End If
Next ii ' loop with the next ii
GenerateKey = strResult ' finished generating, return the key to what called it
End Function