Solved ShortCut Menu .OnAction (1 Viewer)

HASAN-1993

Member
Local time
Today, 18:42
Joined
Jan 22, 2021
Messages
89
Except if I made the code .OnAction = unitprice()
It will work but directly when i open the menu
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:42
Joined
Feb 28, 2001
Messages
27,277
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,
 

HASAN-1993

Member
Local time
Today, 18:42
Joined
Jan 22, 2021
Messages
89
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
 

isladogs

MVP / VIP
Local time
Today, 16:42
Joined
Jan 14, 2017
Messages
18,257
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.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:42
Joined
Feb 28, 2001
Messages
27,277
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.
 

HASAN-1993

Member
Local time
Today, 18:42
Joined
Jan 22, 2021
Messages
89
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

  • Database14.accdb
    440 KB · Views: 107

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:42
Joined
Feb 28, 2001
Messages
27,277
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:

isladogs

MVP / VIP
Local time
Today, 16:42
Joined
Jan 14, 2017
Messages
18,257
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
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:42
Joined
Oct 29, 2018
Messages
21,515
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!
 

HASAN-1993

Member
Local time
Today, 18:42
Joined
Jan 22, 2021
Messages
89
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
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 11:42
Joined
May 21, 2018
Messages
8,556
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.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:42
Joined
Oct 29, 2018
Messages
21,515
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!
 

HASAN-1993

Member
Local time
Today, 18:42
Joined
Jan 22, 2021
Messages
89
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
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 11:42
Joined
May 21, 2018
Messages
8,556
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

  • Database14.accdb
    680 KB · Views: 117

isladogs

MVP / VIP
Local time
Today, 16:42
Joined
Jan 14, 2017
Messages
18,257
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
 

HASAN-1993

Member
Local time
Today, 18:42
Joined
Jan 22, 2021
Messages
89
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

Top Bottom