How to Create a Random Letter and Number

This is the advantage of the online female cialis online best drugstore pharmacy has a fascinating concept behind it of buying less expensive medicines without hassles of dependence of the insurance companies. Whatever, the causes, men can treat male impotence problem effectively with some natural herbs. 4T Plus capsules and Mast Mood oil are not only beneficial for reproductive health but also for general health. viagra pfizer 25mg An online course lets you take it on your viagra shop cute-n-tiny.com own cure name. It viagra generika you could try these out is imperative that you find an authentic source, since that will help you to find the best possible deal, without any risk of purchasing counterfeit or altered Tadalafil tablets.


How to Create a Random Letter and Number Function

In this How To, I will demonstrate how to create a random function for alphabets (letter) and numbers. We can use the random string in our program such as to reset a password, create a reference number, create a product ID, etc. I will also show how to mix the random letter and number into a string. The random function will need a built-in Rnd() function which returns a value less than 1 but greater than or equal to zero. The value of number determines how Rnd() generates a random number.

Create Random Letter Function

We need to create a random function for string by assigning all alphabets from a-z both lower and upper case. Inside the function, we need to assign a number of alphabet to return from the random function. The completed function is shown below.

 

Public Function RandomString(myInt As Integer) As String
Dim myStr As String
Randomize  ‘use Randomize  for not repeating the previous sequence.
myStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Dim i As Long
For i = 1 To myInt
    RandomString = RandomString & Mid$(myStr, Int(Rnd() * Len(myStr) + 1), 1)
Next
End Function

Create Random Number Function

We also need a random function for number by assigning all number 0-9 into the random range. The completed function is shown below.

Public Function RandomNum(myInt As Integer) As String
Dim myStr As String
Randomize
myStr = "0123456789"
Dim i As Long
For i = 1 To myInt
    RandomNum = RandomNum & Mid$(myStr, Int(Rnd() * Len(myStr) + 1), 1)
Next
End Function

 

How to Fill in Zero (0) and Customer ID (Additional)

This is an example of filling in a number 0 in front of Customer_ID. I want to display a certain number of zero (0) in front of the Customer_ID, but limit the total length of zero (0) and Customer_ID is 6 digits. That means there will be 5 number of zero (0) if the Customer_ID is 2 as example below. If the Customer_ID is 14 then the number of zero(0) is 4. We will use a Right () function as the easiest way.

VB Code:

Private Sub Command213_Click()
    Me.txtRamdom = Right("000000" & Me.Customer_ID, 6)
End Sub





Example of Using Random() function

# 1) Display 2 Random letters followed by Customer ID

Per form below, the current record has a Customer_ID is 14. We want to display the Customer_ID after the 2 random letters in the text box. We will an ampersand (&) to connect between the random letter and Customer_ID. After clicking on the Random with ID button the text box on top of a button will display 2 letters and 14. The 2 letters will change every time when clicking the button, but the number 14 will stay the same as it represents the Customer_ID (14) on current record.

VB Code:
Private Sub Command213_Click()
    Me.txtRamdom = RandomString(2) & Me.Customer_ID
End Sub

#2) Display 2 Random letters followed by 6 digits of Zero (0) and Customer_ID

Per form below, the current record has a Customer_ID is 2. We want to display the 2 random letters and a combining of number 0 and Customer_ID in total of 6 digits like “kH000002”. The length of this string is 8 (2+6). If the Customer_ID is 12 then the string will be “kH000012”. Now there are 4 of number 0 placed before 12. We will the RandomString() function and the Right () function in this method as shown below.

VB Code:
Private Sub Command213_Click()
    Me.txtRamdom = RandomString(2) & Right("000000" & Me.Customer_ID, 6)
End Sub

#3) Display 2 Random letters followed by 6 digits of Zero (0) and Customer_ID

The current record has a Customer_ID is 2. We want to display the 2 random letters, Customer_ID and 1 random letter like “kH2Y”. The length of this string will chanomer_ID that is added on.  If the Customer_ID is 12 then the string will be “kH12Y”.

VB Code:
Private Sub Command213_Click()
    Me.txtRamdom = RandomString(2) & Me.Customer_ID & RandomString(1)
End Sub

#4) Combination of RandomString(), Customer_ID, and RandomNum()

The current record has a Customer_ID is 14. We want to display 1 random number, 2 random letters, Customer_ID, 1 random letter, and 2 random numbers like “2HL14Q13”. This method can be used to assign the reference number in database such as a confirmation number. This reference number is not duplicated due to the Customer_ID is a unique key of the table.

VB Code:
Private Sub Command213_Click()
    Me.ReferenceNo = RandomNum(1) & RandomString(2) & Me.Customer_ID & RandomString(1) & RandomNum(2)
End Sub




Related posts