Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-05-05
8
Medium Priority
?
1,268 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
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
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!

 

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

876 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