P
Preston
Hi,
i am trying to make a search form that users can use to enter criteria
like name, date range, etc and then have the results be queried upon and
displayed in the subform on the bottom of the main form. i was able to get
this to work to some degree using form text boxes as filters for the subform
query but there is more logic that i want to be able to apply. specifically i
want the query to look at beg date and end date and if they're both filled in
to use BETWEEN if only beg then dates >= beg if only end then dates <= end. I
wasn't able to do this in the access query creation boxes so i switched to
vba. I can't seem to get this to work. i am able to create a correct sql
statement and then i use currentdb.querydefs("qry").sql to update the
underlying query. next i run docmd.requery "qryMarkPaid subform" this is the
actual subform object on the main form. it worked for a little while then
stopped. i'm not even sure if this is the best approach. thank you for any
help you can give. i will include the code thus far:
+++++++++++++++++++++++++++++++++++
Private Sub Command14_Click()
'On Error GoTo ErrorHandler
Dim strWHERE As String
Dim strSQL As String
Dim qryMarkPaid As QueryDef
If Not IsNull(Me!txtSONum) Then strWHERE = " tblsvcordersdet.svcorder = "
& Me!txtSONum & " And"
If Not IsNull(Me!cmbTechName) Then strWHERE = strWHERE & "
tblsvcordersdet.techid = " & Me!cmbTechName & " AND"
If Not IsNull(Me!txtBegDate) And Not IsNull(Me!txtEndDate) Then
strWHERE = strWHERE & " tblsvcordersdet.dateworked BETWEEN #" &
Me!txtBegDate & _
"# AND #" & Me!txtEndDate & "#"
ElseIf Not IsNull(Me!txtBegDate) And IsNull(Me!txtEndDate) Then
strWHERE = strWHERE & " tblsvcordersdet.dateworked >=#" & Me!txtBegDate
& "#"
ElseIf IsNull(Me!txtBegDate) And Not IsNull(Me!txtEndDate) Then
strWHERE = strWHERE & " tblsvcrodersdet.dateworked <=#" & Me!txtEndDate
& "#"
End If
If Len(strWHERE) <> 0 Then strWHERE = " WHERE" & strWHERE
If Right(strWHERE, 4) = " AND" Then strWHERE = Left(strWHERE,
Len(strWHERE) - 4)
strSQL = "SELECT tblsvcordersdet.svcorder, tblsvcordersdet.dateworked,
tblsvcordersdet.techid," _
& " tblsvcordersdet.[reg hours], tblsvcordersdet.[OT Hours],
tblsvcordersdet.paid FROM tblsvcordersdet" _
& strWHERE
CurrentDb.QueryDefs("qryMarkPaid").SQL = strSQL
DoCmd.Requery "qryMarkPaid subform"
Exit Sub
ErrorHandler:
'show error message and end sub
MsgBox "Error " & Err.Number & ": " & Err.Description
End Sub
i am trying to make a search form that users can use to enter criteria
like name, date range, etc and then have the results be queried upon and
displayed in the subform on the bottom of the main form. i was able to get
this to work to some degree using form text boxes as filters for the subform
query but there is more logic that i want to be able to apply. specifically i
want the query to look at beg date and end date and if they're both filled in
to use BETWEEN if only beg then dates >= beg if only end then dates <= end. I
wasn't able to do this in the access query creation boxes so i switched to
vba. I can't seem to get this to work. i am able to create a correct sql
statement and then i use currentdb.querydefs("qry").sql to update the
underlying query. next i run docmd.requery "qryMarkPaid subform" this is the
actual subform object on the main form. it worked for a little while then
stopped. i'm not even sure if this is the best approach. thank you for any
help you can give. i will include the code thus far:
+++++++++++++++++++++++++++++++++++
Private Sub Command14_Click()
'On Error GoTo ErrorHandler
Dim strWHERE As String
Dim strSQL As String
Dim qryMarkPaid As QueryDef
If Not IsNull(Me!txtSONum) Then strWHERE = " tblsvcordersdet.svcorder = "
& Me!txtSONum & " And"
If Not IsNull(Me!cmbTechName) Then strWHERE = strWHERE & "
tblsvcordersdet.techid = " & Me!cmbTechName & " AND"
If Not IsNull(Me!txtBegDate) And Not IsNull(Me!txtEndDate) Then
strWHERE = strWHERE & " tblsvcordersdet.dateworked BETWEEN #" &
Me!txtBegDate & _
"# AND #" & Me!txtEndDate & "#"
ElseIf Not IsNull(Me!txtBegDate) And IsNull(Me!txtEndDate) Then
strWHERE = strWHERE & " tblsvcordersdet.dateworked >=#" & Me!txtBegDate
& "#"
ElseIf IsNull(Me!txtBegDate) And Not IsNull(Me!txtEndDate) Then
strWHERE = strWHERE & " tblsvcrodersdet.dateworked <=#" & Me!txtEndDate
& "#"
End If
If Len(strWHERE) <> 0 Then strWHERE = " WHERE" & strWHERE
If Right(strWHERE, 4) = " AND" Then strWHERE = Left(strWHERE,
Len(strWHERE) - 4)
strSQL = "SELECT tblsvcordersdet.svcorder, tblsvcordersdet.dateworked,
tblsvcordersdet.techid," _
& " tblsvcordersdet.[reg hours], tblsvcordersdet.[OT Hours],
tblsvcordersdet.paid FROM tblsvcordersdet" _
& strWHERE
CurrentDb.QueryDefs("qryMarkPaid").SQL = strSQL
DoCmd.Requery "qryMarkPaid subform"
Exit Sub
ErrorHandler:
'show error message and end sub
MsgBox "Error " & Err.Number & ": " & Err.Description
End Sub