wnfisba said:
Thanks Dirk. Do you know where there are actual examples of how to do
this out there???
I'm not close to being a VB expert...a novice at best...
Okay, let's flesh the example out a little. Suppose you're running this
from a form with two text boxes, named "txtFromDate" and "txtToDate",
and a command button named "cmdRunQuery". I'll also assume you have a
pass-through query named "qptMyQuery", and you want to open this query
(to display its results as a datasheet or to execute it, if it's an
action query), when the button is clicked. But the query's SQL needs to
be modified to include the values from the text boxes as criteria.
Code might look something like this:
'----- start of untested example code -----
Private Sub cmdRunQuery_Click()
Dim strFromDate As String
Dim strToDate As String
Dim strSQL
' Make sure we have dates to work with.
' NOTE: for this example, I'm insisting that both
' dates be given. You could work it so that
' either or both dates are optional.
With Me!txtFromDate
If IsNull(.Value) Then
MsgBox "Please specify the 'from' date."
.SetFocus
Exit Sub
Else
strFromDate = Format(.Value, "YYYY-MM-DD")
End If
End With
With Me!txtToDate
If IsNull(.Value) Then
MsgBox "Please specify the 'to' date."
.SetFocus
Exit Sub
Else
strToDate = Format(.Value, "YYYY-MM-DD")
End If
End If
End With
' Build the SQL for the query. This is just
' a simple example.
strSQL = _
"SELECT * FROM SomeTable WHERE " _
"SomeDateField >= '" & strFromDate & _
" AND " & _
"SomeDateField <= '" & strToDate
CurrentDb.QueryDefs("qptMyQuery").SQL = strSQL
DoCmd.OpenQuery "qptMyQuery"
End Sub
'----- end of code -----