Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Oracle SQL embedded in an Excel VB macro

Posted on 2008-10-06
3
Medium Priority
?
1,545 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
ID: 22653133
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 1500 total points
ID: 22653215
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
ID: 22663145
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Suggested Courses

782 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