Solved

Oracle SQL embedded in an Excel VB macro

Posted on 2008-10-06
3
1,529 Views
Last Modified: 2012-05-05
I posted a similat question on how to use date variables within Oracle SQL embedded in an Excel VB macro.

I did receive an answer (Thanks again)

I'm currently expanding the previous solution: I want to make all the parameters of the SQL statement variables. The VB creates a new linked query via ODBC connection (The connection is fine) and uses SQL which is derived from the variables (these get their calues from an excel control sheet called "Input")

I have the SQL statement, and it should work but Im getting an error at the FROM statement section.

--Compile error:

--Expected: List Separator or )

This pops up and "FROM is selected. I have tried all combinations of (, " etc and can't find the right syntax. I have a feeling that there might also be other bugs with the SQL syntax..

Can anyone check thorugh it quickly to fix the FROM statement and see if there are any other problems??

I really appreciate your help

Nathanael

SQL:

Sub cQuery_Auto()
   
    Application.ScreenUpdating = False

Dim sDate As String
Dim eDate As String

Dim SelectStatement As String
Dim CountStatament As String
Dim CaseStatement As String
Dim FromStatement As String
Dim WhereStatement As String
Dim StartDateField As String
Dim EndDateField As String
Dim GroupStatement As String

Sheets("Input").Select

sDate = Range("C43").Value
eDate = Range("C45").Value

SelectStatement = Range("C6").Value
FromStatement = Range("C15").Value
WhereStatement = Range("C30").Value
StartDateField = Range("C18").Value
EndDateField = Range("C21").Value
GroupStatement = Range("C36").Value

MsgBox ("WHERE (TblIfhEncounter.EpisodeEndDate Between {ts '" & sDate & "'} And {ts '" & eDate & "'}")

 Sheets("Query").Select

    With ActiveSheet.QueryTables.Add(Connection:= _
        "ODBC;DSN=InFocusDB;UID=hippoadmin;PWD=HIPPO21;APP=Microsoft® Query;WSID=001AA05195FB" _
        , Destination:=Range("B2"))
        .CommandText = Array( _
        "SELECT {'" & SelectStatement &"'}, Count({'" & CountStatement & "'}) AS Count & Chr(13) & "" & Chr(10) & "FROM" & "{'" & FromStatement & "'}" & Chr(13) & "" & Chr(10) & "WHERE ({ '" & WhereStatement & "'}) AND ({ '" & StartDateField & "'} >={ts '" & sDate & "'}) And ({ '" & EndDateField & "'}<={ts '" & eDate & "'})" & Chr(13) & "" & Chr(10) & "GROUP BY {'" & GroupStatement & "'} ")
        .Name = "cQuery_Auto"
        .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
    Application.WindowState = xlMinimized
    Application.WindowState = xlNormal
   
    Application.ScreenUpdating = False
     
End Sub


it should be a simple case of an extra " somewhere ... but I'm not sure where.

Hope you can help

Thanks

Nathanael
0
Comment
Question by:natdacruz
  • 2
3 Comments
 
LVL 8

Expert Comment

by:Leo Eikelman
Comment Utility
Try putting an extra space after your FORM string.  So currently it's

(10) & "FROM" & "{'" & FromStatement

Change to:

10) & "FROM " & "{'" & FromStatement
0
 
LVL 8

Accepted Solution

by:
Leo Eikelman earned 500 total points
Comment Utility
Also, did you mean to specify the CommandText as an Array?

     .CommandText = Array( _
        "SELECT {'" & SelectStatement &"'}, Count({'" & CountStatement & "'}) AS Count & Chr(13) & "" & Chr(10) & "FROM" & "{'" & FromStatement & "'}" & Chr(13) & "" & Chr(10) & "WHERE ({ '" & WhereStatement & "'}) AND ({ '" & StartDateField & "'} >={ts '" & sDate & "'}) And ({ '" & EndDateField & "'}<={ts '" & eDate & "'})" & Chr(13) & "" & Chr(10) & "GROUP BY {'" & GroupStatement & "'} ")

I believe this property just expects a string.
0
 

Author Comment

by:natdacruz
Comment Utility
Hi Thanks Leikelman
I have fixed the From problem I needed an extra " and a space as you said .. now I have a new error.
 
Type Mismatch
This must be a problem with the variables amd the Oracle settings for each variable type. I know that the date variables are fine as I have run the SQL with them before. I am assuming that one of the other variables shouls be DIM something other than string??

heets("Input").Select
sDate = Range("C43").Value
eDate = Range("C45").Value
SelectStatement = Range("C6").Value
CountStatement = Range("C12").Value
FromStatement = Range("C15").Value
StartDateField = Range("C18").Value
EndDateField = Range("C21").Value
WhereStatement = Range("C30").Value
GroupStatement = Range("C36").Value
MsgBox ("WHERE (TblIfhEncounter.EpisodeEndDate Between {ts '" & sDate & "'} And {ts '" & eDate & "'}")
MsgBox ("SELECT {'" & SelectStatement & "'},")
MsgBox ("Count({'" & CountStatement & "'}) AS Count & Chr(13) & "" & Chr(10) & ")
MsgBox (" FROM " & "{'" & FromStatement & "'}" & Chr(13) & "" & Chr(10) & "")
MsgBox ("WHERE ({ '" & WhereStatement & "'}) AND ({ '" & StartDateField & "'} >={ts '" & sDate & "'}) And ({ '" & EndDateField & "'}<={ts '" & eDate & "'})" & Chr(13) & "" & Chr(10) & "")
MsgBox ("SELECT {'" & SelectStatement & "'}, Count({'" & CountStatement & "'}) AS Count & Chr(13) & "" & Chr(10) & "" FROM " & "{'" & FromStatement & "'}" & Chr(13) & "" & Chr(10) & "WHERE ({ '" & WhereStatement & "'}) AND ({ '" & StartDateField & "'} >={ts '" & sDate & "'}) And ({ '" & EndDateField & "'}<={ts '" & eDate & "'})" & Chr(13) & "" & Chr(10) & "GROUP BY {'" & GroupStatement & "'} ")
Sheets("Query").Select
    With ActiveSheet.QueryTables.Add(Connection:= _
        "ODBC;DSN=InFocusDB;UID=hippoadmin;PWD=HIPPO21;APP=Microsoft® Query;WSID=001AA05195FB" _
        , Destination:=Range("B2"))
        .CommandText = Array( _
        "SELECT {'" & SelectStatement & "'}, Count({'" & CountStatement & "'}) AS Count & Chr(13) & "" & Chr(10) & "" FROM " & "{'" & FromStatement & "'}" & Chr(13) & "" & Chr(10) & "WHERE ({ '" & WhereStatement & "'}) AND ({ '" & StartDateField & "'} >={ts '" & sDate & "'}) And ({ '" & EndDateField & "'}<={ts '" & eDate & "'})" & Chr(13) & "" & Chr(10) & "GROUP BY {'" & GroupStatement & "'} ")
        .Name = "cQuery_Auto"
        .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
    Application.WindowState = xlMinimized
    Application.WindowState = xlNormal
   
    Application.ScreenUpdating = False
     
End Sub
 
 
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now