There's 4 main ways to transfer data from one form to another:
1) Use global variables. They're ugly, but they get the job done. Just make sure that you're not using them for another purpose in other parts of your code, or you'll have a debugging nightmare on your hands! Not Recommended.
2) Use public variables or public properties on the called form. This is a very handy way to transfer data. It also abstracts some of the data passing, and allows the VB parameter pop-up window to remind you of what you called things. You can also make read-only and write-only parameters by using the appropiate Property statements. Highly Recommended.
3) Use a public sub/function in the called form to accept your variables. Good for modal dialog-style forms, where you can show the form, let the user do something with the contents, then send the results back (use ByRef function parameters). Can be a little tricky to handle the form show/unload coding for the novice. Recommended.
4) Write the variables to a database table. It's slow, complicated, and Not Recommended.
You can also do variations on #'s 2 and 3, where the called form calls back into the calling form to get it's data. It's tricky, because you first have to pass a reference to the calling form to the called form so the called form knows who to ask for it's data. There might be a special situation where this needs to be used, but I can't think of one off-hand.
Here's an example of technique number 2. Cut and paste this into a text document and save it as "form1.frm"
VERSION 5.00 Begin VB.Form Form1 Caption = "Form1" ClientHeight = 1695 ClientLeft = 60 ClientTop = 345 ClientWidth = 4350 LinkTopic = "Form1" ScaleHeight = 1695 ScaleWidth = 4350 StartUpPosition = 3 'Windows Default Begin VB.CommandButton cmdShowForm2 Caption = "Show Form2" Height = 375 Left = 2400 TabIndex = 2 Top = 420 Width = 1575 End Begin VB.TextBox Text2 Height = 285 Left = 360 TabIndex = 1 Text = "Text2" Top = 1020 Width = 1335 End Begin VB.TextBox Text1 Height = 285 Left = 360 TabIndex = 0 Text = "Text1" Top = 420 Width = 1335 End End Attribute VB_Name = "Form1" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Option Explicit
Private Sub cmdShowForm2_Click()
Dim MyForm As Form
Set MyForm = New Form2
MyForm.FirstName = Text1.Text MyForm.LastName = Text2.Text MyForm.Show vbModal If Not MyForm.WasCanceled Then Text1.Text = MyForm.FirstName Text2.Text = MyForm.LastName End If Set MyForm = Nothing
Cut and paste this into a text document and save it as "form2.frm"
VERSION 5.00 Begin VB.Form Form2 BackColor = &H00808000& Caption = "Form2" ClientHeight = 1815 ClientLeft = 60 ClientTop = 345 ClientWidth = 5745 LinkTopic = "Form2" ScaleHeight = 1815 ScaleWidth = 5745 StartUpPosition = 3 'Windows Default Begin VB.CommandButton cmdCancel Cancel = -1 'True Caption = "Cancel" Height = 375 Left = 4260 TabIndex = 3 Top = 900 Width = 1155 End Begin VB.CommandButton cmdOK Caption = "OK" Default = -1 'True Height = 375 Left = 4260 TabIndex = 2 Top = 300 Width = 1155 End Begin VB.TextBox Text2 Height = 285 Left = 1680 TabIndex = 1 Top = 960 Width = 1335 End Begin VB.TextBox Text1 Height = 285 Left = 1680 TabIndex = 0 Top = 360 Width = 1335 End Begin VB.Label Label1 Alignment = 1 'Right Justify BackStyle = 0 'Transparent Caption = "Last: " Height = 255 Index = 1 Left = 540 TabIndex = 5 Top = 1020 Width = 1035 End Begin VB.Label Label1 Alignment = 1 'Right Justify BackStyle = 0 'Transparent Caption = "First: " Height = 255 Index = 0 Left = 540 TabIndex = 4 Top = 360 Width = 1035 End End Attribute VB_Name = "Form2" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Option Explicit
Private m_FirstName As String Private m_LastName As String Private m_WasCanceled As Boolean
Public Property Get FirstName() As String FirstName = m_FirstName End Property Public Property Let FirstName(RHS As String) m_FirstName = RHS End Property
Public Property Get LastName() As String LastName = m_LastName End Property Public Property Let LastName(RHS As String) m_LastName = RHS End Property
Public Property Get WasCanceled() As Boolean WasCanceled = m_WasCanceled End Property
Private Sub cmdCancel_Click() m_WasCanceled = True Unload Me End Sub
Private Sub cmdOK_Click() m_FirstName = Text1.Text m_LastName = Text2.Text Unload Me End Sub
Private Sub Form_Activate() Static bNotFirstTime As Boolean
If bNotFirstTime Then ' Do nothing Else ' Is first time here. Load form up Text1.Text = m_FirstName Text2.Text = m_LastName bNotFirstTime = True End If End Sub
Private Sub Form_Load() m_WasCanceled = False End Sub
To test this, add these two form files to a new, empty .exe project (remove the Form1.frm that VB provides for you first). Enter a name into form1, click the button, and edit the name in form2. When you click OK, the fields in form1 are updated. If you cancel out, they are left alone.