January 21, 2021
Hot Topics:

Anything to declare?

  • By Karl Moore
  • Send Email »
  • More Articles »

Decoding declarations...

Are your whites whiter than white itself? If so, you're undoubtedly colour blind.

Are you aged between 22 and 22.5 and looking for a date? If so, e-mail me.

Are you in the (Declarations) section of the (General) thingy? If so, read on.

It's now time to get serious and declare our own variable.

  • Type in:
Dim CountClick as Integer

I'll explain what we just did. In the General Declarations section of your code window (the top bit), we created or "declared" something called CountClick. When our program runs, it creates a 'variable' called CountClick to hold a value of the Integer type - also known as a number.

Don't forget - a variable is just something in which we can store a certain type of information.

Let's test the variable by adding some code to the Command Button we created.

  • From the code window, select "cmdVariableCount" from the object drop-down box and ensure the procedure box reads "Click"

If so, that's great! If not, check your screen resolution. Visual Basic should create the following for you -

Private Sub cmdVariableCount_Click()

End Sub

Don't forget, this isn't anything special. It just defines the code boundaries for that particular item. It knows where the Click event of cmdVariableCount starts (the Private Sub bit) and knows where it finishes (the End Sub bit). You then code all the complicated stuff between those lines.

  • Type in the following:
CountClick = CountClick + 1Msgbox "You've clicked me " & CountClick & " times!"

I'll explain what this code does. First, it sets CountClick to equal whatever it is at that moment, plus one. So when we first start the program, it equals zero. Then when we run this code, it adds one to it. The next time we run the code, another one is added, and so on.

The second line of code displays a simple message box saying "You've clicked me " and then uses the ampersand character to stick in the value of our CountClick variable. We then use the & character once more to add " times!" to the message.

Top Tip: The ampersand (&) character just sticks bits of the message together. If you're interested in how you can fiddle with text in VB, be sure to read this article on string manipulation.

If everything goes a Ned Flander's hokily-dokily, we should get a small program that will display the number of times you clicked the button.

  • Press F5 to run your program and click on the button a few times.

Does it work? Wahay!

Just to recap, we simply declared a variable in the General Declarations section using the format:

Dim VariableName as DataType

We then referenced and changed the variable in code. Now is that supercool or what?

Page 3 of 5

This article was originally published on November 4, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date