Call Command (1 Viewer)

Dave E

Registered User.
Local time
Yesterday, 18:39
Joined
Sep 23, 2019
Messages
104
Hi All,

Access 2010

My problem is - In VBA on my main form, I use the line - Call Me.Command117_Click, to execute the action of the Command117 button. It works fine.

On a new form - In VBA, I've used the same method with - Call Me.Command248_Click, to execute the action of the Command248 button. It fails with two different errors-

If I use Call Me.Command177_Click, I get the error message - Method or data member not found
If I use Forms!SearchBuild.commandbutton_248, I get - Application-defined or object-defined error
Both lines are accepted by the editor as correct. The error highlights the Calls not the subsequent action.

Now, both the forms are self-contained and the relevant Command buttons are on their forms and just call an action from another object.
In the working form, the call is made from VBA when a button is pressed.
In the non-working form, the call is made on an AfterUpdate.

Why would the action work on one form and not the other with similar code?

DaveE
 

Minty

AWF VIP
Local time
Today, 02:39
Joined
Jul 26, 2013
Messages
10,371
A number of things stick out. One is please call your controls something meaningful.

Secondly, if you need to reuse code in a form put it in a separate sub or function then simply call that when you need it, from wherever it's needed.

So instead of
Code:
Private Sub Command248_Click
    Do Stuff
    Do More Stuff
    Do final stuff
exit sub

You should have

Code:
Private Sub MyRoutine()

    Do Stuff
    Do More Stuff
    Do final stuff

Exit sub
And then wherever you want to use it
Code:
Private Sub cmdSensibleName_Click
   MyRoutine
Exit sub

This makes your code much easier to follow and debug.
 

Dave E

Registered User.
Local time
Yesterday, 18:39
Joined
Sep 23, 2019
Messages
104
Thank you for your input.
Can you offer any help with the problem?

Thanks

DaveE
 

plog

Banishment Pending
Local time
Yesterday, 20:39
Joined
May 11, 2011
Messages
11,653
Can you post a sample of you database? Lots of ambigous and conflicting info in your post. Is the non working form OnClick or AfterUpdate? Did you copy the code from the working form to the non working form, or just making a call from the non working form to the working one?

Posting your database will clear it up.
 

Dave E

Registered User.
Local time
Yesterday, 18:39
Joined
Sep 23, 2019
Messages
104
Thanks for your questions,
Posting the database would be a little OTT for one line of code since the database is quite large.

In each instance, the working form and the non-working form, there is one line of code that is a problem.

The first form that works has the line, as described above, and the Call is made directly by a button press.

The second form, that doesn't work, has the same syntax line as described above' and the call is made from an AfterUpdate action on a Combo. on the same form.

All of the actions remain on their respective forms.

Since the code in question is only one line, I wrote them out in full. I compacted and repaired, and compiled the database without error.

Thanks

DaveE
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:39
Joined
Sep 21, 2011
Messages
14,336
Is there even a click event for 248? :(
 

Dave E

Registered User.
Local time
Yesterday, 18:39
Joined
Sep 23, 2019
Messages
104
Command248 is a button so, yes, it has a Click event.
 

MarkK

bit cruncher
Local time
Yesterday, 18:39
Joined
Mar 17, 2004
Messages
8,186
If you need help with code, post the code.
 

Dave E

Registered User.
Local time
Yesterday, 18:39
Joined
Sep 23, 2019
Messages
104
The code I'm having a problem with is already posted above. There is one line...

Thanks

DaveE
 

moke123

AWF VIP
Local time
Yesterday, 21:39
Joined
Jan 11, 2013
Messages
3,926
Posting the database would be a little OTT for one line of code since the database is quite large.
We can't see your code so we can't guess why it's not working. It should work as you describe it so there has to be something wrong.

Why not copy and paste the relevant code (with code tags so Gasman doesn't get pissed ;)) Maybe then we can spot it.
We need both the button click and after update code sections.
 

Dave E

Registered User.
Local time
Yesterday, 18:39
Joined
Sep 23, 2019
Messages
104
You can have a button with no events whatsoever?
Would there be a use for a button with no Click event? Or no events at all?
Do you have Option Explicit declared in each of the modules?
Yes, but this line of code is restricted to its form. A similar line works OK with the main form, as above. All of the code is contained within the forms.
 

plog

Banishment Pending
Local time
Yesterday, 20:39
Joined
May 11, 2011
Messages
11,653
The code I'm having a problem with is already posted above. There is one line...
...
Would there be a use for a button with no Click event? Or no events at all?


It seems we want to help you more than you want to help us help you. Not me any longer.
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 21:39
Joined
May 21, 2018
Messages
8,547
Assume in Form 1 I have a button with a click event
Code:
Public Sub cmd1_Click()
  'Must be public if called from outside of form
  MsgBox "Hello World in Event Procedure"
End Sub
In order to call this from an external form or code I must make the procedure PUBLIC. It will be private by default.

However, the better way to share a procedure with other events or code is to pull the code out and put it in its own procedure

Code:
Private Sub cmd2_Click()
  Call HelloWorld()
End Sub
Public Sub HelloWorld()
MsgBox "Hello World In its own Procedure"
End Sub

Now from an external form or code I can call either of these in the same manner. From Form 2
Code:
Private Sub cmd3_Click()
  Forms![form 1].cmd1_Click
  Forms![form 1].HelloWorld
End Sub

Call is unnecessary and cannot be used in all cases. When using call you must include ().
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 21:39
Joined
May 21, 2018
Messages
8,547
Why would the action work on one form and not the other with similar code?
Because your syntax has a lot of problems.
1. To call a procedure from outside the form the procedure has to be PUBLIC
If I use Call Me.Command177_Click, I get the error message - Method or data member not found
2. That is wrong because Me is used to refer to the current Form (or report, or class) from where you are calling the code. If your are making the call from Form2 then ME refers to Form2 and Command177_Click is in Form1 and thus data member not found.
but
Forms!Form1.Command177_Click
should would work assuming the event is Public in Form1

3. I guess I need to mention that the form you are calling has to be Open for this to work

If I use Forms!SearchBuild.commandbutton_248, I get - Application-defined or object-defined erro
4. That does not make sense as written. But if you meant
Forms!SearchBuild.CommandButton248_Click
then it should work

5. Here is an example where call does not work.
This will not even compile
Code:
Call Forms![form 1].cmd1_Click
But this works
Code:
Call Forms("form 1").cmd1_Click
And this works
Code:
Forms![form 1].cmd1_Click
 

Dave E

Registered User.
Local time
Yesterday, 18:39
Joined
Sep 23, 2019
Messages
104
Assume in Form 1 I have a button with a click event
Code:
Public Sub cmd1_Click()
  'Must be public if called from outside of form
  MsgBox "Hello World in Event Procedure"
End Sub
In order to call this from an external form or code I must make the procedure PUBLIC. It will be private by default.

However, the better way to share a procedure with other events or code is to pull the code out and put it in its own procedure

Code:
Private Sub cmd2_Click()
  Call HelloWorld()
End Sub
Public Sub HelloWorld()
MsgBox "Hello World In its own Procedure"
End Sub

Now from an external form or code I can call either of these in the same manner. From Form 2
Code:
Private Sub cmd3_Click()
  Forms![form 1].cmd1_Click
  Forms![form 1].HelloWorld
End Sub

Call is unnecessary and cannot be used in all cases. When using call you must include ().
The puzzling thing is that the Call works on one form within it's own form limits and successfully calls the button as if it was clicked.

The other form, which again is self-contained and works entirely within its form, uses the same call but fails with the messages in my first post.
I have used the alternative forms!formname.buttonname but that only gets me a new error message as shown in my first post.
No othe code is involved and the error relates to the Call.
I was wondering if anyone had had this problem.

It seems not so I will persevere, on a purely academic level, to solve the problem while relocating the code from the button to a local Function.

Thanks for instruction but it isn't relevant to my problem. My little Garden Database will progress, but I do like to get to the bottom of anomalies when they occur.

DaveE
 

Dave E

Registered User.
Local time
Yesterday, 18:39
Joined
Sep 23, 2019
Messages
104
Because your syntax has a lot of problems.
1. To call a procedure from outside the form the procedure has to be PUBLIC

2. That is wrong because Me is used to refer to the current Form (or report, or class) from where you are calling the code. If your are making the call from Form2 then ME refers to Form2 and Command177_Click is in Form1 and thus data member not found.
but
Forms!Form1.Command177_Click
should would work assuming the event is Public in Form1

3. I guess I need to mention that the form you are calling has to be Open for this to work


4. That does not make sense as written. But if you meant
Forms!SearchBuild.CommandButton248_Click
then it should work

5. Here is an example where call does not work.
This will not even compile
Code:
Call Forms![form 1].cmd1_Click
But this works
Code:
Call Forms("form 1").cmd1_Click
And this works
Code:
Forms![form 1].cmd1_Click
Yes, I omitted the Commandbutton number but the syntax was correct and, no, it didn't work. I used several ways as suggested by other online sources without success.
The code, although it won't work, does compile without error.
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 21:39
Joined
Feb 19, 2002
Messages
43,328
It is not possible to actually debug this type of problem without having the database to test with. THEREFORE, you were given instructions on how to accomplish your goal using correct methods rather than incorrect methods which may or may not work reliably. Instead, you complain and accuse the experts of playing "irrelevant". You probably forgot that no one here gets paid to help you.
 

Users who are viewing this thread

Top Bottom