Results 1 to 8 of 8

Thread: Returning SQL to Excel

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    32

    Question Returning SQL to Excel

    I've got a lengthy piece of SQL which I need to be able to run and return to excel. With this sort of thing I normally cheat and use the record function to create a background query which I can then add my variables into etc but because this SQL statement is so long it's just not playing ball

    I think this is due to the sql statement being formed in an array in VB which is limited to certain parameters(?)

    Can anyone offer any advice on how this can be done?

    Any advice much appreciated...

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Returning SQL to Excel

    Excel aside for a moment. Once your query is done running from VB, do you get a successful recordset created?

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Returning SQL to Excel

    What DB are you using SQL Server, Access, MySQL, etc...
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    32

    Re: Returning SQL to Excel

    Right Ok - So this is an Oracle Database (PVEC) which I can happily run my script in SQL Navigator and alike....

    When I go down the path of creating a background query, ie Data > Import external Data > New Database query - I can create the query through MS Query and successfully return the data to Excel.

    My problem is that once I've recorded a macro of my actions, the SQL shows up as an errored array (in red) and the SQL gets chopped short, presumably because it uses an array to hold the SQL statement and this must have some form of character limitation? (I'm just guessing here)...

    So, I can create a background query that I can refresh, however I need a variable in this SQL statement and so need a way of amending the SQL prior to execution......

  5. #5
    Lively Member
    Join Date
    May 2005
    Location
    derby, uk
    Posts
    83

    Re: Returning SQL to Excel

    it sounds like the sql query string is to long, hence showing up in red on the following line.

    you could try building the query string in bits (also watch out for quote marks)

    thus

    VB Code:
    1. mysql = "extreamely long string "withquotemarks" in it in annoying places"

    becomes

    VB Code:
    1. mysql = "extreamely long string 'withquotemarks'"
    2. mysql = mysql & " in it in annoying places"

    to build it in smaller chunks...

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    32

    Re: Returning SQL to Excel

    Ok so I tried this method to get to here;

    VB Code:
    1. l
    2.  
    3. mysql = "select ...........  lots of stuff         "
    4. mysql = mysql & " from someplace"
    5. mysql = mysql & "where some things equal others..."
    6. mysql = mysql & "and myvariable = 'whatitellitto'.... "
    7.  
    8. With ActiveSheet.QueryTables.Add(Connection:= _
    9.         "ODBC;DSN=PVEC;UID=username;PWD=password;SERVER=PVEC;", Destination:=Range("A1" _
    10.         ))
    11.         .CommandText = Array( _
    12.         "" & mysql)
    13.         .Name = "Query from PVEC_1"
    14.         .FieldNames = True
    15.         .RowNumbers = False
    16.         .FillAdjacentFormulas = False
    17.         .PreserveFormatting = True
    18.         .RefreshOnFileOpen = False
    19.         .BackgroundQuery = True
    20.         .RefreshStyle = xlInsertDeleteCells
    21.         .SavePassword = True
    22.         .SaveData = True
    23.         .AdjustColumnWidth = True
    24.         .RefreshPeriod = 0
    25.         .PreserveColumnInfo = True
    26.         .Refresh BackgroundQuery:=False
    27.     End With

    But I just get a runtime error 13 when the code reaches the & mysql point...

    in the above, mysql ends up 1794 characters long if this helps!!??

    Any other Ideas?

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Returning SQL to Excel

    Did you dim mysql as a string variable?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    Re: Returning SQL to Excel

    Does the .commandText as to be an array?
    Can't you just say .commandText= mysql ?

    I don't know, I'm just guessing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width