CHAPTER 4 n USERFORMS 143
Figure 4-7. The View Code button displays the code window for UserForm1 (selected).
Add the following module-level variables in the UserForm code window:
Private m_oCustSurvey As cCustSurvey
Private m_blnSaved As Boolean
The m_oCustSurvey variable will do most of the work for us, and the m_blnSaved variable
will store the return value from the m_oCustSurvey object??™s Save method.
Now let??™s put our initialization and cleanup code in place. Add the following code to the
UserForm??™s UserForm_Initialize and UserForm_Terminate events:
Private Sub UserForm_Initialize()
Set m_oCustSurvey = New cCustSurvey
Set m_oCustSurvey.DBWorkSheet = Sheets("Sheet1")
m_oCustSurvey.GetNextID
lblID.Caption = m_oCustSurvey.ID
m_blnSaved = False
ClearForm
End Sub
Private Sub UserForm_Terminate()
Set m_oCustSurvey = Nothing
End Sub
When the form is initialized, we??™re instantiating our cCustSurvey object. Then we??™re setting
the DBWorksheet property. This is a very important step. This value must be stored right
away so the class can determine the next valid ID and so it knows where to store the data it
collects. Then we get the next available ID number and display it in a label. We then initialize
our save success flag to False, and call a function to clear the form.
The ClearForm procedure does nothing more than blank out the text input fields and set
the check boxes values to False (or not checked).
Private Sub ClearForm()
Me.txtPhone.
Pages:
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164