This program uses multiple forms and a module. The objective is to share variables between
objects in the program. The main form will call a second form to accept user input. User
will enter a date, and then the date form will call a function in the module to determine
the day of the week represented by the user entered date. The date form will store the day
of the week in a public variable, and then return to the main form. The main form will
access and display the public variable maintained by the date form. The date form also
maintains a static variable used to display the number of times the date form has been
called.
Part 1 - Housekeeping / Overhead
Save the Project in your NEW Project directory
Add the Second Form
Click on the Save Icon in the Tool Bar.
Make sure you name the form frmDate and that it is saved to the project directory
Part 2 - Building the Example Application
Build the User Interface for each form
On frmMain | On frmDate | ||
lblDate | Caption Border Style |
txtMonth txtDay txtYear |
Associate with Labels for Keyboard Interface Text Property should be Blank |
cmdGetDate | Caption Default |
cmdOK | Caption Default |
cmdCancel | Caption Cancel |
||
lblCount | Caption Property should be Blank |
Done with the user interface?
On both forms, open the code window and add the comment and Option Explicit
Make sure you save your work.
Run the program: Click on Run | Start With Full Compile
With two forms, this does not buy you much as you can only test the main form.
You have completed the user interface, but some testing remains to be done.
Build the Event Handlers
The first step in coding is to establish the call between the main form and the
subordinate form.
In frmMain, handle the cmdGetDate Click event.
Private Sub cmdGetDate_Click()
frmDate.Show vbModal
lblDate.Caption = "Not done yet"
End Sub
In frmDate, handle the cmdOK and cmdCancel by returning to the main form
Private Sub cmdCancel_Click()
frmDate.Hide
End Sub
Run the program and make sure that you can call the date form and then properly return to main.
Next, declare the public variable that will be shared between the main and date forms. Declare the variable in frmDate.
Option Explicit
Public strDayofWeek As String
This variable can only be declared in one place, but can be referenced from any part of the program
In frmMain, add some code to initialize the public variable.
Private Sub Form_Load()
frmDate.strDayofWeek = "Unknown"
lblDate.Caption = frmDate.strDayofWeek
End Sub
See how the reference to the public variable is fully qualified? You need to specify EXACTLY which variable you are referring too. You want strDayofWeek on frmDate. Also notice that when you type frmDate and press the dot, you get the dropdown containing all of the properties of the frmDate object. The public variable has become a property of the form.
In cmdOK_Click in frmDate add this assignment before the Hide method:
strDayofWeek = "Here is the date"
Replace this line in frmMain's cmdGetDate_Click
lblDate.Caption = "Not done yet"
with
lblDate.Caption = frmDate.strDayofWeek
Running the program should show that the public variable is shared between the forms.
At this point, the forms can talk to each other
Now it's time to implement the project objectives, which is to accept a date from the user and translate it into the proper day of the week. This code will be written as part of frmDate and a Utility module that we will write and add to the project. frmMain is complete.
First, I want to code the logic that will maintain a counter that tells us how many time the Date form has been called.
Private Sub Form_Activate()
Static intCount As Integer
intCount = intCount + 1
lblCount.Caption = "This form has been used " & _
FormatNumber(intCount,
0) & _
IIf(intCount
= 1, " time", " times")
End Sub
In frmDate's Activate event, declare a Static variable. This variable is initialized at zero, when the program starts, and unlike other Local variables, it maintains it's value between calls to the event handler.
Each time the Date form is Activated, add one to the count and then display a message on the form.
IIf(intCount = 1, " time", " times")
Any guesses? This is an inline If-Else statement. It is a function. The first parameter is a condition. When the condition evaluates to True, the second parameter is returned, otherwise the third parameter is returned. It is often called "The Conditional Statement" and it works really well in this situation.
Back to the Date function
The problem I need to solve is to translate the Month, Day, and Year that the user enters into a day of the week. VB has a function called Weekday. It accepts a date string in the format of MM/DD/YY and returns an integer representing the day of the week. I can translate the number to a day of the week using the following logic:
If intDayNumber = 1 Then
strDayofWeek = "Sunday"
ElseIf intDayNumber = 2 Then
strDayofWeek = "Monday"
ElseIf intDayNumber = 3 Then
strDayofWeek = "Tuesday"
ElseIf intDayNumber = 4 Then
strDayofWeek = "Wednesday"
ElseIf intDayNumber = 5 Then
strDayofWeek = "Thursday"
ElseIf intDayNumber = 6 Then
strDayofWeek = "Friday"
ElseIf intDayNumber = 7 Then
strDayofWeek = "Saturday"
Else
strDayofWeek = "*Error*"
End If
Fairly simple logic. Simple enough, yet this is something that I may need to use over and over. I need to package this function so that I can use it again if necessary. I will package this function in a Module.
Add the Module to the Project
Wrap a function around the code that translates a Day Number into a Day String
Public Function strDayName(intDayNumber As Integer) As String
Dim strDayofWeek As String
' Translation code goes here
strDayName = strDayofWeek
End Function
This function is Public, so it can be called from anyplace in the project.
cmdOK_Click in frmDate needs only to:
It's easier than it sounds!
This will reformat the string:
Dim strDateString As String
strDateString = Trim(txtMonth.Text) & "/" & _
Trim(txtDay.Text)
& "/" & _
Trim(txtYear.Text)
This does an edit check to make sure the user entered a real date:
If IsDate(strDateString) = False Then
MsgBox "Enter a valid date"
Exit Sub
End If
Here is the translation of the Date to a Day Number using the Weekday function that comes with VB. Because the Weekday function returns an integer, it can be passed directly to the function called from the modDate Module. he result can go directly into the Public Variable.
strDayofWeek = strDayName(Weekday(strDateString))
That does it! Running the program should result in you entering a date on frmDate, and when you click on OK, you return to the main where the day of the week will be displayed.
Only one more step that is really just a cosmetic and mostly for the convenience of the user. That is to provide some initial values when invoking frmDate.
In frmDate_Load, get the current date, then use the Month, Day, and Year functions to initialize the Text Boxes on the form.
Private Sub Form_Load()
Dim strSaveM As String
Dim strSaveD As String
Dim strSaveY As String
Dim dtmToday As Date
dtmToday = Date
strSaveM = Month(dtmToday)
strSaveD = Day(dtmToday)
strSaveY = Year(dtmToday)
txtMonth.Text = strSaveM
txtDay.Text = strSaveD
txtYear.Text = strSaveY
End Sub
How would the program be different if this code was put in the Activate event? What if frmDate was Unloaded rather that Hidden as a means of returning to the main?
Often, the Shared Public variables are stored in a project module, but in this case, the module created is more of a utility module than a project support module. The goal in using this module was to begin a module that contained date related functions. Placing the Shared Public variable on frmDate was fine as it make frmDate a component that returns a day of the week through a public variable.
A couple of thoughts before this example is complete.
Public Variable can only be declared in one place
Object.Property format is used to refer to a public variable. This demonstrates how a public variable on a form is really a property of that form
Static Variables are Local so they are protected from project level changes