?
Solved

Oracle SQL embedded in an Excel VB macro

Posted on 2008-10-06
3
Medium Priority
?
1,542 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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.
Suggested Courses

718 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