Generally, VBA (not to be conflated with VB, VB.Net, or VBS but the MS Office application code) is often considered a trigger language in that events fire code.
In MS Access, these events occur mostly on forms and reports often due to user interaction. You do not need to jump directly into the VBA Editor to write neeeded code but locate the event (AfterUpdate
, BeforeInsert
, OnClick
, OnCurrent
, OnOpen
, OnClose
) needed in Property Sheet under Event tab and then enable code which will auto write the subroutine shell for you to then add your .Requery
line. In fact, simpler lines like .Requery
can even be handled with macros (not to be confused with Excel macros). Access methods do not run outside of a subroutine or function such as your attempt.
Specifically, you mention:
so that [combobox] updates every time A) Any record is updated in the table B) A new record is created
This is not quite clear on the timing and location of needed requery call. Therefore, you need to answer:
When does this table B update or insert? By user, by automated code, or by outside event? This identifies the type of trigger event (i.e., AfterUpdate
vs OnClick
).
Where does this table B update or insert? On the very form you use or a different form? Is the very form bound to table B? This identifies the location of trigger (i.e., Form A's AfterUpdate
not Form B's AfterUpdate
).
- If trigger is run outside of the form where combobox resides, you need to change the relative reference (
myCombboxName.Requery
or Me.myCombboxName.Requery
) to absolute reference: Forms!myFormName!myCombboxName.Requery
.
Once trigger type and location is identified, you can then place your cursor under Property Sheet in the corresponding event of corresponding form and then enable code such as one of the below subroutines:
Private Sub Form_AfterUpdate()
Forms!myFormName!myCombboxName.Requery
End Sub
Private Sub Form_AfterInsert()
Forms!myFormName!myCombboxName.Requery
End Sub
Private Sub myButton_OnClick()
Forms!myFormName!myCombboxName.Requery
End Sub
Forms(FormName).Controls("ContainerCombo").Requery
– marlan