Rob has attended:
Excel VBA Advanced course
Problems with SQL statements in VBA dealing with filtering.
Hi,
I am trying to access data in an access db and move into an excel sheet (on same basis as example in advanced vba course).
I am able to connect to the db and access the data however the statements in my VBA script which deals with specific filtering is not acting as expected. I am accessing five table fields as follows:
SELECT CompanyCode, AccountCode, YTD_Ccy, Level1Code, Source, Type
FROM tblPvtLink
WHERE CompanyCode = 'CSAUD' OR CompanyCode = 'CSCAD' OR CompanyCode = 'CSEUR'
AND YTD_Ccy <> 0
AND Source = 'CODA'
AND Type = 'B'
ORDER BY CompanyCode"
The syntax in the vba code is continuous stating with a " and end "... roughly as follows:
Const SQL As String = _
"SELECT CompanyCode, AccountCode, YTD_Ccy, Level1Code, Source, Type, AccountName FROM tblPvtLink WHERE CompanyCode = 'CSAUD' OR CompanyCode = 'CSCAD' OR CompanyCode = 'CSEUR' AND YTD_Ccy <> 0 AND Source = 'CODA' AND Type = 'B' ORDER BY CompanyCode"
It is accessing the correct table and the correct fields, and it is even accessing the correct CompanyCodes (i.e. all the OR CSAUD, CSCAD, CSEUR statements are working), but ...
1. it is not excluding zeros (i.e. AND YTD_Ccy <> 0 is not working) as zeros are being returned;
2. it is not limiting selection to of Source = 'CODA' (i.e. AND Source = 'CODA' is not working...other sources are returned incorrectly);
3. it is not limiting slection to Type = 'B' (i.e. AND Type = 'B' is not working...other types are returned incorrectly);
4. It IS ordering by CompanyCode correctly (however as a separate note I'd like to order by CompanyCode first and then by AccountCode but I have no idea what the correct addition syntax is for this ... i.e. is it perhaps ORDER BY CompanyCode THEN BY AccountCode?).
5. As another separate issue is there a more convenient and shorter syntax to filter multiple CompanyCodes without using OR statements...eg. WHERE CompanyCode = {CSAUD, CSCAD, CSEUR}?
It seems the first part of the SQL statement works as well as the last part but all the AND statements are ineffectual...
I have tried moving the AND statements all to the beginning of the SQL statement after the WHERE statement and this doesn't work and the help provided on the internet is really for simple single filtering no multiple variables included.
If you could help with point 1-5 that would be very helpful...
Cheers
Rob
RE: Problems with SQL statements in VBA dealing with filtering.
hi rob
Thank you for your question
This is very hard to resolve without seeing the spreadsheet and the database file that it is extracting data from.
One suggestion would be to group the OR conditions in brackets thus:
Const SQL As String = _
"SELECT CompanyCode, AccountCode, YTD_Ccy, Level1Code, Source, Type, AccountName FROM tblPvtLink WHERE (CompanyCode = 'CSAUD' OR CompanyCode = 'CSCAD' OR CompanyCode = 'CSEUR' )AND YTD_Ccy <> 0 AND Source = 'CODA' AND Type = 'B' ORDER BY CompanyCode"
This might resolve the problem. If not please let me know and I'll see if there is anything else that we can do
Regards
Stephen