I have a custom Tab I have created. How can I use VBA to change the attributes of the controls on the tab. For example, I would like to change the label of a button with id="menubutton" depending on user seelctions in the program. It seems to me that it should be easy but I am struggling with this. Any help would be greatly appreciated.
You can try getLabel function with XML you can change your button label like this
<button id="customButton" getLabel="getLabel1" size="large" onAction="Example_Macro" />
With VBA you can use this macro to change label
Sub getLabel1(control As IRibbonControl, ByRef returnedVal)
returnedVal = "Hello"
End Sub
returnedVal will be your label for button.
Can you do this yourself (eg with the mouse and keyboard) in Word via the customise toolbars context menus?
If so then switch on the macro recorder.
Perform the action (eg use the keyboard and mouse to change the attributes you wish to ).
Stop the macro recorder.
Open up the VBA studio in Word and take a look at the macro that has been created, that should give you a good indication of what you need to do.
Related
My question seems to be very silly, but I haven't found solution myself.
Is there a way to simply paste a macro (example) from Stack Overflow in Access? Or, alternatively, paste it from Access to Stack Overflow?
I know, it is very easy in Word and Excel. But I can't understand how to do it in Access.
Basic questions certainly deserve asking, if they're not duplicates.
There are a couple of different cases:
Add VBA on a form event (this case)
Open up the form in design view
Identify the control you want to add the macro to (the part before the _ is the control name, the part after the _ is the event name, so the KeyDown event on the form control.
Identify the event you want to add code to on the event pane
Press the builder ... thing
Choose Code Builder and press OK.
You will see some event code has already been added. Replace that with the copy-pasted code.
Add VBA (function or sub) outside of a form (triggered elsewhere, such as a user-defined function for use in queries)
Open up the VBA editor by hitting Alt + F11
Choose Insert -> Module in the top bar
Paste the code
Paste a normal, non-VBA macro using AXL
Create a macro on the create tab on the ribbon
Select the body of the macro by clicking the blank space inside it (not the area where you can create new controls)
Hit Ctrl + V to paste
Paste an Access data macro using AXL
Open the table you want to add the data macro to in design view.
In the ribbon, click Create data macros, choose and click the right event. You can identify the event by looking at the event property of the datamacro tag, for example <DataMacro Event="AfterInsert"> = On After Insert
Select the body of the macro by clicking the blank space inside it (not the area where you can create new controls)
Hit Ctrl + V to paste
Note that questions about non-VBA macros rarely contain the AXL necessary to paste it. Pasting AXL is somewhat contra-intuitive, as you don't paste it in a textbox and the result isn't text.
I have a form in Access like this:
When I click on "Order" button, I want to order the "client" table (shown in the bottom) order by "firstname". How I can do? Can I use macro or I must use VBA code? So far I have only used macro, I never used VBA.
You can use VBA in the OnClick Event.
Click on your button while in Design View. Then go over to the properties and go into the Event tab, click in the text box for the On Click Event. Youll see a button with 3 periods appear. Click that and select the Code Builder.
It will bring up a Sub like the below. Just put that line of code in there and your done.
Private Sub OrderButton_Click()
DoCmd.SetOrderBy "[FieldYouWantToSortBy]" DESC, ""
End Sub
Done. I used a macro with "SetOrderBy".
I have a database with many forms, when I added a button with an onClick event to one of them it just doesn't work. The onClick event code is very simple:
Private Sub button_Click()
DoCmd.Close
End Sub
Doesn't matter how I click it, the event is not firing. I put a break point in front of the sub, and realized that when I clicked the button the event is not even firing. I create buttons in other forms they work fine, its just this one specific form that doesn't work.
I did some testing, and realized that when I make the form into a split form, the button stops working.
Any Idea why?
Thanks
Nvm..it seems like there has to be at least one field in the split form for the button to work. Did not know that before
Then maybe the function is not linked to the button.
did you checked that in the button properties "on click" has the vba function linked?
the buttom name and the function im vba are the same? "Button"?
The Command Button Wizard create a command button that performs a specific task. In an Access (.accdb) file, the wizard creates a macro that is embedded in the OnClick property of the command button. In an .mdb or .adp file, the wizard creates VBA code, because embedded macros are not available in those file formats.
This might not be quite relevant to your question but might help someone else in the future where an event is not firing....I had a similar issue in an Excel form where an event was supposed to happen when I click a button...I deleted the button and added it again and it worked...could be a bug or a file corruption...try deleting then adding the control again.
I have two sub forms embedded in control (the one that looks like a set of folders allowing you to change from tab to tab.. Sorry I don't know the name in English)..
Both sub forms are embedded on one sheet, I would like that immediately after executing a routine, I wrote on one of the subforms, that the set focus then goes to the other sub form.
For some strange reason I can't get it to work. The sub form and field I'm trying to go to are frmObjectives03 and Effective_date.
I tried:
Forms!frmObjectives03!Effective_date.Form.SetFocus
but it doesn't work.
I would appreciate any insight.
Thank you,
The control you speak of is a Tab control.
Because frmOjective03 is opened as a subform, it is not part of the Forms collection. Instead you can use Forms!MainForm!frmObjectives03.Form!Effective_date.SetFocus from anywhere, or Me!frmObjectives03.Form!Effective_date.SetFocus from within the main form or Me.Parent.Form!frmObjectives03.Form!Effective_date.SetFocus from the sibling subform.
If these are not working for you, please check the name of the subform control in your main form (because it may be Child17 instead of frmOjective03). Open the main form in design view, click on the subform ONCE and look in the Properties->Other->name.
I'm trying to write some code for my database to enable a dropdown box in a parameter prompt for a query, but it's doing nothing instead of it's intended purpose. This:
Dropdown in Access 2007 parameter query
has been the source of my inspiration. Though I seem to be unable to implement even the most basic VBA code:
Private Sub cmdReset_Click()
Me.cboSelectName = Null
End Sub
(Yes this is not all my code! Just one of the subs)
On the relevant form, I have a button called "cmdReset", which has "[Event Procedure]" for the event "On-Click". I also have a dropdown box called "cboSelectName" on said form. Also I have tried closing the database, and making sure to enable macros when it starts.
So essentially this code should make the value in the dropdown box null when I click the reset button. However it does nothing, it simply deselects the dropdown box. Can anyone help me with this one? I'm keen to start implementing some VBA in my database!
As suggested by Remou, the code wasn't even running. I figured out my problem - I had saved the code in a new module, rather than in the VBA code for my form. Once I shifted the code into the form object, the reset button worked. I've also now got some nifty code working with the actual "generate report" command. VBA really has the power to take your database to a whole new level!