Solved

Oracle SQL embedded in an Excel VB macro

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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
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.

895 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

15 Experts available now in Live!

Get 1:1 Help Now