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!
Related
I have cmbCountry on a form as an unbound dropdown. The dropdown works as expected. I have setup a textbox called txtCM_ID on the same form, in which I want to display the ID that was selected in the dropdown.
When I enter the record source for the textbox as =Forms![frm_ClientModel]!cmbCountry.Column(0) access automatically changes it to read =[Forms]![frm_ClientModel]![cmbCountry].[column](0)
My version returns the correct information when i test it in the VBE Immediate window. The code that access produces returns the below when run in the Immediate window:
runtime error 450: Wrong number of arguments or invalid property
assignment
The frustrating thing is, that regardless of where or how i enter the code in the control source, access changes it to its version. When the form is opened the txtCM_ID simply remains blank.
I have also tried to go around this problem by changing my reference to the following: Forms("frm_ClientModel").Controls("cmbCountry").column(0)
While this version (also tested and ok in VBE) doesn't produce an error, it too returns nothing in the text box.
What am I missing / doing wrong / overlooking?
Use just
=[cmbCountry]
To access the value of cmbCountry from other control.
I was not able to specifically determine why this happened, but ended up solving the issue by using the OnClick Event of the Dropdown and writing the needed info into the Textbox via VBA with this:
Private Sub cmbCountry_AfterUpdate()
Me.txt_CMID = Forms("frm_ClientModel").Controls("cmbCountry").Column(0)
End Sub
If anyone else finds an answer as to why form controls did not work properly I would be interested in hearing from you.
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.
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
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.
EDIT: For clarification, I'm talking in the below post about tabbed document browsing, not a Tab Control. However, if you're looking for roughly the same problem but regarding a Tab Control, Gord Thompson's answer is correct. Two answers for the price of one!
I've got an Access 2007 database that uses tabbed documents. I need to run some VBA code every time a user selects the form called "Reports", either via opening it or clicking on its tab if it's already open.
I could achieve much the same thing by closing it each time it's used and running the code on an OnLoad event, but ideally I'd like to keep it open so that users can keep the settings of the various drop down boxes, radio boxes etc that they've already set on "Reports".
I was hoping for an event that could run code on tab reselection, but neither of my guesses (OnCurrent and GotFocus) seem to work (OnCurrent works only when the form is opened, like OnLoad would).
Any ideas greatly appreciated - can't find what I'm looking for on Google, though I suspect that's because I don't know exactly what I'm looking for.
The .Value property of a TabControl returns the index (zero-based) of the current page. So, if I have a TabControl named TabCtl14 that contains two pages, FirstPage and SecondPage, then the code...
Private Sub TabCtl14_Click()
If Me.TabCtl14.Value = 1 Then
MsgBox "SecondPage was clicked."
End If
End Sub
...displays the MsgBox whenever I click the "SecondPage" tab in the TabControl.
Have found the answer I was looking for. It's the OnActivate event.