Solved

Oracle SQL embedded in an Excel VB macro

Posted on 2008-10-06
3
1,533 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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
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.
This video shows how to recover a database from a user managed backup

803 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