Shortcut Menus that apply anywhere...

  • Thread starter Thread starter Bill Mitchell
  • Start date Start date
B

Bill Mitchell

Hi,

I use a slot of shortcut menus throughout my database,
especially on subforms. The menus are based upon code
that specifies location - such as Form![Contact]!
[ContactData].form![ContactPhone], etc.

The problem is that it seems I have to write new code for
every location. Since the "ActiveForm" bit doesnt work
for subforms, is there some way I can write the code once
so it will work wherever it is?

Hope that makes sense. Thanks.

Bill
 
Bill,

I sort of get your drift, but perhaps not fully...

I wonder if generalising using the Parent property might help.
If you track back up the Parent tree, you'll eventually get to the Main
Form.
Odds on that the ActiveControl on the main form is the subform control,
which gives you access to the form name (if you should want it), the subform
control name, and pretty much whatever else.

This behaves itself even if the subform is in a tab control etc etc. Just
keep on getting the Parent until the path runs out.

Hope this is on target. Apologies if not.

CD
 
This is a good question, and there is a good solution!

No doubt you do want to make all menu code pick up the current form, since
hard coding the form name into the code as you mention is a bad idea. By
hard coding the form into your menu code, it means you can't use the same
menu bar for more then one form. Worse, is that you can't copy a form, and
re-name it and expect it to work. You also can't copy the menu code either,
since again hard coding the menu name really hurts.

So, I think we all agree that hard coding forms names in menu code is bad,
and from a maintenance and programmer production view it is near disaster.

For non sub-form menu code, virtually all of my menu code picks up the
active screen.

Thus, menu code will often look like:

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()

The above is code snip from a invoice print option that also requires that a
invoice number be set. Notice how no where in the code do I actually ref a
form name directly.

Also, often when I have a main form, and a sub form that I need to use, I
use the following code:

Dim MyAForm As Form
Dim MySubForm As Form
Dim lngBookingId As Long

Set MyAForm = Screen.ActiveForm
Set MySubForm = MyAForm.tblBgroup_subform.Form

lngTourId = MyAForm!tour_id

If lngTourId = 0 Then
Beep
MsgBox "You must book to a tour before you use this feature",
vbExclamation, "Not booked to a tour"
Exit Function
End If

You can see how in the above again I grab the sub-form. It is now really
easy to get at stuff in the sub-form. I can use:
MySubForm!ID
in place of

forms!TheMainForm!SubFormContorl.Form!ID

However, in your case you are talking about code that gets run from the sub
form. You can use the following:

Dim frm As Form
Dim frmSub As Form

Set frm = Screen.ActiveForm
Set frmSub = frm.Controls(frm.ActiveControl.Name).Form

MsgBox "the current form name is " & frm.Name
MsgBox "the current sub form is " & frmSub.Name

' to refresh the frm, we can go
frm.Refresh

'to refresh onlythe the sub form, we can go
frmSub.Refresh

' or, for any custom function in the form, we can go

frmSub.MyRefresh

frmSub.MyMoveNext ' code run to move to next record in sub form

MsgBox "The current id of the sub form is " & frmSub!ID
MsgBox "The value of last name is " & frm!Lastname

I often make a custom Refresh routine for a form (so things like the
position of the cursor in a sub-form etc is not lost when doing a re-query
for example). So, you can see in the above code example how I had
frmSub.MyRefresh.

Another VERY important issue here is that you can make a function IN THE
FORMS MODULE public. When you do this, then the function in form's code
module is run! This means if you have a public module, and public function
in the both the form and the module WITH THE SAME NAME, then the function in
the FORM IS RUN! This is very important, since now you can USE THE SAME menu
bar for more then one form, but run different code for each form. Thus,
MyRefresh, MyDelete etc can be custom, and specific to each form, but still
use the same menu bar. If you make a standard names for all your forms code,
then you can use the same menu bar code. For example, the on action for
above to delete a record might be:

=MyDelete()

Now, you simply define a public function in each form called =MyDelete().
That code can then be special for each form. For all forms that need a
GENERAL DELETE code routine, you simply place a public function in a
standard code module. If the public function names does NOT exist in the
forms code module, then the "MyDelete" function in the standard module is
run. Thus, special forms run their own MyDlete code, and all general forms
(that don't have a public function called MyDelete) will thus run the delete
code that can be generic. Often, for a lot of just simple forms, the same
delete code can be used. We want to be as general as possible, and only
write code for the different, or special case forms

Note also that you can pass parameters via the menu code also.

So, the real key here is that you should not have been hard coding forms
ref's in your menu code. It will cost you a huge amount of developer time in
the future, and the code is usually not very re-useable (and you can't even
copy the existing code without extensive modifications).
 
Back
Top