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

Passing Information between forms

Status
Not open for further replies.

areric

Programmer
Jul 13, 2003
47
US
How do i pass information between forms. If i make a global variable will it apply to all forms.
 
Depends on what information you are passing to the new/next form. If you could be a little more specific perhaps I could address...
 
ok i have two variables in a form called Rental Information, they are MovieID and MovieName. They are global variables in the Renal Information form. From rental information there is a button "Check Out" you click the check out button it launches the check out form where it needs (at the least) the movieid. How can i get the id from one form to another.
 
The "nicer" way is to use the OpenArgs argument of DoCmd.OpenForm - you pass in a string containing your data and interpret the incoming arguments in the new form.

i.e.
Code:
'in the calling form
DoCmd.OpenForm "frmCheckout",,,,,,,,,,,& intMovieID 
DoCmd.Close acForm, Me.Name, acSaveNo




'in the frmCheckout's Open() event
    If Nz(Me.OpenArgs) = "" Then
        msgbox "ERR"
        Cancel = True
    Else
        Me.RecordSource = "SELECT * FROM MOVIES WHERE " & Me.OpenArgs



--
Find common answers using Google Groups:
[URL unfurl="true"]http://groups.google.com/groups?group=comp.databases.ms-access[/URL]

Corrupt MDBs FAQ
[URL unfurl="true"]http://www.granite.ab.ca/access/corruptmdbs.htm[/URL]
 
Blah, you get the idea. Bugfixes (v1.01):

Code:
'in the calling form
DoCmd.OpenForm "frmCheckout",,,,,,,,,,,"MovieID=" & intMovieID 
DoCmd.Close acForm, Me.Name, acSaveNo




'in the frmCheckout's Open() event
    If Nz(Me.OpenArgs) = "" Then
        msgbox "ERR"
        Cancel = True
    Else
        Me.RecordSource = "SELECT * FROM MOVIES WHERE " & Me.OpenArgs
 
getting this error when i click the button that opens the new form using your code - Wrong number of arguments or invalid property assignment.


The exact code is this
stDocName = "Check Out"
DoCmd.OpenForm stDocName, , , , , , , , , , , "MovieID=" & intMovieID
 
also if i do do the select query like that how do i seperate the information. like id i need to get movie_name and say genre how do i take the values in those columns (now stored in me.recordsource) and seperate them into two variables movieName and movieGenre? thanks alot
 
heh, sorry, take out any number of commas. OpenArgs is the last argument available, so use Intellisense to figure out how many are needed. Something like 4.

Just checked, it's actually 6.

DoCmd.OpenForm stDocName, , , , , , , , , "MovieID=" & intMovieID
 
Comment One:

In order to NOT have to count commas, you could also pass "By Name", as follows:

DoCmd.OpenForm FormName:=stDocName, OpenArgs:="some string"

The Intellisense reveals the exact name you need to use, and note that the delimiter is ":=", not just "=". You can use this on Access Built-In procedures and, at least in Access 2K, your own procedures as well. Using this style, not only don't you have to count commas, but notice you only have to deal with those optional arguements that you care about. It also doesn't matter what order the arguements appear. And to top it all off, it is more self-documenting.

Comment Two:

Whenever I have to pass information between forms, I pass the information to the form by setting the form's property. This is a bit advanced. It relies on the fact that forms are class objects and that you can define your own properties and methods for the form. I use this technique all the time. One big advantage to this is that you alleviate the need for Global variables. IMHO, Global variables are evil. I easily manage to write even very large and complicated apps without using even one Global. Since forms can easily communicate with each other as class objects, there is very little real need for them. This also presupposes that you write your apps as a series of state-less procedures, that is, no event procedure depends on any other event procedure being called first, or what is has done, save any information that you can read from the controls on the form.




Peleg
PelegNOSPAM@PStrauss.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top