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

Where and how do I declare a public a variable to pass between forms 3

Status
Not open for further replies.

rew2009

Technical User
Apr 21, 2009
114
US
I am opening up a mainform “frmMAIN” that will have command buttons to open a popup form “frmSECOND” and “frmTHIRD” and several others. I want to declare a public variable “strA1” that can be used and passed between the various popup forms. Where do I declare this variable? At this point I am only familiar with using the event handling sections of the vb code (such as in the command buttons and “on click” or “on open” etc of the form objects”) so if the public variables are declared elsewhere I would need to be pointed in the right direction. I tried putting it in the declaration section of the “frmMAIN” under
__________________
Option Compare Database
Public strA1
___________________

And then tried to define “strA1” in the “on open” event:

Private Sub Form_Open(Cancel As Integer)
Dim strA1 As String
strA1 = "City"
Me.Text20.VALUE = strA1

End Sub

but it only worked in the “frmMAIN” and did not pass to the popup forms “frmSECOND”.

So where do I put it and is this the right syntax?

Thanks
 
Howdy rew2009 . . .

In the [blue]declaration section[/blue] of a module in the modules window. Example:
Code:
Option Compare Database
Option Explicit

Public [blue][B][I]VariableName[/I][/B][/blue] As [blue][B][I]Type[/I][/B][/blue]

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
rew2009 . . .

Woops! ... hit submit too soon. You can also use the declaration section of the forms code module, but the form has to be open to gain access!

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
TheAceMan1, it still is not working. I know I am doing something wrong. I open the form and then go into “design mode” then right click the upper left control square to work on the form as the object, Then I go to the properties and the event tab for the form and then to the “On Open” for the form and then I assume that I am in the vb module for the form and scroll to the top where the "Option Compare Database" is located and that I understand is the Declaration section -- and that is where I put the following info:

Option Compare Database
Option Explicit
Public strA1 As String

I then go back to the “on open” event for the form and give the strA1 a value and display it with the text box value as such:

Private Sub Form_Open(Cancel As Integer)
'Dim strA1 As String
strA1 = "City"
Me.Text20.VALUE = strA1 'this on the opening page of the main form

End Sub

This works fine on the first form – the word “City” shows up in the text20 box on the first form. But when I open the second form “frmSECOND” from a command button on this main form, this “strA1” does not seem to pass through. On the second popup form I put the following in its “On Open” event box:

Private Sub Form_Open(Cancel As Integer)
Me.Text20.VALUE = strA1
End Sub

But nothing shows up in its “text20” box.

I may not be putting this code in the right module boxes – I am just not that familiar with going beyond just putting code in the event tabs of these form objects.

Your thoughts.
 
rew2009 . . .

So sorry! I didn't explain why your code failed. In your code you declare [blue]strA1[/blue] twice. Once as a public variable ([green]this was properly done[/green]), then again in a subroutine as a string:
Code:
[blue]__________________
Option Compare Database
[purple][b]Public strA1[/b][/purple] 
___________________

Private Sub Form_Open(Cancel As Integer)
   [purple][b]Dim strA1 As String[/b][/purple]
   
   strA1 = "City"
   Me.Text20.Value = strA1
End Sub[/blue]
Your problem here is that the sub addresses its own variable Dim strA1 instead of the public (access 1st looks for variables within the current running routine ... then looks publically!). Then when the open event is finished the dimmed strA1 falls out of scope (no longer exists). This still leaves the public variable which you have not accessed in tact.

So what you want to do is address the public variable. This changes your code to:
Code:
[blue]__________________
Option Compare Database
[purple][b]Public strA1 as String[/b][/purple] 
___________________

Private Sub Form_Open(Cancel As Integer)

   strA1 = "City"
   Me.Text20.Value = strA1

End Sub[/blue]
This should work ... as long at this form stays open Public strA1 is available to all other forms and code. Moving the variable to a module in the modules window simply allows you to close the form without loosing the variable!

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
I am sure we are on the right track but something is still not right. It is still not working. I copied the code out of the module and this is the code that I am using in the first form:

Option Compare Database
Public strA1 As String

Private Sub Form_Open(Cancel As Integer)
strA1 = "City"
Me.Text20.VALUE = strA1


And this is the only code that I have in the second form (on open):

Private Sub Form_Open(Cancel As Integer)
DoCmd.Maximize
Me.Text20.VALUE = strA1
End Sub

The word “City” appears in the first form text20 box but not in the second form text20 box.

I am baffled!

 
Me.Text20.VALUE = Forms!frmMAIN.strA1

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

"This should work ... as long at this form stays open Public strA1 is available to all other forms and code"

As David Letterman would say, "What?"

Sorry, Aceman, not in v2003! Declaring it Public in a form's module makes it available anwhere in that form's module, but not to other forms, whether the first form is open or not. It has to be in a standard module to be available to one and all. And once a value has been assigned, as in the first form opened, it will be available to all whether that form stays open or not, which is what the OP needs.


The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
A Public variable declared in a form module may be accessed as any other property of the form.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
True, when the form is open, but not by simply referring tro the variable's name, as in

Me.Text20.VALUE = strA1

as he's trying to do. It just seems much simpler to me to place the declaration in a standard module as AceMan originally suggested.

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
rew2009 . . .

Move [blue]Public strA1 As String[/blue] to the declaration section of a module in the modules window and try again. You should have no problem just referencing [blue]strA1[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
It Looks like placing it in a standard module is the best way to do it but my knowledge is limited here. What is a standard module? Where is it located (how can I get to it) and what would the code look like to be placed in it.

Your thoughts?

 
From the Objects box (where you see Tables, Forms, Modules, etc)

Click on Modules
Click on New
Paste in the variable declaration
Click on the Save icon
Name the module
Exit by clicking on the Access red key icon

You're done!

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
As the variable is originally set in frmMAIN and is used by dependants forms, I see no reason to pollute the global namespaceand thus still suggest to follow my suggestion timestamped 31 May 09 18:28.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV said:
[blue] ... [purple]I see no reason to pollute the global namespace[/purple] and thus still suggest to follow my suggestion timestamped 31 May 09 18:28.[/blue]
I have to admit ... I'm surprised to see such a statement from you ... [sad] A programmer uses the global namespace for something important to them and you want to call it pollution!
The bottom line here, is that [blue]its a perference![/blue] determined by the programmer. If assigning any variable to the global namespace pollutes it then we couldn't use golbal variables at all! ... and [purple]how many times in the past have you yourself suggested this same global namespace for use![/purple] I thought this was one of the powers of access, now you say don't use it! ... [blue]as if it will cause problems[/blue] ... [surprise] At least for this one instance. [green]Give me a break![/green]

I have two major reasons why I use the global namespace instead of the same for form:
[ol][li]I can close the form if I have to and maintain the variable.[/li]
[li]I can directly access the variable instead of having to use a form reference![/li][/ol]
... and [purple]that is my perference![/purple]. In my history of globals ... they have never failed . . .

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
I feel like a broken record. I tried the solution suggested above about the standard module and still can’t get it to work. I tried declaring strA1 as public in the standard module as missingling suggested (very good instructions by the way). I put in the following in the standard module:

Option Compare Database
Public strA1 As String

Then I gave the strA1 a value when I open my first form:

Private Sub Form_Open(Cancel As Integer)
strA1 = "City"
Me.Text15.VALUE = strA1
End Sub

And it showed “City” in the Text15 box after opening

However, I opened a second form from the first with a command button and placed the following code in the “on open” event:

Private Sub Form_Open(Cancel As Integer)
Me.Text20.VALUE = strA1
End Sub

When the second form opened the text box text35 was blank.

I then tried PHV approach with :
Me.Text20.VALUE = Forms!frmMAIN.strA1

and that worked fine (Thanks PHV). However, I tried to reset the variable in the second form and pass it to the first form by the following code in the “on change” event of the text box Text35:

'Forms!frmMain.strA1 = Me.Text20.VALUE

This did not work.

I need to be able to send the variable in both directions that was why the global public method was so appealing.

This seems to be a very challenging problem.

Any thoughts?

 
rew2009 . . .

Does not yur 2nd form reference [blue]Me.Text15.VALUE[/blue]
instead of [blue]Text35[/blue]!

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 

However, I opened a second form from the first with a command button and placed the following code in the "on open" event:
Private Sub Form_Open(Cancel As Integer)
Me.Text20.VALUE = strA1
End Sub

When the second form opened the text box text35 was blank.

Well, text35 would be blank, wouldn't it? You've assigned strA1 to Text20, not Text35!


The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top