>> only one instance is added to the forms collection
This is not true for VB. Multiple instances are added. The name of each form would be the same, but in this case, it shouldn't matter. You see, if there are 3 "Form2" forms open and 1 Form1, the Forms.Count property will be 4. If you loop over the forms and call the .SaveRecordMethod, that subroutine will be called on each form.
I prefer to reduce the number of times I need to access the form externally. This allows me to encapsulate the code as much as possible.
If this were my project, I would...
1. Have a source code module that has all the load/save procedures.
2. Have the Form2 (customer form) know how to load and save it's own data.
3. I would have the main form (form1) call a public sub on form2. This sub would load the data and then show the form.
4. I would put the save routine in the QueryUnload event.
There are a couple of tricks that you need to know in order to effectively implement this.
Number 1 is easy. Just add a module and write some public sub/function that returns and saves data.
Number 2 is not so obvious, but it is pretty easy once you get the hang of it.
In form2
Code:
Private lCustomerId As Long
Public Sub ShowForm(ByVal CustomerId As Long)
lCustomerId = CustomerId
' Code here to load the data and set properties
Call Show
End Sub
Then, in form1 (your main form)...
Code:
Private Sub Command1_Click()
Dim oForm As Form2
Set oForm = New Form2
Call oForm.ShowForm(YourCustomerIdNumber)
Set oForm = Nothing
End Sub
Notice the minor difference here. Now, the main form creates an instance of form 2 and calls a public subroutine. Form 2 then loads data, sets properties (captions & text boxes), and then shows itself. The main form does NOT show the sub-form, but causes it to be shown be calling the ShowForm method.
The next 'trick' is using the QueryUnload event to save your data. This is really an awesome event to use. Whenever your form closes, it will call this event. You then have the opportunity to do stuff (like saving data). What's cool about this event is that you can CANCEL it.
The QueryUnload event has 2 byref arguments. The first is Cancel. The default value for this argument is 0. If you set it to any value other then 0, the form will not close. the second parameter is UnloadMode. This argument tells you what caused the QueryUnload.
[tt][blue]
vbFormControlMenu 0 The user chose the Close command from the Control menu on the form.
vbFormCode 1 The Unload statement is invoked from code.
vbAppWindows 2 The current Microsoft Windows operating environment session is ending.
vbAppTaskManager 3 The Microsoft Windows Task Manager is closing the application.
vbFormMDIForm 4 An MDI child form is closing because the MDI form is closing.
vbFormOwner 5 A form is closing because its owner is closing.
[/blue][/tt]
Now that you can capture this event, you can validate data and save. You can even cancel the unload process if the data does not validate.
To test this, create a new VB6 project with 2 forms (Form1 & Form2).
On form 1, put a command button (named Command1).
Then, copy/paste this code.
Code:
Option Explicit
Private Sub Command1_Click()
Dim oForm As Form2
Set oForm = New Form2
Call oForm.ShowForm(6) ' 6 represents your customer id
Set oForm = Nothing
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim oForm As Form
For Each oForm In Forms
If oForm.Name = "Form2" Then
Unload oForm
End If
Next
If Forms.Count > 1 Then
Cancel = 1
End If
End Sub
On form2, put 2 command buttons and name then btnCancel and btnSave. Put 2 text boxes (Text1 and Text2).
Then copy/paste this code.
Code:
Option Explicit
Private lCustomerId As Long
Private UserCancel As Boolean
Public Sub ShowForm(ByVal CustomerId As Long)
UserCancel = False
lCustomerId = CustomerId
' Code to load the data and populate the controls on the form
Call Show
End Sub
Private Sub btnCancel_Click()
UserCancel = True
Unload Me
End Sub
Private Sub btnSave_Click()
Unload Me
End Sub
Private Function ValidateData() As Boolean
ValidateData = True
If Not IsNumeric(Text1.Text) Then
ValidateData = False
End If
If Not IsDate(Text2.Text) Then
ValidateData = False
End If
End Function
Private Sub SaveData()
' code to save the form data
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UserCancel Then
Exit Sub
End If
If Not ValidateData Then
Call MsgBox("Your data does not validate. Unable to save.", vbInformation, "Save Data Error")
Cancel = 1
Exit Sub
End If
Call SaveData
End Sub
Make sense?
-George
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom