Passing arguments by reference to a Form

J

Jess

How can I pass parameters (two o more) by reference to an Access form? Can
anybody post an example?

This is what I would like to implement:

Arguments by reference are passed from a from
The form receving the arguments is open
Arguments are updated
form receiving the arguments is closed

Thanks
 
V

vanderghast

The easiest way would be to use Access global variable, to read then in the
open event, and to write into them in the unload event of the form...

if that is not applicable (because you don't like global variables, or
because you cannot relay on 'fixed' variable names):

add public methods to the form, say Initialise and GetArgs:

---frm-----------------------
Public Sub Initialise(x As long, y As long)
' do something with x and y
' probably setting some control value
... = x
... = y
End Sub

Public Sub GetArgs( ByRef x As long, ByRef y As long)
x = ...
y = ...
End Sub
--------------------------------


So, programA creates the form object frm,
programA calls the method frm.Initialise
programA makes the form visible, frm.Visible=true
and before the form closes definitively
programA calls the method frm.GetArgs


The tricky part is probably that your code is under frm flow of execution,
NOT under programA flow of execution, when frm closes (ie, your programA is
NOT the one who closes frm, but it is the end user, trough the GUI, who
decided to close it). If so, in the Unload event of frm, you would have to
make programA calling frm.GetArgs method. If programA is a form, or another
object, you can pass it in the Initialise method of frm:


--- frm (revisited ) ------------------------------
Private programA_Is AS Object ' <-- new
Public Sub Initialise(x As long, y As long, _
who As object) ' < --modified
' do something with x and y
' probably setting some control value
... = x
... = y
programA_Is = who ' <--new
End Sub

Public Sub GetArgs( ByRef x As long, ByRef y As long)
x = ...
y = ...
End Sub

Private Sub Form_Unload(Cancel As Integer) ' <-- new
programA_Is.CallGetArgs( Me )
End Sub
------------------------------------------------


and programA method CallGetArgs is something that will supply the right
parameters to frm.CallArgs:


----programA ----------
Public Sub CallGetArg( x As Form_xyz )
x.CallArgs( foo, fee)
End Sub
-------------------------


(Note that instead of Object, you could use an Interface, to be safer, which
will impose that the third parameter of Initialise own a method
CallGetArgs( xxx As Form) and so the line

programAIs.CallGetArgs( Me )

would be validated at compile time, rather than at runtime.)


Well, there is nothing really wrong with global variables too, in general...

As far as error handling, mainly in Form_Unload, if any error occurs,
preferable to Cancel = true, at least in the debug version, so you may be in
a better position to track back the source of the error.



Vanderghast, Access MVP
 
A

Allen Browne

If the goal is to update some fields in a record, it might be better to do
this with an Update query statement instead of opening a form.

Say you want to update tblClient to change City to "New York" for ClientID
77. The code to do that would be:
Dim strSql As String
strSql = "UPDATE tblClient SET City = ""New York"" WHERE ClientID = 77;"
DBEngine(0)(0).Execute strSql, dbFailOnError

More information in this article:
http://allenbrowne.com/ser-60.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top