calling a function from another form

M

Michael S. Montoya

I need to have a form's routine run from another form...here is an example:

frmContacts is loaded. The user double-clicks cboCompanyID to open
frmCompany
frmCompany displays other contacts for that same company in a listbox. If
the user double-clicks a contact in the listbox I would like frmCompany to
close and frmContacts (which is not filtered) to jump to the contactID that
was selected from frmCompany.

Is this possible? I have a "QuickJump" combobox on frmContacts that does a
DAO FindFirst. I was thinking I could call that function on the
frmCompany!lstbox doubleclick, but how do I reference a function on another
form?
 
A

Andy - UK Access User Group

The following describes how to make code on one form call code on
another form.

The rule is that you can only call a Function and not a sub on another
form, and the Function MUST be declared as public.

So on Form2 we have in this case a command button, but to call this
code I must call a function which then calls the code.

This is the code on Form2
--------------------------------------------------------
Option Compare Database
Option Explicit

Public Function Form2Target()
cmdButtonOnForm2_Click
End Function

Private Sub cmdButtonOnForm2_Click()
MsgBox "Hello"
End Sub
------------------------------------------------------------

Now I create a button on form1 to call this code.

----------------------------------------------------------------------
Private Sub cmdOnForm1_Click()
Dim dummy As Variant
DoCmd.OpenForm "Form2"
Dim frm As Form
Set frm = Forms!Form2
dummy = frm.Form2Target()
End Sub
-----------------------------------------------------------------------

Don't expect any intelisense help when typing in frm.Form2Target().

You can also make one form control another form with a Form variable
doing virtually anthing you can imagine. It is quite a neat little
technique.

Hope this helps Michael.
 
G

Graham R Seach

Or it can be simplified as follows:
dummy = Form_Form2.Form2Target()

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 

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