Calling Click event of another form

K

Ken Warthen

I have an Access 2007 application in which I'm creating a custom ribbon. I
have a callback module in which there is a routine called onOpenForm. Within
the routine there is a select case statement where code is executed based on
the calling ribbon button. I'm having trouble with one of Case statements
where I need to both open a form and call the click event of a command button
on the form. Here is the relevant code:

Case "btnEnterProject"
If (CurrentProject.AllForms("frmProjects").IsLoaded) Then
Forms!frmProjects.SetFocus
Forms!frmProjects.cmdAddProject_Click
Else
DoCmd.OpenForm "frmProjects", acNormal
Forms!frmProjects.SetFocus
Forms!frmProjects.cmdAddProject_Click
End If

When I run the code I get an error message #2465, "Application-defined or
object-defined error." If anyone can tell me what I'm doing wrong here, I
would be very appreciative.

Ken
 
B

bcap

If you study the procedure declaration that Access gave you for the command
button's click event, you will see that it is created using the "Private"
keyword, like this:

Private Sub cmdMyButton_Click()

This means that the procedure is visible only within that particular module
(or that particular object, to be more precise). To make it visible to
other objects, you need to change "Private" to "Public", thus:

Public Sub cmdMyButton_Click()

Unfortunately, if you have command buttons with the same name on other
forms, you are now going to encounter duplicate declaration problems. To
solve this, you could implement your code in a public procedure (i.e. a
method) which you can call both from the click event and from other objects.

i.e. instead of this:

Private Sub cmdMyButton_Click()

<some code here>

End Sub

You could do this:

Public Sub DoSomething()

<some code here>

End Sub

And this:

Private Sub cmdMyButton_Click()

DoSomething

End Sub
 
S

Steve

Hi Ken,

I'm not familiar with Access 2007 or the ribbon, but if you want to call the
Click event of one form from another, you need to do two things:

1. Make sure the function being called is declared "Public" instead of
"Private" (the default).

2. Use the Forms collection to get a reference to your form and call the
event. For instance, if you know that frmProjects is opened, call your
function as follows: Call Forms("frmProjects").cmdAddProject_Click

That should do it.


Steve
 

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