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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Variable from form to form? 2

Status
Not open for further replies.

DaveL

Programmer
Jun 8, 2000
40
US
What is wrong with this code? I know it something basic, but I cannot figure it out? I am just trying to figure out how to pass a field value from one form to another. In this case when the button for TestForm0 is clicked, the program opens up TestForm1. I am trying to get TestForm1 to display a Public Variable (FieldA) that was set in TestForm0. What am I doing wrong???

Any help or ideas would be greatly appreciated! Thanks!

TestForm0
Code:
Option Compare Database
Option Explicit
Public FieldA As String

Private Sub CmdBtnFieldA_Click()
On Error GoTo Err_CmdBtnFieldA_Click

    Dim stDocName As String
    Dim stLinkCriteria As String
    
    FieldA = "test"
    stDocName = "TestForm1"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_CmdBtnFieldA_Click:
    Exit Sub

Err_CmdBtnFieldA_Click:
    MsgBox Err.Description
    Resume Exit_CmdBtnFieldA_Click
    
End Sub


TestForm1
Code:
Option Compare Database
Option Explicit

Private Sub Form_Load()
Me.txtFieldA = FieldA
End Sub

 
Hi

Your Public delclaration for your variables should reside in the module section. From here it is accessible to all other forms. Declaring a Public variable in the only exposes it within that form.

hth Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
You can't declare a variable in a form and have it be public for all forms, it's only public for THAT form. You need to move the declare statement to a module in order for it to be available to ALL forms.

Joe Miller
joe.miller@flotech.net
 
I understand now. Thank you all for the help!!!
 
Joe,

You CAN declare a public variable in a form and have it visible to everything in your project! Simply, reference the form!


Gary
gwinn7
 
Also, it can be excellent programming practice to keep the variable declared public in the form. That way the scope of the variable is encapsulated by the form and not simply 'Global'.

This way, you can extend the properties of the form to your needs when you need to pass values.

Gary
gwinn7
 
How do you mean "reference the form"?
Joe Miller
joe.miller@flotech.net
 
Example:

Forms(MyForm).<MyPublicVariable> = MyValue

Another advantage is that you can also place property procedures in your form that can pre-process or validate the value being passed into your form, or retrieved.

Gary
gwinn7

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top