Clicking a button via VBA

G

GLT

Hi,

I am trying to (using VBA), open a form, populate a combo box, and then
clicka button on the newly opened form...

I have tried the following:

DoCmd.OpenForm "frm01_Services"

Forms.frm01_Services.cmbServer.Value = Me!Server
Forms.frm01_Services.cmdGetServicesData.SetFocus
Forms!frm01_Services.cmbserver_Click <<-----Error

Line # 3 bombs - is there a function in access that allows me to do this?

Any help is always greatly appreciated...

Cheers,
GLT.
 
D

Dirk Goldgar

GLT said:
Hi,

I am trying to (using VBA), open a form, populate a combo box, and then
clicka button on the newly opened form...

I have tried the following:

DoCmd.OpenForm "frm01_Services"

Forms.frm01_Services.cmbServer.Value = Me!Server
Forms.frm01_Services.cmdGetServicesData.SetFocus
Forms!frm01_Services.cmbserver_Click <<-----Error

Line # 3 bombs - is there a function in access that allows me to do this?


You have to modify the definition of the event procedure, in the module of
the form containing it, to make the procedure Public instead of Private. I
think in this line:
Forms!frm01_Services.cmbserver_Click <<-----Error

.... you probably meant "cmdGetServicesData_Click", since that looks
"cmdGetServicesData" should be the name of your command button. But it
still won't work unless you modify the declaration to be like this:

Public Sub cmdGetServicesData_Click()

Then you should be able to call it like this:

Forms!frm01_Services.cmdGetServicesData_Click

You don't even have to SetFocus to the button, since you are calling the
procedure directly.
 
G

GLT

Hi Dirk,

Thanks for your reply, from reading your reply and other posts, I have done
the following:

1) Created a Public Sub module wioth the following:

Public Sub cmdGetServicesData_Click()

Forms!frm01_Services.cmdGetServicesData_Click

End Sub

2) My original code (mistake amended):

Private Sub Form_DblClick(Cancel As Integer)

DoCmd.OpenForm "frm01_Services"
Forms.frm01_Services.cmbServer.Value = Me!Server
Call cmdGetServicesData_Click

End Sub

When I try to run the above, I get a run-time error 2465,
application-defined or object defined error. Am i putting the code in the
correct places?
 
D

Dirk Goldgar

GLT said:
Hi Dirk,

Thanks for your reply, from reading your reply and other posts, I have
done
the following:

1) Created a Public Sub module wioth the following:

Public Sub cmdGetServicesData_Click()

Forms!frm01_Services.cmdGetServicesData_Click

End Sub

2) My original code (mistake amended):

Private Sub Form_DblClick(Cancel As Integer)

DoCmd.OpenForm "frm01_Services"
Forms.frm01_Services.cmbServer.Value = Me!Server
Call cmdGetServicesData_Click

End Sub

When I try to run the above, I get a run-time error 2465,
application-defined or object defined error. Am i putting the code in the
correct places?

No. You should not have made a new public sub in a standard module. Delete
that sub, and the module if you created one just for that purpose.

Instead, open the form frm01_Services in Design View, open its code module
in the VB Editor, and locate the existing command button's event procedure,
which will have the declaration line,

Private Sub cmdGetServicesData_Click()

Change that line to:

Public Sub cmdGetServicesData_Click()

Save and close the form.

** Note: if the command button doesn't have a VBA event procedure, this
whole procedure is not going to work, and you won't be able to
programmatically "click" the button.

Now go to the code on the form where you wanted to call the procedure, and
change this:
DoCmd.OpenForm "frm01_Services"
Forms.frm01_Services.cmbServer.Value = Me!Server
Call cmdGetServicesData_Click

.... to this:

DoCmd.OpenForm "frm01_Services"
Forms!frm01_Services.cmbServer.Value = Me!Server
Forms!frm01_Services.cmdGetServicesData_Click

Note: you originally had form references in the form,
"Forms.frm01_Services". That's not strictly correct, though it works in
some versions of Access, and I've changed it to "Forms!frm01_Services".

With these changes it ought to work -- if I've understood your setup
correctly.
 

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