Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I pass a variable from one form to another form 2

Status
Not open for further replies.

faust13

Programmer
Aug 7, 2001
176
0
0
US
How do I pass a variable from one form to another form?

Simply put:

I want to pass varA from form1 to varA on form2.

Suggestions? ----------------------------------------
Is George Lucas Kidding...
 
Use the form name as part of the name of the variable when you reference it.
 
Okay, how do I reference an object? Like an ADO object instantiated on Form1, but I want to reference it on Form2? ----------------------------------------
Is George Lucas Kidding...
 
I tried your suggestion of using the form name with the variable name (i.e. form1.varA), but it doesn't seem to work. ----------------------------------------
Is George Lucas Kidding...
 
Put a module in your form,
Then declare your varable as follows in the module

Puclic Name As Type.


Now it will go between forms. Brad,
Free mp3 player,games and more.
 
Youngguy was right on track, but he forgot to tell you to make the form's variable Public. Then the variable can be accessed from anywhere in the program, just like all the other form properties:

'In form called frmMain:
Public MyVar as Integer

Form_Load()
MyVar = 3
End Sub

'In module:

Public Sub Main()
Debug.Print frmMain.MyVar 'Displays 0
Load frmMain
Debug.Print frmMain.MyVar 'Displays 3
End Sub

This works for variables and controls.

This can be a better approach than using a global variable in a module, because it allows you to use the same variable name for several forms. Just specify which MyVar you need using the form.variable syntax.



BTW: You can also make user-defined subs and functions public within a form. Call them the same way you'd use the variable:

frmMain.DoMySubroutine

Hope that helps!

-Mike


Difference between a madman and a genius:
A madman uses his genius destructively,
A genius uses his madness constructively.
 
For an extensive coverage of the subject refer to faq222-400 or thread222-37333 _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
You can also do the following :-


Dim example as string

Form2(example) or
Call Form2(example)

'Depending if you want to pass a variable ByRef or ByVal

------------------------

Public Sub Form2(example as string)

End Public

GazC
 
Another good way to do it is to create a Public Sub in the 2nd form.

Here is an example I did:

In the 1st form (called frmDocBloc)
*** Triggered by a dblclick on a grid
*** gRs is my Global Recordest

Private Sub msFlex_DblClick()
Dim sCote As String
Dim sNum As String
Dim sSQL As String

msFlex.Col = 1
sCote = msFlex.Text
msFlex.Col = 2
sNum = msFlex.Text

sSQL = "cote = '" & Apostrophe(sCote) & "'"
sSQL = sSQL & " AND doc_no = '" & Apostrophe(sNum) & "'"

gRs.FindFirst sSQL

If gRs.NoMatch = False Then
Unload Me
frmDocuments.ShowFrmDocuments gRs
End If

End Sub

Then on the 2nd form (called FrmDocuments)

Option Explicit
Dim mRs As Recordset

Public Sub ShowFrmDocuments(ByVal rRs As Recordset)
Dim sSQL As String
Dim bReturn As Boolean

Load frmDocuments
Set mRs = rRs
UpdateScreen mRs
frmDocuments.Show vbModal

End Sub

Private Sub UpdateScreen(rRs As Recordset)

If rRs.RecordCount = 0 Then
Me.cmdPrecedent.Enabled = False
Me.cmdSuivant.Enabled = False
Me.txtCount.Text = rRs.RecordCount
ElseIf rRs.EOF = True Then
rRs.MoveFirst
Else

frmDocuments!txtDoc_id.Text = "" & rRs("doc_id")
frmDocuments!cmb(0).Text = "" & rRs("cote")
frmDocuments!txt(0).Text = "" & rRs("doc_no")
frmDocuments!txt(1).Text = "" & rRs("titre_cote")
frmDocuments!txt(2).Text = "" & rRs("titre")
frmDocuments!txt(3).Text = "" & rRs("numero_regle")
frmDocuments!txt(4).Text = "" & rRs("titre_regle")
frmDocuments!txt(5).Text = "" & rRs("actif")
frmDocuments!txt(6).Text = "" & rRs("semi")
frmDocuments!txt(7).Text = "" & rRs("innactif")
frmDocuments!txt(8).Text = "" & rRs("etat")
frmDocuments!txt(9).Text = "" & rRs("copie")
frmDocuments!txt(10).Text = "" & rRs("annee")
frmDocuments!txt(11).Text = "" & rRs("support")
frmDocuments!txt(12).Text = "" & rRs("localisation")
frmDocuments!txt(13).Text = "" & rRs("descLocal")
frmDocuments!txt(14).Text = "" & rRs("filiere")
frmDocuments!txt(15).Text = "" & rRs("descFiliere")
frmDocuments!txt(16).Text = "" & rRs("commentaire")

DoEvents
LoadMSFlexGridAuteur
LoadMSFlexGridKeyword

End If
End Sub

Hope this exemple will help you

Have fun...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top