Hi,
I was wondering if someone could help me.
I have a few tables in SQL that I wish to query. I have a form with a subform that displays my results. On my form I have 6 text boxes that will allow a user to fill in criteria.
Now my problem is that not all the fields could be filled in. Maybe only 1. So I need for my SQL query to be written on the fly via VBA.
My problem is that if one of the boxes if left blank, I get the "invalid use of Null" error.
Could someone please help me try and get the VBA code to skip the SQL where entry if that field has been left blank.
Here is my code:
Private Sub cmdStart_Click()
Dim strSQL As String
Dim qdf As DAO.QueryDef
Dim partno As String
Dim Customer As String
Dim Description As String
Dim CustomerNo As String
Dim StartDate As String
Dim EndDate As String
partno = Me.partno
Customer = Me.Customer
Description = Me.Description
CustomerNo = Me.CustomerNo
StartDate = Me.StartDate
EndDate = Me.EndDate
If IsNull(Me.StartDate) Then
Me.StartDate = "01/01/1980"
End If
If IsNull(Me.EndDate) Then
Me.EndDate = Date
End If
Set qdf = CurrentDb.QueryDefs("qry-OperationsSQLData")
strSQL = " SELECT sitem.son, scust.comp_name, sitem.pstk, sitem.desc1, sitem.ord_qty, sitem.unit_pr, shead.cust_no, shead.order_date" & _
" FROM (shead LEFT JOIN sitem ON shead.son = sitem.son) LEFT JOIN scust ON shead.comp_no = scust.comp_no" & _
" WHERE sitem.pstk = " & Chr$(39) & partno & Chr$(39) & _
" & sitem.desc1 Like " & Chr$(39) & Chr$(37) & Description & Chr$(37) & Chr$(39) & _
" & scust.comp_name Like " & Chr$(39) & Chr$(37) & Customer & Chr$(37) & Chr$(39) & _
" & shead.cust_no Like " & Chr$(39) & Chr$(37) & CustomerNo & Chr$(37) & Chr$(39) & _
" & shead.order_date >= " & Chr$(39) & StartDate & Chr$(39) & _
" & shead.order_date <= " & Chr$(39) & EndDate & Chr$(39)
qdf.SQL = strSQL
DoCmd.OpenQuery "Qry-OperationsSearch"
Me.Refresh
Set qdf = Nothing
Exit Sub
End Sub
Thanks
I was wondering if someone could help me.
I have a few tables in SQL that I wish to query. I have a form with a subform that displays my results. On my form I have 6 text boxes that will allow a user to fill in criteria.
Now my problem is that not all the fields could be filled in. Maybe only 1. So I need for my SQL query to be written on the fly via VBA.
My problem is that if one of the boxes if left blank, I get the "invalid use of Null" error.
Could someone please help me try and get the VBA code to skip the SQL where entry if that field has been left blank.
Here is my code:
Private Sub cmdStart_Click()
Dim strSQL As String
Dim qdf As DAO.QueryDef
Dim partno As String
Dim Customer As String
Dim Description As String
Dim CustomerNo As String
Dim StartDate As String
Dim EndDate As String
partno = Me.partno
Customer = Me.Customer
Description = Me.Description
CustomerNo = Me.CustomerNo
StartDate = Me.StartDate
EndDate = Me.EndDate
If IsNull(Me.StartDate) Then
Me.StartDate = "01/01/1980"
End If
If IsNull(Me.EndDate) Then
Me.EndDate = Date
End If
Set qdf = CurrentDb.QueryDefs("qry-OperationsSQLData")
strSQL = " SELECT sitem.son, scust.comp_name, sitem.pstk, sitem.desc1, sitem.ord_qty, sitem.unit_pr, shead.cust_no, shead.order_date" & _
" FROM (shead LEFT JOIN sitem ON shead.son = sitem.son) LEFT JOIN scust ON shead.comp_no = scust.comp_no" & _
" WHERE sitem.pstk = " & Chr$(39) & partno & Chr$(39) & _
" & sitem.desc1 Like " & Chr$(39) & Chr$(37) & Description & Chr$(37) & Chr$(39) & _
" & scust.comp_name Like " & Chr$(39) & Chr$(37) & Customer & Chr$(37) & Chr$(39) & _
" & shead.cust_no Like " & Chr$(39) & Chr$(37) & CustomerNo & Chr$(37) & Chr$(39) & _
" & shead.order_date >= " & Chr$(39) & StartDate & Chr$(39) & _
" & shead.order_date <= " & Chr$(39) & EndDate & Chr$(39)
qdf.SQL = strSQL
DoCmd.OpenQuery "Qry-OperationsSearch"
Me.Refresh
Set qdf = Nothing
Exit Sub
End Sub
Thanks