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

problems sql statements vba

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.

ResolvedVersion 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:

Autofit column width – Excel (all versions)

a. Highlight the column or columns you wish to alter the width of. You do this by clicking on the grey button at the top of the column showing the column letter. Click and drag on these letters to select more than one column.
b. Double click the dividing line between the columns. This dividing line is the break between the columns on the column headers (grey buttons showing the column letter at the top of each column). When you hover your mouse over one of these dividing lines the point will change and show an arrow pulling a line in two directions. When you have this mouse pointer you should double click to get Excel to automatically set the column width to fit the contents of the column (autofit)

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.