Macro not working without VBA Editor open - ms-access

In one of my forms in my Access Database I have a button with a Macro Event that opens another form where two fields match. However, whenever I open my Database this button with the Macro Event doesn't work until I've opened the VBA Editor. After I've opened the VBA Editor and refreshed the form it works just fine.
Also, the Conditional Formatting that I've attached to one of my text boxes on this form (same form as button with Macro Event) doesn't work unless the VBA Editor is open either..
Any thoughts?
Just to clarify, after the VBA editor is closed the code works as well, it seems as if it just needs that initial opening. I have other VBA and Macro events that work without the VBA editor needing to be opened, like I said it’s just that conditional formatting and that specific macro event on my button. When I’m back at work on Monday I’ll provide more details when it comes to the code and etc.
Solved: So the code must have disabled, although there was no yellow bar indicating this was the case. I made a copy of my database to troubleshoot and when I opened the my new copy it asked me to enable code and now everything works fine.

Strange. I suggest you create an AUTOEXEC macro and run a function with something simple like just showing a message box in VBA. With that you can verify if your code is the problem or something else.

Related

Unable to replicate Refresh All quick access toolbar button in form

I have a form with several subforms whose content is edited in separate forms, opened by command buttons.
When the data in those forms is updated, and the extra forms closed, naturally the main form needs to be refreshed before those changes can be seen. Clicking the Refresh All quick access toolbar button achieves this perfectly.
So I made a macro for the "got focus" event on the main form to refresh it, but it does nothing. I tried repaint and requery as well, applying the latter specifically to the subforms in question, and ran them out of vba instead of an access interface built macro too, none of these seem to solve the problem.
vba coding:
Private Sub Form_GotFocus()
Screen.ActiveForm.Requery
Screen.ActiveForm.Repaint
Screen.ActiveForm.Refresh
Me.sfmContact.Requery
Me.frmCustCert.Requery
Me.frmCustReq.Requery
End Sub
Annoying thing is that if I put this (or even just the macro or Screen.ActiveForm.Refresh) in the on click event of one of the controls it works fine. Just can't seem to make it work in any automatic events that don't require thought or clicks from the end user.
This question is sort of a dupe of Refresh button on an Access form but that question is over a year old, and has no accepted answer. So I've expanded on it.
What you mentioned in your comment there is correct:
My guess is that it's not running it, but I don't know why. I got mixed up between OnActivate and OnLoad, which is why I didn't try that option. That works and solves the problem, but why wouldn't GotFocus fire? Surely the main form lost focus when another form got it for updating, and then it got it back again when the other form closed/anything in the main form was selected...
To avoid this type of error in the future, it may be prudent to specifically name the form in your code instead of using the "Me." command, as that only works for current focus. For instance:
Me.sfmContact.Requery > Forms![FormName].sfmContact.Requery
Alternatively, you could just set the focus to the form in question before that sort of coding.
Forms![FormName].SetFocus

To paste a macro from Stack Overflow into Access

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.

Why MS Access pop up form doesn't regain focus after switching apps with Alt-Tab?

I have an Access 2016 application that opens a pop-up form when user double-clicks on a record in a datasheet form. That's meant to be a 'detail form' showing all fields in record (datasheet shows only a few). Now, while editing data on popup form, I switch to another app (say Notepad) to copy some text from there or simply check some other thing. I then switch back to Access app by applying Alt-Tab. However, now focus is over datasheet form, instead of pop up form it was when I first switched to Notepad.
Can anyone please help on understanding why this happens? Ultimately I want to return focus over popup form whenever I switch back from any other app by using Alt-Tab, i.e., maybe by detecting in VBA when Access application gets focus back so I can set focus on desired form. I've read some suggestions about using some Windows API function but no detailed examples on that.
Any help will be greatly appreciated.

ms access button on a form not working

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.

Problems implementing basic VBA in Access 2007

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!