We help IT Professionals succeed at work.
Get Started

object required error

ktjamms2 asked
Last Modified: 2012-05-09
I am trying to write a VB script in excel that will query some tables and return the results in a spreadsheet. Before I've used an access query and imported it into excel as a database query. Now, I'm using SQL Enterprise Manager and don't know of a way to import a query from the query analyzer, so I wrote this VB script trying to connect to my tables and import the data that way. I'm getting an error that says object required. Here is my script:

Sub CompareQry()

' Declare the QueryTable object
Dim qt As QueryTable

' Declare database varaiables
Dim SQLDB           As String
Dim SQLServer       As String
Dim SQLDbase        As String
Dim SQLUser         As String
Dim SQLPword        As String

Open App.Path & "\SQLConnect.txt" For Input As #1
Input #1, SQLServer, SQLDbase, SQLUser, SQLPword
Close #1

' Set up the SQL Statement
sqlstring = "SELECT a.item,a.qty,a.strnum,b.item as itemb, b.qty as qtyb,b.strnum " _
& "FROM     firsttbl a " _
& "inner join [server1].STORE.dbo.tablename b " _
& "on a.strNum = b.strnum and " _
& "a.Item = b.Item " _
& "and a.qty <> b.qty " _
& "Where a.strNum = '09'  order by a.item"

' Set up the connection string
connstring = "Provider=sqloledb;Server=" & SQLServer & ";User Id=" & SQLUser & ";Pwd=" & SQLPword & ";Database=" & SQLDbase

' Implement the connection, run the query, and add
' the results to the spreadsheet starting at row A1
With ActiveSheet.qt.Add(Connection:=connstring, Destination:=Range("A1"), Sql:=sqlstring)
 End With

End Sub

Thanks for all of your help!

Watch Question
Expert of the Quarter 2010
Expert of the Year 2010
This problem has been solved!
Unlock 4 Answers and 31 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE