problems sql statements vba

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Problems with SQL statements in VBA dealing with filtering.

Problems with SQL statements in VBA dealing with filtering.

resolvedResolved · Urgent Priority · Version 2003

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

Edited on Thu 17 Sep 2009, 15:13

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

Wed 23 Sep 2009: Automatically marked as resolved.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Return to the active cell after scrolling

When I scroll a long way down the screen from a selected cell, I can return to that cell with the Ctrl+Back Space shortcut. The active cell now appears in roughly the middle of the screen.

Shift+Back Space does something similar. Scroll down from the active cell and Shift+Back Space returns me to it and puts the active cell at the top of the screen; scroll up from the active cell and Shift+Back Space returns me to it and puts the active cell at the bottom of the screen.

Note also, that while Ctrl+Back Space will return me back to a selected range, Shift+Back Space only ever returns me to the active cell, which is normally at the top left-hand corner of any selected range.

View all Excel hints and tips


Server loaded in 0.07 secs.