Generate Random String in Visual Basic

Tutorial Description:

This tutorial in visual basic will guide us on how to generate random string.

Please follow the step by step guide

1. Open you visual basic 6 application. Select Standard EXE.

2. Add a form (Project in the menu bar > Click Add Form).

3. Add the following to the form:
1 label
1 textbox name it as stringcode
1 command button name it as cmdGenerate

vb6p34. We will create a function for randomization. Paste the code below in the form

Function Rand(ByVal Low As Long, ByVal High As Long) As Long
Rand = Int((High - Low + 1) * Rnd) + Low
End Function

5. Double click the Generate button and paste the code below:

Const KEYS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
Dim intCounter As Integer
Dim strKey As String
Dim intIndex As Integer
strKey = ""
Randomize
For intCounter = 1 To 8
intIndex = Rand(1, Len(KEYS))
strKey = strKey & Mid$(KEYS, intIndex, 1)
Next
stringcode.Text = strKey

6. Save the Project and Run the Program (Press F5).

The program will generate 8 random characters. Just change 8 to the number you want in the for loop statement.
Download

Comments

comments

, , , , ,

Post navigation