Programatically determine where a specified form

S

Steve

I am working on a very large database that has a main menu, many sub-menus
and a few sub-sub-menus. These menus typically have many command buttons
that open forms and reports in the database. Forms and reports may also be
opened from command buttons on some of the forms.
1. Is there any way to programatically determine where a sub-menu or
sub-sub-menu is opened from? The code would need to reveal the name of the
menu and the name of the command button.

2. Is there any way to programatically determine where a specified form
is opened from? Again, the code would need to reveal the name of the menu
and the name of the command button.

Thanks!

Steve
 
T

Terry

I would pass the name in the OpenArgs of a form being opened and check that
property.

DoCmd.OpenForm "frm_MyForm1", acNormal, , , , acWindowNormal,
"frmCallingFormName"

In the OnOpen event of the called form

Select Case Me.OpenArgs
Case "frm_MyForm1"
' Do something here
Case "frm_MyForm2"
' Do something else here, etc
Else Case
' do something here
End Select

Regards

Terry
 
A

Albert D. Kallal

Steve said:
I am working on a very large database that has a main menu, many sub-menus
and a few sub-sub-menus. These menus typically have many command buttons
that open forms and reports in the database.

I assume you're talking about the built in the menu system? (yes/no??).
Forms and reports may also be
opened from command buttons on some of the forms.
1. Is there any way to programatically determine where a sub-menu or
sub-sub-menu is opened from? The code would need to reveal the name of the
menu and the name of the command button.

I think you've got this backwards? Why do you care what button is calling
the code that's going to be run? Approximately 90% of my custom menus call
vba code in the form that has the ****focus****.

All you need to do is make the code (a function) public, and then simply
place the function name in the buttons on-action setting.

Further, likely often you will have specific code to a particular form, and
once again, you simply declare those functions (in that form) as public.

The syntax to call the code then is:

=YourFunctionName()

Often, (if not most of the time), you code you call will need to pick up
some information about the current screen etc. So, my code most of the time
starts out, and grabs the current screen name. I use:

Public Function AskInvoicePrint()

Dim tblgroupid As Long
Dim frmActive As Form

Set frmActive = Screen.ActiveForm

tblgroupid = frmActive.frmMainClientB.Form!ID

If frmActive.InvoiceNumber = 0 Then
frmActive.InvoiceNumber = nextinvoice
frmActive.Refresh
End If

DoCmd.OpenForm "guiInvoicePrint", , , "id = " & tblgroupid

End Function

The above is code that the invoice print button runs. note how I right away
pick up the active form. After that, I can easily ref the forms object as if
the code was running much like the code would if put behind a button on the
form. In the above example, I also check if a invoice xnumber has been
generated before printing. And, the Refresh forces a disk write if in fact I
do change the invoice number. And, in addition the above clip also passes
the currently selected sub-form item that the invoice print form needs.

Also, if the code you write is for the particular form, then as mentioned,
you can simply place the code into the forms module code. There is no need
to pick up the active screen...and you can use "me.LastName, me.Refresh etc
as you always used.

So keep in mind if the function is public in a standard code module, then
the menu will call that function in that standard code module. However, the
form with the ***current*** focus code module is searched first for the name
of that function (this is perfect behavior as now we whichever form has the
focus will have it's code run. And, if we need/want "global" code for all
forms then place the function routine in a standard code "module and it will
be called for ALL forms (we simply pick up the form name using the above
screenactive example).
2. Is there any way to programatically determine where a specified form
is opened from? Again, the code would need to reveal the name of the menu
and the name of the command button.

Once again I'm confused as to why you need to pick up the menu and command
button on the menu? Just have it call your own vba code in the form (I must
be missing a BIG puzzle here, but perhaps you explain why you need to do
this???)

As to how to determine the previous calling form?

You can simply pickup the name of
the pervious form in the on-open event. In fact, you can grab the previous
form name as late as the on-load event. Hence, the following works well


In the forms code module place:


Option Explicit
dim frmPrevious as form

Then, in the on-open event go:


set frmPrevious = screen.ActiveForm


Now, anywhere in code, you can use fields, properties and even code of the
previous from


msgbox "calling form = " & frmPrevious.Name

(above display the name of the calling form)


frmPrevious!LastName = "bla bla bla"


(above would set the last name field of the previous form to bla bla bla)

frmPrevious.Refresh

(the above would force the previous form to write its current record to
disk, but not change its current record position).

Or, even call custom code or methods from that previous form like:


frmPrevous.MyRefresh


Since I create quite a few custom menu bars, then I don't have to worry
about using open args to "pass" the value/name of the previuos form. In fact
if you make it a coding standard in all your forms to always decleare a
"frmPrevious", then you always have a ready made reference to the calling
form. You can then over time even cut and paste code between different forms
becuase you alwasy declare frmPrevious in each form you build...

Anyway you might want a further explain as to why you need to pick up the
name of the command bar or button in the menu system? You can certainly pass
parameters from the custom menu bar when they call your vba code:

eg, you simply set the menu bar on-action to:

=MyPublicFunctionInAForm("parm1","Parm2")

The above would call public function in a form, and would also pass two
string parameters (so you would have to correctly declare the function to
accept two string parameters)

As I mentioned as a general rule you'll not need to pass parameters because
you simply place the code in the form's code module (this means whichever
form has the focus is the code that's going to get called by the custom menu
bar). As I mentioned for the cases where you have a global routine, simply
place the code in a standard code module, and when the menu system does not
find the "function name in the form's module, it will actually search for it
in a standard code module and run the code in that module. (and as mentioned
you simply pick up the forms name that's active by using screen active as
per the above example).

When I place a button on a form and I click on it to run some code in the
form, I never really care what the name of the button was or is. And, I
don't think you should have anymore of a problem when you use a custom menu
bar in place of buttions on a form....the problem is really teh same and
that code should not care/know what button is calling that code....
 
S

Steve

Thanks for the quick response, Terry!

I have an existing database and I want to make a list of all the forms in
the database and for each form I want to include the name of the menu/form
that contains a button to open the form and include the name of the button.

For example ...
MyForm MyMenu cmd1321

Steve
--
 
S

Steve

Thanks for responding Albert!

The main menu, sub-menus and sub-sub-menus are all forms that have numerous
command buttons on them to open forms and reports in the database. The
database has hundreds of forms and reports. I started this project by
printing a list of the forms and reports in the database. I was asked to go
through all the menu forms and determine if all the buttons worked, if there
were any problems when either the form or report opened and to come up with
a list of the forms and reports that were no longer used in the database. As
I went through each menu, I systematically clicked each button, determined
the name of the form or report the button opened and then cheched off the
name in my list. I'm done with all that. There are many forms and reports
with various problems. The problems need fixed. I know the name of each form
or report with a problem from my list (I marked each with a problem) but I
wasn't thinking. I did not note how to navigate to the form or report. I
need to know which menu form to go to and which button on that menu form
opens each form or report with a problem. Hense my question.

Steve
 
A

Albert D. Kallal

Steve said:
Thanks for responding Albert!

I was asked to go through all the menu forms and determine if all the
buttons worked


Ah, ok...fool me!! I now understand. You not trying to fix a code issue when
code is called from a menu bar button.

You simply want a list of menu bars + buttons + what forms for documentation
reasons (different problem!!!).
Hense my question.

It is a darn good question!

All you have to do is iterate the commandbars collection and print out the
report/form that the button calls.

It not hard to do this. the only thing here is do you want the listing by
menu bars, or simply grouped by reports? (if you send the resulting data to
a table, then you can report grouped by each form....

The basic code will look like:

Sub ListMyBars()

Dim cbar As CommandBar

For Each cbar In CommandBars
Call DisplayControls(cbar)
Next

End Sub

Sub DisplayControls(cbar As CommandBar)

Dim cControl As CommandBarControl

If cbar.BuiltIn = False Then
For Each cControl In cbar.Controls
Debug.Print cControl.Caption & _
" Action = " & cControl.OnAction & _
" form/reprot = " & cControl.Parameter

If cControl.Type = 10 Then
Debug.Print cControl.Caption & "---->"
Call DisplayControls(cControl.CommandBar)
End If
Next
End If

End Sub


The above is recursive and "calls" itself since menu bars can go "many"
levels deep. I would likely add a 4-5 more lines of code to send the above
data to a reocrdset. You thus can then run a report grouped by form/report.
 
S

Steve

Thanks once again Albert!

However my question does not refer to menubars. I have a bunch of forms each
with a bunch of command buttons that open forms and reports. I am
generically referring to these as "menu forms".

Steve
 
K

Keith Wilby

Steve said:
Thanks once again Albert!

However my question does not refer to menubars. I have a bunch of forms
each with a bunch of command buttons that open forms and reports. I am
generically referring to these as "menu forms".

Contact me and I will provide a solution. My fees are very modest.
 

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