VBA example - Microsoft Access: if else to random border color

VBA if else random border color

In this example, you will learn VBA If...Else...Statement to make phrase animate by cutting off one character from the left in a specific timer interval till reaching the end of the phrase. The Random Function(Rnd) is used to generate the random border color of a label. The example demonstrates the RandomBorderColor form containing the URL phrase "WWW.WorldBestLearningCenter":

 VBA example - Microsoft Access Random BorderColor Form 

- When the form loads and the timer runs, the label's border-color will change randomly and the URL phrase "WWW.WorldBestLearningCenter.com" is cut off one character for each time from the left appearing in middle of the label.

To have the RandomBorderColor form as the figure above, you have to create the form in Form Design. See as the figure below:

VBA example - Microsoft Access Random BorderColor Form Design 

-  Drag and drop a label control on the form.

-  Set Border Style and Caption properties of the label:

 Caption: WWW.WorldBestLearningCenter.com

Border Style: Dash Dot Dot

Note: For label's border style and caption, you can set any words or phrases and  border styles that you like. You may see the URL phrase "WWW.WorldBestLearningCenter.com" is in the middle of label(vertical direction). To do so, you have to set the value Top Margin property of  label. For the value, we cannot tell you the specific number because it's according to the height of label. So, you have to set the value by yourself. When you set the value to Top Margin property of your label and you see label's caption that is in the middle of label, it is your right value.

Then apply the VBA Code below:


Option Compare Database

Option Explicit

Dim i As Integer

Dim ca As String


Private Sub Form_Load()

Label0.FontSize = 20 'Set label's font size to 20

TimerInterval = 300

ca = Label0.Caption

i = 0

End Sub


Private Sub Form_Timer()

' Make the words animate

If i < Len(ca) Then

i = i + 1

Label0.Caption = Left(ca, i)

Else: i = 0

End If

Randomize    ' Generate the random number seed

' Generate the random border color of label

Label0.BorderColor = RGB(CInt(255 * Rnd + 1), CInt(255 * Rnd + 1), CInt(255 * Rnd + 1))

End Sub


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.