Button on Form running a Query

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a button on a form performing a query, using data from the form to go
into the "Criteria" argument of the Query. Does this even seem like I'm
getting close? New to VBA for Access, but old to VBA. Thanks.

Option Explicit:

Private Sub CmdTirtlQry_Click()

Dim datBegin As Date
Dim strBegTime As String
Dim datEnd As Date
Dim strEndTime As String

datBegin = List14.Value 'value from the listbox
datEnd = List18.Value
strBegTime = List16.Value
strEndTime = List20.Value

Query!tirtltest.Filter = "[date]=#" & datBegin & "#"
Query!tirtltest.FilterOn = True

End Sub

Of course I would try and filter using all the variables, but I hope someone
can point me in the right direction.
 
Two points
1. You don't have to use vba to do this, you can have reference in the query
to the fields in the form

Select * From TableName Where [Date] = Forms![FormName]![List14]

2. The Date field name is a reserve name in Access, it better if you rename
the field name. Or always put it in square brackets.
==============================================
If you still want to use VBA then please tell me
 
Where would I add this reference? It looks like SQL correct? I had an idea
it could be done without VBA, but I wasn't quite sure how. This appears
quite simplier.
 
In the query itself, there is no need to rebuild the sql of the query, you
create it once with the filter and after the fields in the form are filled
you can run the query.
 
Ah, so I just modify or change the "Where" clause in the SQL. Thanks, I'll
give it a try and see if it works.
 
Back
Top