when/how/where is the code triggered?
Just pass the current form to the code...
Call myCodeInAModule(me)
The above passes the current form ref tot he code....
Public Sub MyCodeInAModeule( frm as form)
we now can su the form
frm.Requery
frm!LastName = bla bal bla...
you can thus use "frm" in place of me in the code....
If you want, you can even pass the form + field name as a string.....
Call myCodeInAModule("frmCustomers","LastName")
your module code would look like
Public Sub MyCodeInAModeule(strForm as string, strField as string)
msgbox "the value of form = " & strForm & _
" and that of field = " & strField & _
" is currenlty = " & forms(strForm)(strField)
So, you can even pass control ref if you want.....like
Call myCodeInAModule(me.LastName)
Public Sub MyCodeInAModeule(ctrlLast as contorl)
msgbox "value of last name = " & ctrLast.Value
ctrLast.Requery
I mean, I could type on for a long time here...there is MANY ways to pass
a ref, be it the form, a simple string value, or even the contorl....
I also do have a lot of custom menu code..and in that case, I do pick up the
activeform. But, as a general rule you need to pick it up RIGHT
AWASY...since any kind of focus change..or even code that launches another
form will then change the ref.. Here is a exmaple of some custom menu
code..and not how it picks upt he form RIGHT away (so, if you use
activeForm..then grab it right away..and then your ref will no change if the
current form focus is changed due to somthing as semimply as bumping the
mouse).
I would say that 90% of my custom menu bars call, and run my VBA code.
All you need to do is make the code (a function) public, and then simply
place the function name in the buttons on-action event code.
Further, likely often you will have specific code to a particular form, and
once again, you simply declare those functions (in that form) as public.
The syntax to call the code then is:
=YourFunctionName()
Often, (if not most of the time), you code you call will need to pick up
some information about he current screen etc. So, my code most of the time
starts out, and grabs the current screen name. I use:
Public Function AskInvoicePrint()
Dim tblgroupid As Long
Dim frmActive As Form
Set frmActive = Screen.ActiveForm
tblgroupid = frmActive.frmMainClientB.Form!ID
If frmActive.InvoiceNumber = 0 Then
frmActive.InvoiceNumber = nextinvoice
frmActive.Refresh
End If
DoCmd.OpenForm "guiInvoicePrint", , , "id = " & tblgroupid
End Function
The above is code that the invoice print button runs. note how I right away
pick up the active form. After that, I can easily ref the forms object as if
the code was running much like the code would if put behind a button on the
form. In the above example, I also check if a invoice number has been
generated before printing. And, the Refresh forces a disk write if in fact I
do change the invoice number. And, in addition the above clip also passes
the currently selected sub-form item that the invoice print form needs.
Also, if the code you write is for the particular form, then as mentioned,
you can simply place the code into the forms module code. There is no need
to pick up the active screen...and you can use me. as you
always used.
If you want to see some sample menu bars, and why I use them, you can read
the following:
http://www.members.shaw.ca/AlbertKallal/Articles/UseAbility/UserFriendly.htm
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
(e-mail address removed)
http://www.members.shaw.ca/AlbertKallal