Solved

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

Posted on 2009-05-05
8
1,224 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
  • 4
  • 4
8 Comments
 
LVL 85

Expert Comment

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

Author Comment

by:KentDBerry
Comment Utility
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
Comment Utility
What does the returned connection string look like? It should start with "ODBC;" or "OLEDB;"
 
0
 

Author Comment

by:KentDBerry
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks again for your help!!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
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 demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now