How to requery from a form button

sumdumgai

Registered User.
Local time
Today, 06:00
Joined
Jul 19, 2007
Messages
453
Hi again. I'm new to forms design. I've created a Form that accepts multiple field inputs and the Query that uses those inputs to select records into a datasheet view. When I change the parameters on the Form, the query does not update the resulting datasheet. I have to close the query result and then the Form button produces the new result. Is there a way besides using VBA that I can re-query the database using the new parameters?
Thanks.
 
Is the query the RecordSource of form?

Try:

Me.Requery
 
Thanks for reply. The query is a select query that I created using Query Design. On the Form, I have a button that runs that query. The recordSource is a table and the form is unbound. Does that make sense? Like I said, I'm pretty knew to forms.
 
Don't open query. Open form or report bound to query.
 
Something about your setup sounds odd. June7 is right that normally you would bind the form to an underlying record, in which case Access would take care of business for you.

So ... what's up with this form design that you DIDN'T want it bound to the record? I am not clear on your intent here.
 
if the query is already open, close it again and then re-open:

private sub button_click()
If syscmd(acSysCmdGetObjectState, acQuery, "theNameOfQuery") <> 0 Then _
DoCmd.Close acQuery, "theNameOfQuery"
DoCmd.OpenQuery "theNameOfQuery"
end sub
 
If you really must refresh a query that is open, and don't want to close:

CommandBars.ExecuteMso "DataRefreshAll"
 
that will only work if the Query has the focus.

private sub button_click()
If syscmd(acSysCmdGetObjectState, acQuery, "theNameOfQuery") <> 0 Then
DoCmd.SelectObject acQuery, "theNameOfQuery", False
commandbars.ExecuteMso("DataRefreshAll")

Else
DoCmd.OpenQuery "theNameOfQuery"
End If
end sub
 
Last edited:
Thanks, I have never actually used that code. Just picked up from somewhere in another similar thread. So always forget about the focus issue.
 
Thanks to everyone. I found another solution in another thread that I posted.
 

Users who are viewing this thread

Back
Top Bottom