View Full Version : Parameter Query


aziz rasul
05-11-2007, 07:35 AM
Why does strSQL1 work and strSQL2 not work?

Private Sub Form_Close()

Dim strSQL1 As String
Dim strSQL2 As String
Dim qdfNew As DAO.QueryDef
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset

If IsNull(Me.Form!frmPromotionSub2!DistributorCode) Or IsNull(Me.Form!frmPromotionSub2!LicenseeCode) Then
With CurrentDb
'strSQL1 = "SELECT tblLicenseeProducts.LicenseeCode, tblLicenseeProducts.ProductIDprod, tblLicenseeProducts.DistributorCode " & _
"FROM tblLicenseeProducts " & _
"WHERE (((tblLicenseeProducts.DistributorCode) Is Null));"
strSQL2 = "SELECT tblLicenseeProducts.LicenseeCode, tblLicenseeProducts.ProductIDprod, tblLicenseeProducts.DistributorCode " & _
"FROM tblLicenseeProducts " & _
"WHERE (((tblLicenseeProducts.LicenseeCode)=[Forms]![frmProduct&Distribution].[Form]![frmPromotionSub2]![LicenseeCode]) AND ((tblLicenseeProducts.ProductIDprod)=[Forms]![frmProduct&Distribution].[Form]![frmPromotionSub2]![ProductIDprod]) AND ((tblLicenseeProducts.DistributorCode) Is Null));"
DeleteQuery ("NewQueryDef")
Set qdfNew = .CreateQueryDef("NewQueryDef", strSQL2)
If DCount("[ProductIDprod]", "NewQueryDef") > 0 Then
Set rst = .OpenRecordset("NewQueryDef")
rst.Delete
Set rst = Nothing
End If
.QueryDefs.Delete qdfNew.Name
Set qdfNew = Nothing
End With
End If

End Sub

KeithG
05-11-2007, 07:49 AM
You need to change you WHERE clause to something like below

"WHERE (((tblLicenseeProducts.LicenseeCode)=” & [Forms]![frmProduct&Distribution].[Form]![frmPromotionSub2]![LicenseeCode] & “) AND ((tblLicenseeProducts.ProductIDprod)=” & [Forms]![frmProduct&Distribution].[Form]![frmPromotionSub2]![ProductIDprod] & “) AND((tblLicenseeProducts.DistributorCode) Is Null));"

aziz rasul
05-11-2007, 08:24 AM
Many thanks. That did the trick.