Solved

Using VB.Net 2005 to drive an Excel 2007 QueryTable problem

Posted on 2009-05-05
8
1,249 Views
Last Modified: 2013-11-10
The below VB.Net 2005 code errors out on the line

        qryTable = oSheet.QueryTables.Add(conn, oSheet.Range("A1"), "Select * from tblActiveIngrediantAnalysis")

with the below error message.  Please help to resolve.  Thanks.

System.ArgumentException was unhandled
  Message="The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))"

Dim oExcel As New Excel.Application
        Dim oSheet As Excel.Worksheet
        oExcel.Workbooks.Add()
        oSheet = oExcel.ActiveSheet
 
        Dim conn As New SqlConnection(My.Settings.BRDWorklsates)
        conn.Open()
        Dim qryTable As Excel.QueryTable
        qryTable = oSheet.QueryTables.Add(conn, oSheet.Range("A1"), "Select * from tblActiveIngrediantAnalysis")
        With qryTable
            .RefreshStyle = XlCellInsertionMode.xlInsertEntireRows
            .Refresh(False)
        End With

Open in new window

0
Comment
Question by:KentDBerry
[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
  • 4
  • 4
8 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24311804
The first parameter should be a connection string, not a Connection object.
Regards,
Rory
0
 

Author Comment

by:KentDBerry
ID: 24315274
Thanks Rory.  Now I have a subsequent error:

"Exception from HRESULT: 0x800A03EC"

When I try:
qryTable = oSheet.QueryTables.Add(conn.ConnectionString, oExcel.ActiveSheet.Range("A1"))

or

qryTable = oSheet.QueryTables.Add(conn.ConnectionString, oExcel.ActiveSheet.Range("A1"), "Select * from tblActiveIngrediantAnalysis")


Thoughts?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24316141
What does the returned connection string look like? It should start with "ODBC;" or "OLEDB;"
 
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:KentDBerry
ID: 24318663
Well, it was this:

conn.ConnectionString = "Data Source=usgrrd3;Initial Catalog=BRDWorkslates;Integrated Security=True"

So I changed it to

Dim conn As New OleDb.OleDbConnection(My.Settings.OLEDB_Connection)
and conn.ConnectionString now equals
"Provider=SQLOLEDB.1;Data Source=USGRRD3;Integrated Security=SSPI;Initial Catalog=BRDWorkslates"

I still get the same error.  I'm trying to directly access a SQL Server.  Am I using the wrong approach.  Basically I'm wanting to quickly dump the results of 15,000 records from a SQL Server 2005 into Excel 2007 using VB.Net 2005


0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 24319427
Your connection string would need to be:
"OLEDB;Provider=SQLOLEDB.1;Data Source=USGRRD3;Integrated Security=SSPI;Initial Catalog=BRDWorkslates"

0
 

Author Comment

by:KentDBerry
ID: 24319928
That now works, Thanks.  Is there a way to add that OLEDB via the Project Settings form, so that I would not have had to append it as shown below?

qryTable = oSheet.QueryTables.Add("OLEDB; " & conn.ConnectionString, oExcel.ActiveSheet.Range("A1", "A1"), "Select * from tblActiveIngrediantAnalysis")
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24320006
Not that I know of, no. The prefix depends entirely on the type of connection - whether it's OLEDB, ODBC, web etc.
0
 

Author Closing Comment

by:KentDBerry
ID: 31578276
Thanks again for your help!!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

740 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