Solved ShortCut Menu .OnAction

Except if I made the code .OnAction = unitprice()
It will work but directly when i open the menu
 
So from the earlier post I suddenly see a hint that the thing you want to call is a routine declared public in the main form. But that can't work correctly because in the sub-form, again that is a DIFFERENT FORM. The code for the sub-form is a DIFFERENT MODULE. And the problem is that unless you have the routine declaration declared public in a GENERAL module, you can't call it from outside its (class) module,
 
Ok, I originally want to call a function from the same subform
But I do all the attempts
The important thing is, I do not want to call the function from the main module
 
So from the earlier post I suddenly see a hint that the thing you want to call is a routine declared public in the main form. But that can't work correctly because in the sub-form, again that is a DIFFERENT FORM. The code for the sub-form is a DIFFERENT MODULE. And the problem is that unless you have the routine declaration declared public in a GENERAL module, you can't call it from outside its (class) module,
Hi Doc
Whilst it is normal to call a public sub/function that is located in a standard module, it is possible to call a public procedure from one form which is saved in another form.

For example, I have a main form with this procedure: Public Sub UpdateLocationMapControls(). It is used both in the main form & its 3 subforms:
From each of the subform I call it using: Call Parent.UpdateLocationMapControls.

I have many other similar examples e.g. close a form using a Public sub from another separate form.
 
Colin, now that you mention it, I recall we had this discussion. Something must have changed regarding scope rules since I started using Access because that used to be illegal (scope issues). Because of the ephemeral nature of a form's class modules (only visible while the form is open), though, it still has to be unwise. Since this is a parent/child case, I guess it might be one of the OK cases, but (as a personal preference) I consider this questionable.

Do you think that it might have something to do with it being a Macro action? That's what the syntax looks like.
 
There has to be. Can you post a demo db, so we can see exactly what you mean?
I have put a Form, if you press it with the right Click, the shortcut menu will appear, I want onAction Call Function Insert On Same Form
 

Attachments

I'm going to assume that you are talking about this kind of thing:



When I try to run your form, it tells me I have an error trying to delete the previous shortcut menu, which is to be expected on the first run of the DB.

According to what I read, this shortcut menu is actually a one-time thing that you shouldn't run with every re-opening of the DB.

EDITED by THE_DOC_MAN to improve clarity of statement.
 
Last edited:
Agree with @The_Doc_Man
If you disable the line Application.CommandBars("customsm").Delete, the rest of your code will then run
The context menu will show Insert Row but clicking on it still won't work due to other issues
 
I have put a Form, if you press it with the right Click, the shortcut menu will appear, I want onAction Call Function Insert On Same Form
Okay, after playing with this for a couple of days and consulting with a CommandBar expert, we have concluded using a Sub or Function in the Form's Class Module does not work. I'm afraid you will have to use a Standard Module for this case. If you're trying to execute specific routines placed in each Form, he suggests you create a function in the Standard Module to call the routine from the Form when the user clicks on the Shortcut Menu.

Good luck!
 
Okay, after playing with this for a couple of days and consulting with a CommandBar expert, we have concluded using a Sub or Function in the Form's Class Module does not work. I'm afraid you will have to use a Standard Module for this case. If you're trying to execute specific routines placed in each Form, he suggests you create a function in the Standard Module to call the routine from the Form when the user clicks on the Shortcut Menu.

Good luck!
Your efforts are appreciated
However, this will not work for me because I am working with Instance Form so the function must be called from the same form
 
However, this will not work for me because I am working with Instance Form so the function must be called from the same form
Could you maintain a tempVar or global variable that is a pointer to the "current" form instance? Then you can put the code in a standard module and operate on the form instance.
 
Your efforts are appreciated
However, this will not work for me because I am working with Instance Form so the function must be called from the same form
Well, that does complicate things a bit more. Please let us know if you do find a solution. Cheers!
 
Could you maintain a tempVar or global variable that is a pointer to the "current" form instance? Then you can put the code in a standard module and operate on the form instance.
great idea
But how do I call the Instance Form
 
Code:
Public Sub Insert()
  Dim frm As Form
  Set frm = Screen.ActiveForm
  MsgBox "Insert " & frm.Hwnd
  frm.Detail.BackColor = vbGreen
End Sub

I open three forms from form2. Make sure to move them. This sets the current form to green.
 

Attachments

Very neat!
Incidentally, if you change this line to frm.Detail.BackColor = frm.Hwnd instead of vbGreen, each form instance will be a different colour each time you run it

1616436772628.png
 
Code:
Public Sub Insert()
  Dim frm As Form
  Set frm = Screen.ActiveForm
  MsgBox "Insert " & frm.Hwnd
  frm.Detail.BackColor = vbGreen
End Sub

I open three forms from form2. Make sure to move them. This sets the current form to green.
Thank you
More than great idea and the problem was solved
Thank you very much
 

Users who are viewing this thread

Back
Top Bottom