Bzouiden
03-11-2005, 01:10 PM
Ok I have two tables (DNBLIB_PFSOLD & dnblib2) with are linked to outside data sources. I have a query (qrySALESDATA) which gets critera from a form (frmSEARCH). DNBLIB_PFSOLD has customer info like contact name, address, etc. DNBLIB_MSTSALES has sales numbers in it. Both tables have account numbers so I setup a relationship between them using account number. So everything works great until I try to get the data from DNBLIB_MSTSALES to show up in the query. I get duplicated data from dnblib1 and different data from DNBLIB_MSTSALES. I can create a new query without using the form as critera passthru and it works great. I also noticed that when I run the query the relationship doesn't show up on the query. Sorry if this doesn't make sense but it is hard to explain in words. I don't know why this isn't working. thanks in advance for any assistance.
Bryan
ALSO HERE IS MY CODE for the search form:
Private Sub cmdSearch_Click()
'Set the Dimensions of the Module
Dim strSQL As String, strOrder As String, strWhere As String
Dim dbNm As Database
Dim qryDef As QueryDef
Set dbNm = CurrentDb()
'Constant Select statement for the Query definition
'THIS LINE IS WORKING HOWEVER DOES NOT PULL IN MSTSALES TABLE
strSQL = "SELECT DNBLIB_PFSOLD.CSOSLD, DNBLIB_PFSOLD.CSONME, DNBLIB_PFSOLD.CSOAD1, DNBLIB_PFSOLD.CSOAD2, DNBLIB_PFSOLD.CSOCTY, DNBLIB_PFSOLD.CSOST,DNBLIB_PFSOLD.CSOZIP, DNBLIB_PFSOLD.CSOARN, DNBLIB_PFSOLD.CSOTEL, DNBLIB_PFSOLD.CSOFAX, DNBLIB_PFSOLD.CSOSSM " & _
"FROM DNBLIB_PFSOLD"
'?THIS WAS YET ANOTHER ATTEMPT TO GET THIS WORKING, ADDED SLSOLD & CSOSLD TO KEEP RELATIONSHIP IN QRY
'strSQL = "SELECT DNBLIB_PFSOLD.CSOSLD, DNBLIB_MSTSALES.SLSOLD, DNBLIB_PFSOLD.CSONME, DNBLIB_PFSOLD.CSOAD1, DNBLIB_PFSOLD.CSOAD2, DNBLIB_PFSOLD.CSOCTY, DNBLIB_PFSOLD.CSOST,DNBLIB_PFSOLD.CSOZIP, DNBLIB_PFSOLD.CSOARN, DNBLIB_PFSOLD.CSOTEL, DNBLIB_PFSOLD.CSOFAX, DNBLIB_PFSOLD.CSOSSM, DNBLIB_MSTSALES.SLCYYD, DNBLIB_MSTSALES.SLLYYD, DNBLIB_MSTSALES.SLPYR1, DNBLIB_MSTSALES.SLPYR2, DNBLIB_MSTSALES.SLPYR3, DNBLIB_MSTSALES.SLPYR4 " & _
'"FROM DNBLIB_PFSOLD, DNBLIB_MSTSALES"
'?THIS LINE WILL SELECT ALL RECORDS BUT ERASES RELATIONSHIP SO PFSOLD RECORDS ARE DUP BUT MSTSALES ARE NOT?
'strSQL = "SELECT DNBLIB_PFSOLD.*, DNBLIB_MSTSALES.*" & _
"FROM DNBLIB_PFSOLD, DNBLIB_MSTSALES"
'?THIS LINE WILL NEED TO BE INSERTED SOMEWHERE INORDER TO GRAB INFO FROM MSTSALES?
'strSQL = "SELECT DNBLIB_MSTSALES.SLCYYD, DNBLIB_MSTSALES.SLLYYD, DNBLIB_MSTSALES.SLPYR1, DNBLIB_MSTSALES.SLPYR2, DNBLIB_MSTSALES.SLPYR3, DNBLIB_MSTSALES.SLPYR4 " & _
"FROM DNBLIB_MSTSALES"
strWhere = "WHERE"
strOrder = "ORDER BY DNBLIB_PFSOLD.CSONME;"
'Set the WHERE clause for the QueryDef if information has been entered into a field on the form
If Not IsNull(Me.txtCSONME) Then '<--If the textbox txtCSONME contains no data THEN do nothing
strWhere = strWhere & " (DNBLIB_PFSOLD.CSONME) Like '*" & Me.txtCSONME & "*' AND" '<--otherwise, apply the LIKE statment to the QueryDef
End If
If Not IsNull(Me.txtCSOSLD) Then
strWhere = strWhere & " (DNBLIB_PFSOLD.CSOSLD) Like '*" & Me.txtCSOSLD & "*' AND"
End If
If Not IsNull(Me.txtCSOARN) Then
strWhere = strWhere & " (DNBLIB_PFSOLD.CSOARN) Like '*" & Me.txtCSOARN & "*' AND"
End If
If Not IsNull(Me.txtCSOAD1) Then
strWhere = strWhere & " (DNBLIB_PFSOLD.CSOAD1) Like '*" & Me.txtCSOAD1 & "*' AND"
End If
If Not IsNull(Me.txtCSOSSM) Then
strWhere = strWhere & " (DNBLIB_PFSOLD.CSOSSM) Like '*" & Me.txtCSOSSM & "*' AND"
End If
If Not IsNull(Me.txtCSOCTY) Then
strWhere = strWhere & " (DNBLIB_PFSOLD.CSOCTY) Like '*" & Me.txtCSOCTY & "*' AND"
End If
If Not IsNull(Me.txtCSOST) Then
strWhere = strWhere & " (DNBLIB_PFSOLD.CSOST) Like '*" & Me.txtCSOST & "*' AND"
End If
If Not IsNull(Me.txtCSOZIP) Then
strWhere = strWhere & " (DNBLIB_PFSOLD.CSOZIP) Like '*" & Me.txtCSOZIP & "*' AND"
End If
'If Not IsNull(Me.txtSLCYYD) Then
'strWhere = strWhere & " (MSTSALES.SLCYYD) Like '*" & Me.txtSLCYYD & "*' AND"
'End If
'If Not IsNull(Me.txtSLLYYD) Then
'strWhere = strWhere & " (MSTSALES.SLLYYD) Like '*" & Me.txtSLLYYD & "*' AND"
'End If
'Remove the last AND from the SQL statment
strWhere = Mid(strWhere, 1, Len(strWhere) - 5)
'Pass the QueryDef to the query
Set qryDef = dbNm.QueryDefs("qrySALESDATA")
qryDef.SQL = strSQL & " " & strWhere & "" & strOrder
'Open the Query
DoCmd.OpenQuery "qrySALESDATA", acViewNormal
End Sub
Bryan
ALSO HERE IS MY CODE for the search form:
Private Sub cmdSearch_Click()
'Set the Dimensions of the Module
Dim strSQL As String, strOrder As String, strWhere As String
Dim dbNm As Database
Dim qryDef As QueryDef
Set dbNm = CurrentDb()
'Constant Select statement for the Query definition
'THIS LINE IS WORKING HOWEVER DOES NOT PULL IN MSTSALES TABLE
strSQL = "SELECT DNBLIB_PFSOLD.CSOSLD, DNBLIB_PFSOLD.CSONME, DNBLIB_PFSOLD.CSOAD1, DNBLIB_PFSOLD.CSOAD2, DNBLIB_PFSOLD.CSOCTY, DNBLIB_PFSOLD.CSOST,DNBLIB_PFSOLD.CSOZIP, DNBLIB_PFSOLD.CSOARN, DNBLIB_PFSOLD.CSOTEL, DNBLIB_PFSOLD.CSOFAX, DNBLIB_PFSOLD.CSOSSM " & _
"FROM DNBLIB_PFSOLD"
'?THIS WAS YET ANOTHER ATTEMPT TO GET THIS WORKING, ADDED SLSOLD & CSOSLD TO KEEP RELATIONSHIP IN QRY
'strSQL = "SELECT DNBLIB_PFSOLD.CSOSLD, DNBLIB_MSTSALES.SLSOLD, DNBLIB_PFSOLD.CSONME, DNBLIB_PFSOLD.CSOAD1, DNBLIB_PFSOLD.CSOAD2, DNBLIB_PFSOLD.CSOCTY, DNBLIB_PFSOLD.CSOST,DNBLIB_PFSOLD.CSOZIP, DNBLIB_PFSOLD.CSOARN, DNBLIB_PFSOLD.CSOTEL, DNBLIB_PFSOLD.CSOFAX, DNBLIB_PFSOLD.CSOSSM, DNBLIB_MSTSALES.SLCYYD, DNBLIB_MSTSALES.SLLYYD, DNBLIB_MSTSALES.SLPYR1, DNBLIB_MSTSALES.SLPYR2, DNBLIB_MSTSALES.SLPYR3, DNBLIB_MSTSALES.SLPYR4 " & _
'"FROM DNBLIB_PFSOLD, DNBLIB_MSTSALES"
'?THIS LINE WILL SELECT ALL RECORDS BUT ERASES RELATIONSHIP SO PFSOLD RECORDS ARE DUP BUT MSTSALES ARE NOT?
'strSQL = "SELECT DNBLIB_PFSOLD.*, DNBLIB_MSTSALES.*" & _
"FROM DNBLIB_PFSOLD, DNBLIB_MSTSALES"
'?THIS LINE WILL NEED TO BE INSERTED SOMEWHERE INORDER TO GRAB INFO FROM MSTSALES?
'strSQL = "SELECT DNBLIB_MSTSALES.SLCYYD, DNBLIB_MSTSALES.SLLYYD, DNBLIB_MSTSALES.SLPYR1, DNBLIB_MSTSALES.SLPYR2, DNBLIB_MSTSALES.SLPYR3, DNBLIB_MSTSALES.SLPYR4 " & _
"FROM DNBLIB_MSTSALES"
strWhere = "WHERE"
strOrder = "ORDER BY DNBLIB_PFSOLD.CSONME;"
'Set the WHERE clause for the QueryDef if information has been entered into a field on the form
If Not IsNull(Me.txtCSONME) Then '<--If the textbox txtCSONME contains no data THEN do nothing
strWhere = strWhere & " (DNBLIB_PFSOLD.CSONME) Like '*" & Me.txtCSONME & "*' AND" '<--otherwise, apply the LIKE statment to the QueryDef
End If
If Not IsNull(Me.txtCSOSLD) Then
strWhere = strWhere & " (DNBLIB_PFSOLD.CSOSLD) Like '*" & Me.txtCSOSLD & "*' AND"
End If
If Not IsNull(Me.txtCSOARN) Then
strWhere = strWhere & " (DNBLIB_PFSOLD.CSOARN) Like '*" & Me.txtCSOARN & "*' AND"
End If
If Not IsNull(Me.txtCSOAD1) Then
strWhere = strWhere & " (DNBLIB_PFSOLD.CSOAD1) Like '*" & Me.txtCSOAD1 & "*' AND"
End If
If Not IsNull(Me.txtCSOSSM) Then
strWhere = strWhere & " (DNBLIB_PFSOLD.CSOSSM) Like '*" & Me.txtCSOSSM & "*' AND"
End If
If Not IsNull(Me.txtCSOCTY) Then
strWhere = strWhere & " (DNBLIB_PFSOLD.CSOCTY) Like '*" & Me.txtCSOCTY & "*' AND"
End If
If Not IsNull(Me.txtCSOST) Then
strWhere = strWhere & " (DNBLIB_PFSOLD.CSOST) Like '*" & Me.txtCSOST & "*' AND"
End If
If Not IsNull(Me.txtCSOZIP) Then
strWhere = strWhere & " (DNBLIB_PFSOLD.CSOZIP) Like '*" & Me.txtCSOZIP & "*' AND"
End If
'If Not IsNull(Me.txtSLCYYD) Then
'strWhere = strWhere & " (MSTSALES.SLCYYD) Like '*" & Me.txtSLCYYD & "*' AND"
'End If
'If Not IsNull(Me.txtSLLYYD) Then
'strWhere = strWhere & " (MSTSALES.SLLYYD) Like '*" & Me.txtSLLYYD & "*' AND"
'End If
'Remove the last AND from the SQL statment
strWhere = Mid(strWhere, 1, Len(strWhere) - 5)
'Pass the QueryDef to the query
Set qryDef = dbNm.QueryDefs("qrySALESDATA")
qryDef.SQL = strSQL & " " & strWhere & "" & strOrder
'Open the Query
DoCmd.OpenQuery "qrySALESDATA", acViewNormal
End Sub