I don't like the method suggested in the faq that chip pointed to. The problem I have is that you must set properties on the form before opening it, then you open it, and finally, check the properties. I have another method that I prefer, and would like to share with you.
On form 2, you would have several text boxes to store FirstName, LastNAme, title, age, and Type. Assume that the text boxes are named, txtFirstName, txtLastName, txtTitle, txtAge and txtType.
On form 2, create a public Subroutine.
Code:
Option Explicit
Private mLastName As String
Private mFirstName As String
Private mTitle As String
Private mAge As String
Private mHumanType As String
Private bCancel As Boolean
Public Sub GetUserSelection(ByRef LastName As String, ByRef FirstName As String, ByRef Title As String, ByRef Age As String, ByRef HumanType As String, ByRef Cancel As Boolean)
bCancel = True
txtLastName.Text = LastName
txtFirstName.Text = FirstName
txtTitle.Text = Title
txtAge.Text = Age
txtType.Text = HumanType
Call Show(vbModal)
LastName = mLastName
FirstName = mFirstName
Title = mTitle
Age = mAge
HumanType = mHumanType
Cancel = bCancel
End Sub
Private Sub btnCancel_Click()
Unload Me
End Sub
Private Sub btnOk_Click()
mLastName = txtLastName.Text
mFirstName = txtFirstName.Text
mTitle = txtTitle.Text
mAge = txtAge.Text
mHumanType = txtType.Text
bCancel = False
Unload Me
End Sub
On the first form, you would have something like this...
Code:
Private Sub Command1_Click()
Dim strLastName As String
Dim strFirstName As String
Dim strTitle As String
Dim strAge As String
Dim strHumanType As String
Dim bCancel As Boolean
Call Form2.GetUserSelection(strLastName, strFirstName, strTitle, strAge, strHumanType, bCancel)
If bCancel Then
Exit Sub
End If
[green]' user did not cancel so do something with the data[/green]
End Sub
The trick here is to call the Show method of the form from within the form itself. Also, you need to use byref parameters to pass the data back to the calling form. This method only works if you are opening the form modally.
I prefer this method because you cannot forget to set a property on the form, because there are no properties. Since all of the parameters are required, you must supply them.
I find this code easier to maintain. Suppose, for example, you call this form from several places in your code. Then, 6 months from now, you want to add shoe size or eye color. With my method, you need to add parameters to the function. After adding the parameters to the function, you will get compile errors everywhere else. So you will know all the places in the code that need to be updated. With Chip's property method, your code would compile and you would have to find all the places (manually) that you are calling this form.
-George
Strong and bitter words indicate a weak cause. - Fortune cookie wisdom