Results 1 to 6 of 6

Thread: VBA SQL Query shuts down Excel

  1. #1

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    34

    VBA SQL Query shuts down Excel

    Have the following code. When I run this the first query works and the second one shuts down Excel. When I step into this, it does it's dirty deed on the SQLExecQuery line.

    Any ideas out there?

    Dean

    VB Code:
    1. Sub UsingSQLOpen()
    2.  
    3. Dim Chan As Variant
    4. Dim DataDate As String
    5. DataDate = Range("sheet2!B3")
    6.     Chan = SQLOpen("DSN=SMP_DB;UID=sa;PWD=suprsonic")
    7.     SQLExecQuery Chan, "SELECT * FROM InventoryTotalsHistory Where " & _
    8.                     "ith_dtBusinessDate Between " & DataDate & ""
    9.     SQLRetrieve Chan, Range("sheet2!D2")
    10.     SQLClose Chan
    11.    
    12.     Chan = SQLOpen("DSN=SMP_DB;UID=sa;PWD=suprsonic")
    13.     SQLExecQuery Chan, "SELECT * FROM InventoryFoodCostHistory Where " & _
    14.                     "ifc_dtBusinessDate Between " & DataDate & ""
    15.     SQLRetrieve Chan, Range("sheet2!AD2")
    16.     SQLClose Chan
    17. End Sub
    Last edited by RobDog888; May 22nd, 2006 at 03:40 PM. Reason: Added [vbcode] tags

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

    Re: VBA SQL Query shuts down Excel

    First you need to add some error handling so you can determine what the error message is.

    VB Code:
    1. Sub UsingSQLOpen()
    2.     On Error GoTo MyError
    3.  
    4.     Dim Chan As Variant 'Shouldnt be declared as a variant but a ADODB.Connection object (I think your using ADO?)
    5.     Dim DataDate As String
    6.     DataDate = Range("sheet2!B3")
    7.     Chan = SQLOpen("DSN=SMP_DB;UID=sa;PWD=suprsonic")
    8.     SQLExecQuery Chan, "SELECT * FROM InventoryTotalsHistory Where " & _
    9.                     "ith_dtBusinessDate Between " & DataDate & ""
    10.     SQLRetrieve Chan, Range("sheet2!D2")
    11.     SQLClose Chan
    12.    
    13.     Chan = SQLOpen("DSN=SMP_DB;UID=sa;PWD=suprsonic")
    14.     SQLExecQuery Chan, "SELECT * FROM InventoryFoodCostHistory Where " & _
    15.                     "ifc_dtBusinessDate Between " & DataDate & ""
    16.     SQLRetrieve Chan, Range("sheet2!AD2")
    17.     SQLClose Chan
    18.     Exit Sub
    19. MyError:
    20.     MsgBox Err.Number & " - " & Err.description
    21. End Sub
    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

  3. #3

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    34

    Re: VBA SQL Query shuts down Excel

    Have changed the code to this. Get an error stating the dim chan as adodb.connection is not defined. I have the odbc addin installed???

    VB Code:
    1. Sub UsingSQLOpen()
    2.     On Error GoTo MyError
    3.  
    4.     Dim Chan As ADODB.Connection
    5.     Dim DataDate As String
    6.     DataDate = Range("sheet2!B3")
    7.     Chan = SQLOpen("DSN=SMP_DB;UID=sa;PWD=suprsonic")
    8.     SQLExecQuery Chan, "SELECT * FROM InventoryTotalsHistory Where " & _
    9.                     "ith_dtBusinessDate Between " & DataDate & ""
    10.     SQLRetrieve Chan, Range("sheet2!D2")
    11.     SQLClose Chan
    12.    
    13.     Chan = SQLOpen("DSN=SMP_DB;UID=sa;PWD=suprsonic")
    14.     SQLExecQuery Chan, "SELECT * FROM InventoryFoodCostHistory Where " & _
    15.                     "ifc_dtBusinessDate Between " & DataDate & ""
    16.     SQLRetrieve Chan, Range("sheet2!AD2")
    17.     SQLClose Chan
    18.     Exit Sub
    19. MyError:
    20.     MsgBox Err.Number & " - " & Err.Description
    21. End Sub
    Last edited by RobDog888; May 22nd, 2006 at 10:23 PM. Reason: Added [vbcode] tags

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

    Re: VBA SQL Query shuts down Excel

    Please use the [Highlight=VB] tags as it makes it easier to read code.

    What was the error message? I havent seen any Add-In for ADO so you should make sure you have a reference set to "Microsoft ActiveX Data Objects 2.x Library" as that is probably the error since you say it stops at that line.
    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

  5. #5

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    34

    Re: VBA SQL Query shuts down Excel

    Ok on the vb tags, sorry I'm a newbie...

    The addin is the odbc addin. As far as I know, it has to be installed to use the adodb functions.

    Thanks for your help.

    Dean

  6. #6

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    34

    Re: VBA SQL Query shuts down Excel

    Here is the latest code. Now I get an error "91 - Object variable or With block variable not set" when I run this line of code [Chan = SQLOpen("DSN=SMP_DB;UID=sa;PWD=suprsonic")]. Not sure what I'm missing?

    Sub UsingSQLOpen()
    On Error GoTo MyError

    Dim Chan As ADODB.Connection
    Dim DataDate As String
    DataDate = Range("sheet2!B3")
    Chan = SQLOpen("DSN=SMP_DB;UID=sa;PWD=suprsonic")
    SQLExecQuery Chan, "SELECT * FROM InventoryTotalsHistory Where " & _
    "ith_dtBusinessDate Between " & DataDate & ""
    SQLRetrieve Chan, Range("sheet2!D2")
    SQLClose Chan

    Chan = SQLOpen("DSN=SMP_DB;UID=sa;PWD=suprsonic")
    SQLExecQuery Chan, "SELECT * FROM InventoryFoodCostHistory Where " & _
    "ifc_dtBusinessDate Between " & DataDate & ""
    SQLRetrieve Chan, Range("sheet2!AD2")
    SQLClose Chan
    Exit Sub
    MyError:
    MsgBox Err.Number & " - " & Err.Description
    End Sub

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