Results 1 to 10 of 10
  1. #1
    RedGoneWILD is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jun 2010
    Posts
    105

    Can not refresh a form - what am I doing wrong?

    I have a form that is populated with employee records from a query. I have a button that I want to use to show/hide inactive employees. When it is clicked, the query is updated correctly. But I can not get the form to refresh. I have tried Me.refresh, Me.repaint, Me.requery but none of those seem to work. When I close the form, then reopen it, it's displayed correctly since the query is correct. But I can't get it to refresh automatically without closing the form. Any suggestions?

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Requery should work. What exactly is your code?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    RedGoneWILD is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jun 2010
    Posts
    105
    I can post the exact code later when I get home, but essentially it's:

    sub button_click()
    dim db as dao.database
    dim qdf as dao.querydef
    set qdf = db.querydefs
    dim sqlstr as string
    sqlstr = "SELECT ....."

    qdf.sql = sqlstr
    'at this point, the query is properly updated. the last thing i need to do is refresh form.

    'here is where i tried me.refresh/requery/repaint with no success.
    end sub

  4. #4
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try with the Requery, and maybe throw a DoEvents right before in case it's a timing issue. If that doesn't work, post the exact code and maybe we'll see a hole in the process.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    RedGoneWILD is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jun 2010
    Posts
    105
    Tried DoEvents as well, but never used that before so didn't really understand what I found on the web for it. Form view is "Continuous Forms", each row has two textboxes (1 for ID, 1 for name), a checkbox (for inactive/active) and a button that opens up the employee detail form. Code is below:

    Code:
    Private Sub btnInactives_Click()
    On Error GoTo Err_btnInactives_Click
        Dim db As DAO.Database
        Dim qdf As DAO.QueryDef
        Set db = CurrentDb
        Set qdf = db.QueryDefs("qryLeasedDriverList")
        
        Dim sqlStr As String
        sqlStr = "SELECT [Leased Drivers].EmployeeNumber, [Leased Drivers].LastName, [Leased Drivers].FirstName, [Leased Drivers].Inactive " & _
                "FROM [Leased Drivers] "
    
    
        'default button caption is "Show Inactives"
        'if button reads "Hide Inactives" then add WHERE clause to query
        If Me.btnInactives.Caption = "Hide Inactives" Then
            sqlStr = sqlStr & "WHERE((([Leased Drivers].Inactive) = False)) "
            Me.btnInactives.Caption = "Show Inactives"
        Else
            Me.btnInactives.Caption = "Hide Inactives"
        End If
    
    
        sqlStr = sqlStr & "ORDER BY [Leased Drivers].EmployeeNumber;"
    
    
        qdf.SQL = sqlStr
        
        DoEvents
    
    
        Me.Requery
    
    
    Exit_btnInactives_Click:
        Exit Sub
    
    
    Err_btnInactives_Click:
        MsgBox Err.Description
        Resume Exit_btnInactives_Click
    End Sub

  6. #6
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I would expect that to work, presuming the form the code is in is bound to that query. There isn't a subform involved here, is there? Can you post the db here?

    Another direction you might consider is a checkbox with code like this behind it:

    Code:
      If Me.chkShowRetired = True Then
        Me.FilterOn = False
      Else
        Me.Filter = "Retired = False"
        Me.FilterOn = True
      End If
    Which in your case would be

    Me.Filter = "Inactive = False"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    RedGoneWILD is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jun 2010
    Posts
    105
    Can't post the database. no subform. form is bound to query. I basically took over a 12 year old program, and am adding new features and removing the junk (btw it's .mdb, not .accdb if that makes any difference). This is a pretty minor addition, but it would be nice if this form could be refreshed to show only the records that the user will want to see.

    I will try the checkbox filter to see if that works. Thanks again for your help.

  8. #8
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    accdb or mdb shouldn't matter. I can only imagine it's some sort of timing issue, though I thought the DoEvents would account for that. If you don't like the filter method, this should certainly work because I do it a lot:

    Me.RecordSource = sqlStr

    In essence you're changing the record source of the form from the query to the SQL while it's open.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    RedGoneWILD is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jun 2010
    Posts
    105
    Me.RecordSource = sqlStr worked exactly as I wanted. I kept thinking I should try that, not sure why I didn't.

    The odd thing is I have Me.requery set to the Got_Focus event. When you open the Employee file from that form, and then change an employee from active to inactive, then close the Employee form and return to the one this one, it immediately removes their name from the list (provided Hide Inactives is true). Oh well, I'll figure it out I guess when I have more time, but your solution worked like a charm.

    Cheers!

  10. #10
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Form with Subreport Displaying Wrong
    By claysea in forum Forms
    Replies: 3
    Last Post: 02-14-2012, 03:25 PM
  2. Replies: 1
    Last Post: 09-30-2011, 06:54 PM
  3. Access 2010 Refresh VS Refresh ALL
    By Snwboarder1982 in forum Access
    Replies: 1
    Last Post: 09-09-2011, 04:07 PM
  4. Refresh Form
    By tpcervelo in forum Forms
    Replies: 7
    Last Post: 02-07-2011, 08:03 AM
  5. refresh a form
    By RedGoneWILD in forum Forms
    Replies: 18
    Last Post: 09-03-2010, 08:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums