Jason,
Firstly, I would format the SQL statements within your code to make them a
bit more readable. Like so:
strSQL = "SELECT Principal, " & _
"Customer, " & _
"Distributor, " & _
"Salesman, " & _
"InvoiceDate, " & _
"ExtendedPrice, " & _
"[Comm%], " & _
"[Comm $] " & _
"FROM CommissionResults " & _
"WHERE (InvoiceDate BETWEEN #" & request.Form("txtFromDate") &
_
"# AND #" & request.Form("txtToDate") &
"#)"
numSalesSQL = "SELECT SUM(ExtendedPrice) As TotalExtendedPrice " & _
"FROM CommissionResults " & _
"WHERE (InvoiceDate BETWEEN #" &
request.Form("txtFromDate") & _
"# AND #" & request.Form("txtToDate")
& "#)"
numComSQL = "SELECT SUM([Comm $]) As TotalComm " & _
"FROM CommissionResults " & _
"WHERE (InvoiceDate BETWEEN #" & request.Form("txtFromDate")
& _
"# AND #" & request.Form("txtToDate") &
"#)"
You'll notice I removed the table qualifier from every column name. If the
table is the same, there's no need to fully qualify the column names. It
just makes it easier to read.
Secondly, although you can get away without it, whenever you use aggregation
in SQL, you should give it an alias. For example, the following two
statements:
numSalesSQL = "SELECT SUM(ExtendedPrice) "
numComSQL = "SELECT SUM([Comm $]) "
....should be:
numSalesSQL = "SELECT SUM(ExtendedPrice) As TotalExtendedPrice "
numComSQL = "SELECT SUM([Comm $]) As TotalComm "
Thirdly, and I'm betting this is your problem, if SalesmanID is text, then
your assignment is OK, but if it's numeric, the assignment should be as
follows:
strSQL = strSQL & "SalesmanID = " & arrSalesmen(I)
numSalesSQL = numSalesSQL & "SalesmanID = " & arrSalesmen(I)
numComSQL = numComSQL & "SalesmanID = " & arrSalesmen(I)
....the same can be said about PrincipalID, CustomerID, and DistributorID.
I'm not familiar with ASP, so your code looks good to a blind man. The SQL
looks OK now, so if I were you, I'd check the contents of strSQL,
numSalesSQL and numComSQL just before you poke them into a recordset. Print
them out (maybe Debug.Print) and check them for problems.
Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html
Jason said:
Graham,
First, thank you for your response, however I did not totally understand
your response. I am building the query in code. I am including it in my
reply. My problem is that I am only getting part of the results I desire.
WHERE ( conditionA AND conditionB) AND (conditionC OR conditionD OR
conditionE) AND (conditionF OR conditionG OR conditionH). What I am looking
for is the recordset that meet Conditions A and B and any of C, D, OR E that
meet condition F, G, or H. So, it would return A, B, C, F or A, B, D, F or
A, B, E, H, G etc. Basically any combination of A, B, and all records that
meet condition C that have condition F, G, or H as well as all records that
meet A, B and all records that meet condition D that have condition F,G, H.
and so on down the line.
I hope this clarifies my question and thank you very much for your help.
I know this may not be the cleanest code, but everything is working fine except for the sql.
set connlib = server.createobject("adodb.connection")
with connlib
.connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("Commissions.mdb")
.open
end with
Dim strSQL
Dim numSalesSQL
Dim numComSQL
Dim arrSalesmen
Dim arrDistributors
Dim arrPrincipals
Dim arrCustomers
Dim curSales
Dim curCommissions
arrDistributors = split(request.Form("selDistributors"), ",")
strSQL = "SELECT CommissionResults.Principal, CommissionResults.Customer,
CommissionResults.Distributor, CommissionResults.Salesman,
CommissionResults.InvoiceDate, CommissionResults.ExtendedPrice,
CommissionResults.[Comm%], CommissionResults.[Comm $] FROM CommissionResults
WHERE (InvoiceDate > #" & request.Form("txtFromDate") & "# AND InvoiceDate <
#" & request.Form("txtToDate") & "#)"
numSalesSQL = "SELECT SUM(CommissionResults.ExtendedPrice) FROM
CommissionResults WHERE (InvoiceDate > #" & request.Form("txtFromDate") & "#
AND InvoiceDate < #" & request.Form("txtToDate") & "#)"
numComSQL = "SELECT SUM(CommissionResults.[Comm $]) FROM
CommissionResults WHERE (InvoiceDate > #" & request.Form("txtFromDate") & "#
AND InvoiceDate < #" & request.Form("txtToDate") & "#)"