How to requery from a form button (1 Viewer)

sumdumgai

Registered User.
Local time
Today, 09:31
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.
 

June7

AWF VIP
Local time
Today, 05:31
Joined
Mar 9, 2014
Messages
5,466
Is the query the RecordSource of form?

Try:

Me.Requery
 

sumdumgai

Registered User.
Local time
Today, 09:31
Joined
Jul 19, 2007
Messages
453
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.
 

June7

AWF VIP
Local time
Today, 05:31
Joined
Mar 9, 2014
Messages
5,466
Don't open query. Open form or report bound to query.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:31
Joined
Feb 28, 2001
Messages
27,142
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.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:31
Joined
May 7, 2009
Messages
19,233
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
 

June7

AWF VIP
Local time
Today, 05:31
Joined
Mar 9, 2014
Messages
5,466
If you really must refresh a query that is open, and don't want to close:

CommandBars.ExecuteMso "DataRefreshAll"
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:31
Joined
May 7, 2009
Messages
19,233
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:

June7

AWF VIP
Local time
Today, 05:31
Joined
Mar 9, 2014
Messages
5,466
Thanks, I have never actually used that code. Just picked up from somewhere in another similar thread. So always forget about the focus issue.
 

sumdumgai

Registered User.
Local time
Today, 09:31
Joined
Jul 19, 2007
Messages
453
Thanks to everyone. I found another solution in another thread that I posted.
 

Users who are viewing this thread

Top Bottom