In this example, you will learn VBA If...Else...Statement with random function to generate the number. The example demonstrates the Lottery form to generate random number from zero to nine. The Lottery form is shown as the figure above.
- When you tick Digit One check box and click the Start command button, first text that represents Digit One will appear and start to generate the number from zero to nine with the random color.
- When you tick Digit Two check box and click the Start command button, second text that represents Digit Two will appear and start to generate the number from zero to nine with the random color.
- When you tick Digit Three check box and click the Start command button, third text that represents Digit Three will appear and start to generate the number from zero to nine with the random color.
- If you want to show the random number in the list box, click the Stop command button. By Looking at the figure below it may be easier to understand:
To have the Lottery form, create a form in Form Design as the figure:
- Drag and drop one logo, one label, three check boxes, three text boxes, one list box, and three command buttons on the form.
- Set Name and Caption properties of a label, three check boxes, and three command buttons:
Label:
Name: lblTitle
Caption: Lottery
Check Box 1:
Name: ChkDigitOne
Caption: DigitOne
Check Box 2:
Name: ChkDigitTwo
Caption: DigitTwo
Check Box 3:
Name: ChkDigitThree
Caption: DigitThree
Command Button 1:
Name: CmdStart
Caption: Start
Command Button 2:
Name: CmdStop
Caption: Stop
Command Button 3:
Name: CmdClear
Caption: Clear
- Set Name properties of three text box, and a list box controls:
Text Box 1:
Name: txtDigitThree
Text Box 2:
Name: txtDigitTwo
Text Box 3:
Name: txtDigitOne
List Box:
Name: lstData
After you designed the form already, apply the VBA code below:
Option Explicit
Option Compare Database
Dim rndnumber As String
Private Sub Form_Load()
ChkDigitOne.Value = 0
ChkDigitTwo.Value = 0
ChkDigitThree.Value = 0
txtDigitOne.FontSize = 30
txtDigitTwo.FontSize = 30
txtDigitThree.FontSize = 30
txtDigitOne.Visible = False
txtDigitTwo.Visible = False
txtDigitThree.Visible = False
Me.lstData.RowSourceType = "Value List"
End Sub
Private Sub ChkDigitOne_Click()
If ChkDigitOne.Value = True Then
Me.txtDigitOne.Visible = True
Else
Me.txtDigitOne.Visible = False
End If
End Sub
Private Sub ChkDigitTwo_Click()
If ChkDigitTwo.Value = True Then
Me.txtDigitTwo.Visible = True
Else
Me.txtDigitTwo.Visible = False
End If
End Sub
Private Sub ChkDigitThree_Click()
If ChkDigitThree.Value = True Then
Me.txtDigitThree.Visible = True
Else
Me.txtDigitThree.Visible = False
End If
End Sub
Private Sub CmdStart_Click()
Me.TimerInterval = 100
End Sub
Private Sub CmdStop_Click()
Me.TimerInterval = 0
rndnumber = ""
If Me.txtDigitOne.Visible = True Then
rndnumber = Me.txtDigitOne
End If
If Me.txtDigitTwo.Visible = True Then
rndnumber = rndnumber & Me.txtDigitTwo
End If
If Me.txtDigitThree.Visible = True Then
rndnumber = rndnumber & Me.txtDigitThree
End If
Me.lstData.AddItem rndnumber
End Sub
Private Sub CmdClear_Click()
Me.lstData.RowSource = ""
End Sub
Private Sub Form_Timer()
' Generate the border color
Me.logo.BorderColor = RGB(50 + CInt(Rnd() * 199), 100 + CInt(Rnd() * 100), 0)
' Generate the number in the text box
Me.txtDigitOne = CInt(Rnd() * 9)
' Generate the ForeColor of the number in the text
Me.txtDigitOne.ForeColor = RGB(50 + CInt(Rnd() * 199), 100 + CInt(Rnd() * 100), 0)
Me.txtDigitTwo = CInt(Rnd() * 9)
Me.txtDigitTwo.ForeColor = RGB(100 + CInt(Rnd() * 99), 50 + CInt(Rnd() * 155), 0)
Me.txtDigitThree = CInt(Rnd() * 9)
Me.txtDigitThree.ForeColor = RGB(100 + CInt(Rnd() * 100), 50 + CInt(Rnd() * 200), 0)
End Sub
0 comments:
Post a Comment