How to Requery after a table insert

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sc5502
    New Member
    • Jun 2014
    • 102

    How to Requery after a table insert

    Front End: MS Access 2010
    Back End: SQL Server 2008

    I have a form (form A) that displays records via a query. There is a button on form A that opens form B. Form B adds a table entry that to the table shown on form A. Form B closes and Form A has control. How can I do a requery so that form A displays the newly created table entry done by form B.
    Thanks in advance.
  • jforbes
    Recognized Expert Top Contributor
    • Aug 2014
    • 1107

    #2
    This is what I do when I’m in your situation. I have this function in a Module just waiting around:

    Code:
    Function isLoaded(ByRef sFormName As String) As Boolean
        
        ' Determines if a form is loaded
        Dim i As Integer
    
        isLoaded = False
        For i = 0 To Forms.Count - 1
            If Forms(i).FormName = sFormName Then
                isLoaded = True
                Exit Function
            End If
        Next
    End Function
    In FormB, I put this code right after the code that inserts the new record.
    Code:
    If isLoaded("FormA") Then
        Forms!FormA.Requery
    End If

    Comment

    • twinnyfo
      Recognized Expert Moderator Specialist
      • Nov 2011
      • 3664

      #3
      jforbes,

      Your first block of code is simple and seems like it could be quite useful at times.

      Of course, line 2 of your second block is the only line needed if we know the form is loaded--but I like the double-check and error prevention concepts you present.

      Nice to have you contributing to the site!

      Comment

      • sc5502
        New Member
        • Jun 2014
        • 102

        #4
        This worked. Thanks so much.
        Last edited by NeoPa; Aug 25 '14, 10:57 PM. Reason: Typo

        Comment

        Working...