Kayleigh
Member
- Local time
- Today, 16:52
- Joined
- Sep 24, 2020
- Messages
- 709
I have a toggle button to change queries behind data source of a report but doesn't seem to be changing the data - any ideas how to resolve?
Here is my code:
Here is my code:
Code:
Private Sub cmdDataSource_Click()
Dim strSQL1 As String
Dim strSQL2 As String
Dim strSQL3 As String
Dim strSQL4 As String
Dim qd1 As DAO.QueryDef
Dim qd2 As DAO.QueryDef
Dim db As DAO.Database
Set db = CurrentDb()
Set qd1 = db.QueryDefs("qrySumClientValBase")
Set qd2 = db.QueryDefs("qrySumClientValBaseAll")
Dim x As Boolean
Debug.Print Me.cmdDataSource.Caption
If Me.cmdDataSource.Caption = "All Sales" Then
Me.cmdDataSource.Caption = "All Enquiries"
Me.lblHeader.Caption = "Sales Source Report by Enquiry Date"
x = True
Else
Me.cmdDataSource.Caption = "All Sales"
Me.lblHeader.Caption = "Source Report by Sold Date"
x = False
End If
strSQL1 = "SELECT qryOrderExtended.fldOrderID, qryOrderExtended.fldAClientID, qryOrderExtended.cfGrandTotal, tblClients.fldCHDYHAUID, qryOrderExtended.fldOHDYHAU " & vbCrLf & _
"FROM (qryOrderExtended INNER JOIN lkptblOrderStatus ON qryOrderExtended.fldOStatusID = lkptblOrderStatus.fldOrderStatusID) INNER JOIN tblClients ON qryOrderExtended.fldAClientID = tblClients.fldClientID " & vbCrLf & _
"WHERE (((lkptblOrderStatus.fldOSSort)>45) AND ((qryOrderExtended.fldOStatusID)<>12) AND ((tblClients.fldCTradeRetailID)=[Forms]![frmdlgSalesRpt]![cmbTradeRtl]) AND ((qryOrderExtended.fldOSoldDate)>=[Forms]![frmdlgSalesRpt]![txtFrom] And (qryOrderExtended.fldOSoldDate)<=[Forms]![frmdlgSalesRpt]![txtTo]));"
strSQL2 = "SELECT qryOrderExtended.fldOrderID, qryOrderExtended.fldAClientID, qryOrderExtended.cfGrandTotal, tblClients.fldCHDYHAUID, qryOrderExtended.fldOHDYHAU " & vbCrLf & _
"FROM (qryOrderExtended INNER JOIN lkptblOrderStatus ON qryOrderExtended.fldOStatusID = lkptblOrderStatus.fldOrderStatusID) INNER JOIN tblClients ON qryOrderExtended.fldAClientID = tblClients.fldClientID " & vbCrLf & _
"WHERE (((lkptblOrderStatus.fldOSSort)>45) AND ((qryOrderExtended.fldOStatusID)<>12) AND ((tblClients.fldCTradeRetailID)=[Forms]![frmdlgSalesRpt]![cmbTradeRtl]) AND ((qryOrderExtended.fldOSoldDate)>=[Forms]![frmdlgSalesRpt]![txtFrom] And (qryOrderExtended.fldOSoldDate)<=[Forms]![frmdlgSalesRpt]![txtTo]));"
strSQL3 = "SELECT DISTINCT qryOrderExtended.fldOrderID, qryOrderExtended.fldAClientID, qryOrderExtended.cfGrandTotal, qryOrderExtended.fldOHDYHAU, tblClients.fldCHDYHAUID " & vbCrLf & _
"FROM qryOrderExtended INNER JOIN tblClients ON qryOrderExtended.fldAClientID = tblClients.fldClientID " & vbCrLf & _
"WHERE (((qryOrderExtended.fldOCreated)>=[Forms]![frmdlgSalesRpt]![txtFrom] And (qryOrderExtended.fldOCreated)<=[Forms]![frmdlgSalesRpt]![txtTo]) AND ((tblClients.fldCTradeRetailID)=[Forms]![frmdlgSalesRpt]![cmbTradeRtl]));"
strSQL4 = "SELECT DISTINCT qryOrderExtended.fldOrderID, qryOrderExtended.fldAClientID, qryOrderExtended.cfGrandTotal, qryOrderExtended.fldOHDYHAU, tblClients.fldCHDYHAUID, qryOrderExtended.fldOSoldDate " & vbCrLf & _
"FROM qryOrderExtended INNER JOIN tblClients ON qryOrderExtended.fldAClientID = tblClients.fldClientID " & vbCrLf & _
"WHERE (((qryOrderExtended.fldOCreated)>=[Forms]![frmdlgSalesRpt]![txtFrom] And (qryOrderExtended.fldOCreated)<=[Forms]![frmdlgSalesRpt]![txtTo]) AND ((tblClients.fldCTradeRetailID)=[Forms]![frmdlgSalesRpt]![cmbTradeRtl])) OR (((tblClients.fldCTradeRetailID)=[Forms]![frmdlgSalesRpt]![cmbTradeRtl]) AND ((qryOrderExtended.fldOSoldDate)>=[Forms]![frmdlgSalesRpt]![txtFrom] And (qryOrderExtended.fldOSoldDate)<=[Forms]![frmdlgSalesRpt]![txtTo]));"
If x = True Then
qd1.SQL = strSQL1
qd2.SQL = strSQL3
Else
qd1.SQL = strSQL2
qd2.SQL = strSQL4
End If
Set qd1 = Nothing
Set qd2 = Nothing
Set db = Nothing
Me.RecordSource = "qrysumorderval"
Me.Requery
End Sub