Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS ACCESS---Refresh all via VBA

How do I refresh all via vba? I want to refresh all open forms...

like image 405
Christopher Robinson Avatar asked Feb 07 '11 20:02

Christopher Robinson


People also ask

How do you refresh all in access?

To refresh the records in Datasheet or Form view, on the Home tab, in the Records group, click Refresh All, and then click Refresh.

How do you refresh a query in Access VBA?

At this point you could call this function from any other VBA code in your project. To open/refresh a query from a macro as the OP wanted to do, create a new macro and add a RunCode action with open_or_refresh_query("my_query") in the Function Name field, changing my_query to the name of your query.

What is the difference between refresh and requery in access?

Requery pulls all new data from the underlying table or query, including new additions and deletions; Me. Refresh only updates the records in the current form, but will not show new or deleted records.


2 Answers

The reason @CodeSlave's answer probably didn't do what you needed, is that the VBA method you need is requery, not refresh. Refresh will display changes made to existing records, but only requery will display newly added records.

Here is a more concise version of the code (put this in a Module, so that you can call it from any form):

Public Sub RequeryOpenForms()
    Dim f as Form

    For Each f In Access.Forms
        f.Requery
    Next
End Sub

NOTE: Unfortunately, requery has the side-effect of losing the currently selected record. This can be particularly frustrating for a use if there are a long list of records, since they may have to scroll down a long way to find the record that they were previously looking at.

like image 61
Todd Owen Avatar answered Nov 10 '22 05:11

Todd Owen


What about something like this?

Sub AllForms()
Dim obj As AccessObject
dim dbs As Object
Dim i As Integer
dim intFormCount as Integer

    Set dbs = Application.CurrentProject
    intFormCount  = dbs.AllForms.Count - 1

    For i = 0 To intFormCount
        If dbs.AllForms(i).isloaded = True Then
            dbs.AllForms(i).refresh
        End If
    Next
End Sub
like image 36
BIBD Avatar answered Nov 10 '22 05:11

BIBD