3.2 Write some code with a variable

Right under the line Option Compare Database, enter the following line:

dim counter as Integer  
    

PIC

Then, in the definition of Command0_Click, enter the following:

counter = counter + 1 ’ increment the counter  
Command0.Caption = counter ’ display the counter as the caption  
    

PIC

Next, we can test the code. Use the task bar to switch back to Microsoft Access, and switch the view to “Form View”.

PIC

You can now try to click the button a few times. Each time you click the button, the number displayed on the button should increment.

PIC

Congratulations, you have just used variables in VBA!