Changing a form's recordsource through code

S

Santiago Gomez

Hi guys, these groups are amazing, thanks for taking the time.

I have a form based on a saved query (qryfrmActiveRecords). I want to add a
checkbox to the form that shows all records, ie. active and inactive
(qryfrmAllRecords).

How can I change the recordsource of the form using code?

Ideally it would look at another saved query.


Private Sub chkShowAll_Loads_Click()
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
rst.Open ("qryfrmAllRecords"), , adOpenDynamic, adLockOptimistic
Me.Recordset = rst
Me.Requery
end sub

Thanks
 
D

Dirk Goldgar

Santiago Gomez said:
Hi guys, these groups are amazing, thanks for taking the time.

I have a form based on a saved query (qryfrmActiveRecords). I want to
add a checkbox to the form that shows all records, ie. active and
inactive (qryfrmAllRecords).

How can I change the recordsource of the form using code?

Ideally it would look at another saved query.


Private Sub chkShowAll_Loads_Click()
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
rst.Open ("qryfrmAllRecords"), , adOpenDynamic, adLockOptimistic
Me.Recordset = rst
Me.Requery
end sub

Thanks

That's more complicated than you need. Try something like this:

Private Sub chkShowAll_Loads_AfterUpdate()

If Me!chkShowAll_Loads = True Then
Me.RecordSource = "qryfrmAllRecords"
Else
Me.RecordSource = "qryfrmActiveRecords"
End If

End Sub
 
S

Santiago Gomez

very good to know, thanks a million

Dirk Goldgar said:
That's more complicated than you need. Try something like this:

Private Sub chkShowAll_Loads_AfterUpdate()

If Me!chkShowAll_Loads = True Then
Me.RecordSource = "qryfrmAllRecords"
Else
Me.RecordSource = "qryfrmActiveRecords"
End If

End Sub

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top