LESSON SUMMARY: Using variables with 'Private' and 'Public' scope.

VBA TutorNote: This lesson refers to exercises that can be downloaded from here!

In 'Lesson 1' we declared a 'Private' [ Local ] variable 'Msg' in a 'procedure' using the following code ....

Private Sub CommandButton1_Click()
Dim Msg as String
Msg = "Hello!"
MsgBox Msg
End Sub

This meant that 'Msg' could only be used or recognized within that Procedure.

However, we will often need to use variables that can be used by several 'procedures', so we need to declare these variables as 'Module level' variables at the top of the code, in what is called the 'Declaration section', in the following way ....

Private Msg as String
_________________________________________________________
Private Sub CommandButton1_Click()
' Rest of code ....


EXERCISE:

This exercise demonstrates the use of a 'Module level' variable called 'Ans'.

STEP 1: Create a new word document and save it as 'RadioButton_test.doc'.

STEP 2: Enter the text "Q1. This is question one?", then press the 'Enter' key to start a new line.

STEP 3: Then select 'View ... Toolbars ... Control Toolbox'

STEP 4: From the Toolbox select RadioButtons.

STEP 5: Deselect the RadioButton by clicking the mouse cursor elsewhere in the document then press the 'Enter' key to start a new line.

STEP 6: Next, repeat step four and five to create a second RadioButton.

STEP 7: In WORD 2000 onwards, you MUST set the "GroupName" value of grouped 'Objects' in their property sheet to the same name.

VBA TutorAn 'Objects' property sheet is accessed by right clicking on the 'Object' and selecting 'Properties' from the menu items listed.

Set the "GroupName" for 'OptionButton1' and 'OptionButton2' to 'group_1', as shown in the screen shot below.




STEP 8: Then set the "Caption" for 'OptionButton1' to 'TRUE', as shown in the screen shot above, and the "Caption" for 'OptionButton2' to 'FALSE'.

STEP 9: After copying the following code, enter the VB Editor and paste the code into the code window.
[ Note that the variable 'ans' has not been declared by a 'Dim' statement at this stage. ]

Private Sub OptionButton1_Click()
OptionButton2.value = False
Ans = 1
End Sub

Private Sub OptionButton2_Click()
OptionButton1.value = False
Ans = 0
End Sub


STEP 10: Then you exit the 'VB editor' and then the 'Design Mode' by deselecting the  button.

When you click on button one its 'value' will be automatically set to 'true'. Your script will manually deselect button two by setting its 'value' to 'false' and the variable 'Ans' will be set to ''1. The reverse will happen if you click on button two, with your script manually deselecting button one by setting its 'value' to 'false' and setting the variable 'Ans' to '0'.

STEP 11 : After entering the 'Design mode' again by clicking on the the  button, access the propertiy sheet for the button one and set it 'Caption' value to 'TRUE'. With the property sheet dialog still open, click on the second button to access its property sheet and enter 'FALSE' for its caption. Finally, close the property sheet dialog box and turn off 'Design Mode' before testing the buttons.

STEP 12 : You can now test the users responses by creating a test button as shown on 'Lesson 2', with the following code.

Private Ans as Integer

Private Sub CommandButton1_Click()
Dim Message
If Ans = 1 Then
Message = "TRUE"
Else
Message = "FALSE"
End If
MsgBox ("The Radio Button State is " + Message + "!")
End Sub



EXPLANATION : USING VARIABLES

If you look at the script for the Button, you will see that a 'module level' variable called 'Ans' has been declared in the 'Declaration section' of the 'module' you are working on. This means that this variable can be used by any procedure within that 'module'. It has also been declared as an integer so we can collect the button response as either a '0' [False] or a '1' [True] by setting the value of the variable 'Ans' in the OptionButton script.

BOOLEAN variables are automatically given the value of '0' [FALSE] when they are declared.
Therefore, always give a FALSE response [or no response] the value of '0', and give a TRUE response the value of '1'.

Look at the Quiz Example in 'VBA Exercise1.doc' which uses RadioButtons.