LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default SQL query syntax

I try to run an SQL query in VBA code, to perform a query on an Excel
table and have the result added as an Excel table in the same sheet (to
range j1). (see code below) The SELECT statement comes into the
variable strQuery.

Everything works fine if I use the Microsoft Query syntax for the
query.

Eg in a simplified version that syntax would be:

(1) SELECT ... FROM ... WHERE Name="An" And Age="26" Or Name="John" and
Age="26"

But it doesn't work with the following statement, which in SQL means
the same:

(2) SELECT ... FROM ... WHERE (Name="An" Or Name="John") and Age="26"

If I go into the Microsoft Query program and I enter manually query (2)
in the SQL editor, it translates it automatically into query (1), so
there query (2) does work.
However, in my VBA code I assign the query (2) to strQuery, and then I
get an error message. As I said, I don't get this error message with
query (1). It seems in the VBA code I can only assign the (1) syntax to
strQuery, because the code doesn't know how to handle syntax (2).

Now you may say, why don't you just work with a query syntax like (1)?
Because with much more complicated queries, queries like (2) are much
shorter and easy to build.

Anyone knows how to solve this problem?

With
ActiveSheet.QueryTables.Add(Connection:=Array(Arra y("ODBC;DSN=Excel-bestanden;DBQ=C:\test.xls;DefaultDir=C:"),
;DriverId=790;MaxBufferSize=2048;PageTimeout=5;")) ,
Destination:=Range("j1"))
strQuery = ... '
here comes the SELECT statement
.CommandText = Array(strQuery)
.Name = "Query1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
MS Query Oracle syntax Jeff[_3_] Excel Discussion (Misc queries) 3 May 12th 07 03:06 AM
MS Query Oracle syntax Jeff[_3_] Excel Discussion (Misc queries) 0 May 11th 07 09:26 PM
Microsoft Query syntax Krish Excel Discussion (Misc queries) 0 October 7th 05 02:33 PM
MS Query - Alias syntax David P Excel Worksheet Functions 3 February 16th 05 09:29 AM
DB query syntax Mike Fogleman Excel Programming 5 December 19th 03 09:42 AM


All times are GMT +1. The time now is 05:42 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"