|
-
May 22nd, 2006, 03:07 PM
#1
Thread Starter
Member
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:
Sub UsingSQLOpen()
Dim Chan As Variant
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
End Sub
Last edited by RobDog888; May 22nd, 2006 at 03:40 PM.
Reason: Added [vbcode] tags
-
May 22nd, 2006, 03:43 PM
#2
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:
Sub UsingSQLOpen()
On Error GoTo MyError
Dim Chan As Variant 'Shouldnt be declared as a variant but a ADODB.Connection object (I think your using ADO?)
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
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 22nd, 2006, 07:05 PM
#3
Thread Starter
Member
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:
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
Last edited by RobDog888; May 22nd, 2006 at 10:23 PM.
Reason: Added [vbcode] tags
-
May 22nd, 2006, 10:25 PM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 23rd, 2006, 09:32 AM
#5
Thread Starter
Member
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
-
May 23rd, 2006, 10:10 AM
#6
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|