Where is the Form/Report `Menu` event (that relates to OnMenu) defined in Access? - ms-access

Access Forms and Reports support an OnMenu property, that follows the naming convention and behavior of other Form/Report event properties like OnLoad (where assigning the OnLoad property a value of [Event Procedure] will cause the Load event to be handled by the code-behind with signature Private Sub Report_Load()).
However, while the Load event and its OnLoad property are easily identifiable in the Object Browser and in the Form/Report properties dialog, the same is not the case with OnMenu. The OnMenu property is in Object Browser, but it does not appear to be is in the Properties dialog as "Menu Bar", and the Menu event is nowhere to be seen. But if I assign the name of a public function to the OnMenu property, then the function is called when I open the report:
Report_Report1.OnMenu = "=MyOnMenuCallback()"
But I also see there is a MenuBar property, which seems to have the same value that I assigned to OnMenu....
EDIT: There also appears to be a ShortcutMenuBar property which fires an event, but the event is not visible in Object Browser, and there doesn't seem to be an OnShortcutMenu property?
Where is this Menu event defined? I can't find anything in the Object Browser other than the OnMenu property. Is it a real event that I can write an event handler for (if I assign the OnMenu property a value of [Event Procedure]?

The OnMenu property is essentially deprecated in Access 2.0 and higher and is shown in the Object Browser for backwards compatibility with Access 1.x only. It was never an Event, it is a property. In Microsoft Access 1.x, you created special Macros to define your custom drop down menus and menu items and then set the OnMenu property to that Macro name. In Access 2.0 through Access 2003, you would either create the same kind of custom menu Macros, or create menubars or toolbars using the GUI and assign them to the MenuBar property. In Access 2007 and above, the proper method of providing custom menus or toolbars in your application is to create custom Ribbons using Ribbon XML and putting in the USysRibbons table.
The online documentation for many version of Access are misleading and incorrect for the OnMenu entry. For example, looking at this MSDN entry, it would lead you to believe that OnMenu was a Form or Report event, but it isn't, and the one piece of information that is missing in that documentation is "how" the Event is triggered, because it isn't an event. Click on the links there for some of the actual Form events, and you will notice they are almost the same kind of entry, but they include the crucial statement:
The XXX event occures when ...
The documentation for OnMenu is simply wrong (sadly, as is the case with too many bits of Microsoft Documentation).
Think of OnMenu as being renamed to MenuBar in Access 2.0 and higher, which is why if you set it in code, it appears in the MenuBar entry in the GUI properties dialog.

Related

MS Access - Change appearance of form when using "Filter by Form"

I have created a front and back-end database MS Access solution (I am limited to MS Access 2013) for my team, utilizing forms for data entry, lookup, and editing. It is working well, but every now and then a user will forget they are in "Filter by form", and enter data into the filter instead of the actual form. When they realize their mistake, they have to re-enter all of the data.
Is it possible to change the appearance of the form when you are in "Filter by Form"? For instance, change the background color, or add a text box notification so that it is very obvious to the user that they are in "Filter by Form"?
You could use the Me.FilterOn property to help determine whether the form filter (ie., Home→Advanced→Filter By Form ) is on.
You might need to play with it a bit but from my experiment, you could have something in the OnOpen event that checks the property and acts appropriately.
I believe the catch is that code won't execute when it's in Filter mode, so therefore, you'd have to make the default appearance the "Filter appearance" (perhaps add a Graphic or Textbox that says "Filter Mode"), and then if Me.FilterOn = False you can hide that label in the OnOpen event.
Also:
The Apply Filter button indicates the state of the Filter and FilterOn properties. The button remains disabled until there is a filter to apply. If an existing filter is currently applied, the Apply Filter button appears pressed in.
To apply a filter automatically when a form or report is opened, specify in the OnOpen event property setting of the form either a macro that uses the ApplyFilter action or an event procedure that uses the ApplyFilter method of the DoCmd object.
(Source)
More Information:
Office.com : Apply a filter to view select records (Form Filter)
expertsexchange : Determine if filter is applied
MSDN : Form.FilterOn Property (Access)

Why does ddl 'onchange' event not appear in intellisense?

I was working out a problem with a ddl trying to get a message box to popup when the item changed. You can read about that here >>>
How to Popup Alert() from asp:DropDownList OnSelectedIndexChanged?
The working answer shows me to use the onchange event but then I'm working in VS2010 this event does not appear in the intellisense dropdown. However if I type it in anyway it works fine.
For this, you need to understand how the thing works....when you change the value of a input element, onchange event gets triggered on the browser, so the browser looks for a way to handle it. So when you put the onchange event specified for the element it gets called.
Now, ASP.NET OnSelectedIndexChanged uses the same functionality(logically saying) to POST the page to the server. From there, the ASP.NET runtime triggers the function you wrote in the codebehind file and returns you the result. Now, if you really don't require any operation that can only happen on the server, you don't need to use the server functionality, instead you can do it in javascript.
On the other hand, if you want something that happens on server: like some database get, you are supposed to use the OnSelectedIndexChanged event.
And if you use the OnSelectedIndexChanged event, you can still call some javascript functions from there.
Page.ClientScript.RegisterClientScriptBlock(typeof(string),"myScript","alert('HI')",true);
To answer your question about intellisense, onchange is a event of input types, and in aspx pages, i guess you are using <asp:..> tags, which does not have the same event - thus visual studio does not show it in the intellisense. But when you put it, it gets assigned to the HTML markups, which is interpreted correctly by the browser.
PROS and CONS
onchange works on your browser, so it is lot faster than the server-side code. On the other hand, we had an issue once that the browsers has the capability to restrict pop-ups. So if you want some really important message to be shown, it is better to use the Server-Side event and the RegisterClientScriptBlock function.
Hope it helps.

How do I declare a reference to an ActiveX "ListView" Control in Access VBA?

I'm using Access 2003.
In "References" (Tools > References > Browse...), I've added in "Microsoft Windows Common Controls 6.0 (SP6)" (c:\windows\system32\mscomctl.ocx) and I've created/inserted an instance of the control "Microsoft ListView Control 6.0 (SP6)" on a Form and given the control the Name "MyListView".
I wanted to decorate MyListView with some custom methods so I've created a class ("DecoratedListView") which contains a member field ("lvw").
I want 'lvw' to point/reference MyListView, but I don't know what reference type to use in its declaration. Importantly, I also want to capture lvw's ColumnClick event.
I've tried:
Public WithEvents lvw As Object
Public WithEvents lvw As Control
Public WithEvents lvw As MSComctlLib.ListView.2
and none works when I
set lvw = MyForm.MyListView
Can anyone explain how I should create the reference (lvw) to the existing object (MyListView)?
Access can't really handle that sort of thing.
The OnClick should be available on your MyForm though, but you can't see it in the Event tab in properties. ActiveX Controls are too complicated for that. Instead, go into the code and choose MyListView in the combobox on the top left. You'll then find the extended ActiveX events in the combobox on the top right. One of them is ColumnClick.
Not sure what your full intentions are, but if you want to sort of mimic an isolated DecoratedListView class then create a special form that only includes a ListView control and use it as a subform for other forms. You can refer to the ListView from the parent Form as you would any other subform control object. Parent specific code can be run from the listview subform by checking the me.parent.name object which is basically the parent form. You can even call subform's listview events by changing the default Private Sub to Public Sub.
I can't comment yet, so I'll type it as an answer.
Did you look in View, Object Browser to see what methods and properties are available for that object? You should be able to write what you want using that information, assuming what you want is possible.

VBA: Why must I set focus to control every time?

I am creating a personal Library Inventory system using an Access 2007 database. In code, whenever I reference the .Text property of a form control, whether it be changing the value, or simply checking the value in an IF statement, I get prompted with Run-time error '2185': You can't reference a property or method for a control unless the control has the focus.
Why is this?
For setting the .Text it's not a huge deal, but when I'm checking the value in an IF statement, I can't set the focus when I'm checking multiple conditions.
Use .Value instead - that doesn't require setting focus first. From the documentation, for example for the TextBox control (emphasis mine):
While the control has the focus, the Text property contains the text
data currently in the control; the Value property contains the last
saved data for the control. When you move the focus to another
control, the control's data is updated, and the Value property is set
to this new value. The Text property setting is then unavailable until
the control gets the focus again.

How to SetFocus on a TextBox in the Form Load

Working in both A2003 & A2007.
How do we ensure that a selected TextBox gets the focus when the form loads? If we put MyTextBox.SetFocus in the Form_Load then we get the error:
can't move the focus to the control
This form is designed for rapid data entry, and the form somewhat rearranges itself based on the last used settings. So there are several different textboxes any of which may need the focus depending on the user. We can't just fix it in design time by giving MyTextBox TabIndex=0.
The help says something about calling Repaint which just doesn't make any sense at all:
You can move the focus only to a
visible control or form. A form and
controls on a form aren't visible
until the form's Load event has
finished. Therefore, if you use the
SetFocus method in a form's Load event
to move the focus to that form, you
must use the Repaint method before the
SetFocus method.
The best bet in this case, is to ensure that the textbox to get focus is numbered 0 in the Tab Index property.
You cant set the focus as the controls don’t really exist yet, try putting the code in the OnActivate event instead
Or just put a DoCmd.Repaint in the OnLoad event before trying to set the focus. Both should work but I'm not near a computer to check
In my experience, I've always gotten that error when the control I was trying to set focus to was either 1)not visible or 2)not enabled. I assume you've already checked those, but it would be worth double checking at runtime when you get the error message (especially since you said you are shuffling the controls at runtime).
I use the .SetFocus method pretty regularly without trouble. I don't recall ever getting an error message when setting focus to a control that already has it as Remou stated in his answer.
I believe there is also a third case that occurs if you try to set focus to a control in the form header/footer of a bound form that has had all of its records filtered out. I know that situation causes "disappearing" contents in an unbound combo box, but I think it may also play havoc with the SetFocus method. If you are opening the form in Data Entry mode, though, that should not be an issue.
Move SetFocus to the form's On Current event. Should work then unless perhaps the form's record source contains no records and you've set the form's Allow Additions property to No. In that case your text box will not be available to SetFocus on, but in my testing it doesn't throw an error.