Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Command Button Error for Event Procedure

Status
Not open for further replies.

ActMod

Technical User
Aug 25, 2003
45
US

I have two forms (call them frm1 and frm2) in an Access Project. On frm1, I placed a command button (cmd1) with the "On Click" property set to "Event Procedure". When I left click cmd1, I go to the Event Procedure called "Private Sub cmd1_Click". When I left click cmd2, I get an error message saying "Sub or Function Not Defined".

As best I can tell I did the exact same thing in developing cmd1 and cmd2 but I can't get cmd2 to work. In fact, if I put a cmd2 type button on frm1, it works so I must have some problem with frm2. Why would a command button work on one form but not another?

Thank you for any help.

Jim
 
So your buttons are on seperate forms? I.E.

Form 1 contains Button 1
Form 2 contains Button 2

If this is the case it just sounds like you haven't written the Sub or Function that Button 2 is trying to call. If button 2 is calling Private Sub cmd1_Click when it's on Form 2 then it will look inside the code specifically for Form 2 for a Sub or Function called Private Sub cmd1_Click.

So when you move Button 2 to Form 1 it will look for the same code again but this time in Form 1's code collection, and since it sounds like you've written it in Form 1 it will find it and work.

If this is the case then the problem is that Private Sub cmd1_click is defined as Private so will only let stuff in Form 1 call it. If you have a piece of code you want to run from two different forms then I'd recommend putting it in a module and having Private Sub cmd1_click call that.

Then all you have to do is write Private Sub whatever_Click in form 2 for button 2 to call when it is clicked.

So to summarise(because I've just confused myself!)

1.You should take the code in Private Sub cmd1_Click and copy it to a new Public sub in a module.

2.Change Private Sub cmd1_Click to call the Sub you've just created in that module.

3.Make sure that Button2 is calling code that exists, if not, create the code and all it needs to do is call the same Sub in the module as Private Sub cmd1_Click is calling.

Bleurgh, sorry for the long windedness! And sorry if I've totally missed the problem as well!

Cheers,

Pete
 
Thanks much for the detailed response.

cmd1 on Form1 has its own Private Sub cmd1_Click that is assigned to Form1.

Similarly cmd2 on Form2 has its own Private Sub cmd2_Click that is assigned to Form2. It looks to me that cmd1 and cmd2 are organized identically, yet cmd1 works and cmd2 does not.

Does this make any sense to you (both my question and Access' apparent inconsistency)?

I am new to Access, and it's driving me crazy.

Thanks again for your help.

Jim
 
Well the error message "Sub or Function Not Defined" means that Access can't find the code that the button's properties say it wants to run when it's clicked.

I'd say it's less to do with the code on each button, which by the sounds of it are identical, and more to do with the On Click property not calling the correct code.

Have a look at the properties of the button cmd2 and make sure that the "Sub or Function" that is assigned to the On Click property has got the same name as the Sub that has been written.

If that doesn't work, the only other thing I could think is that you've got an "End Sub" or "End Function" missing off the end of your procedure that cmd2 calls.

Hope that helps,

Pete
 
I'm not sure about the inconsistency of Access based on your explanation.

What I would do is open form 2 in in design mode and bring up the properties for the page for the command button. On the events tab, make sure that Click event is set to [Event Procedure] and then click to the right of the text box, and see where that takes you. If the procedure is already defined, it will take you to the code. If not, it will create the proper sub definition for that control event.

It's also quite possible that you're trying to call a function/sub from within the event handler which is not defined. This can be easy to track down by setting a break point on the first executable line within the event handler. Then stop through the code, line by line, until you find the specific line where the error occurs.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top