index > Visual Basic for Applications (VBA) > OleDbConnection to Excel

OleDbConnection to Excel


Hi,

I'm having a little trouble reading from a excel file into a datatable using the OledbConnection and OleDbDataAdpter. I get an error when calling the OleDbConnection.Open() method (""External table is not in the expected format") and the excel workbook I'm referencing isn't open. I dont get this error if the file is open. Am I doing something wrong or does the excel file need to be open for the connection to work? Thanks in advance!

Code:

Private Function getSpreadSheetData(ByVal xlFileName As String) As DataTable

Dim xlConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & xlFileName & ";" & "Extended Properties=""Excel 8.0;HDR=Yes;"""

Dim xlconn As New OleDbConnection(xlConnString)

Dim xlda As New OleDbDataAdapter("select distinct G, J, M, N from [Arb Deals$] where E = 'USD'", xlconn)

Dim xldt As New DataTable

Try

xlconn.Open()

xlda.Fill(xldt)

Catch ex As Exception

MsgBox(ex.Message)

Throw New Exception(ex.Message, ex.InnerException)

Finally

xlconn.Close()

End Try

Return xldt

End Function

JeffJ

Hi,

Could be a couple of things and it depends on your data.... Unless you have a heading row in row 1 that contains the field names then you don't need HDR=Yes in the connection string. It looks like you don't have this row as your referencing the columns using A, F, etc.. but I could be wrong.

let me know if that fixes things as there could be other problems. Also it would be good to post an example of your data as it could determine a few things with your connection strings and your select statement. It can be dummy data but seeing the structure of your data would be good.




www.dsmyth.net/blog
Derek Smyth

What happens if you remove the double-quotes from around the extended properties in the connection string? I've got very similar code right now and it seems to be working fine.




a little trolley is a dangerous thing for example you could trip over it
duck thing
reply 3

You can use google to search for other answers

 

More Articles

• Personal.xls
• How to convert excel file to .DAT file?
• whats wrong with this code?
• converting text to Hours Minutes and Seconds
• DLL/OCX Redirection from Word template
• wrong count of Records in Access.Form
• VBA Excel - Using FindNext function
• Excel macro not able to SetSourceData for chart
• Problem regarding VBA in excel - Combine 52 VBA codes for 52 week...
• form-Subform
Welcome to Bokebb   New Update   Joins the collection  
 

New Articles

• Referencing unregistered DLL
• xlDialogPasteSpecial
• copying links from specific cells in a w
• oledb connaction display xml
• Date Problem
• I dont know whether you have come across
• Distributing a Macro across an organizat
• SetSourcedata of pivotchart on a pivot t
• Openning Excel from Access 97
• Exporting a table from an Access File to
• VBA SourceName method
• Novice VBA user trying to consolidate in
• Error 3251 using ADO & VBA to drop a
• Word 2000 section page numbering bug? An
• File Protection from Users

Hot Articles

• 'Call was rejected by callee' using VBA
• Function which get range arg.
• Sorting Subreport
• [Access 2000] Error when importing text
• Microsoft Document Image Printer - MODI
• Access 2003 - programming arrow keys wit
• MsForms.TextBox refresh function
• Access Project and Views
• reportbuilder
• Select query returns non-existing data
• Trying to create Data Acess Pages to man
• Adding minimize button to userform in de
• Welcome to the VBA discussion thread!
• Adding an Outlook Feature
• fast search

Recommend Articles

• combine 3 combo boxes values into one st
• Table or Array in the header of an excel
• How to create a hyperlink in Excel Chart.
• Why Excel 2003 keeps PivotItems with zer
• Challenge writing a macro for setting Me
• Check diff
• InputBox() in MS Word form textbox.
• Word Macro and Template File
• MS Word Option Button Collection
• Outlook experienced a serious error: 'mi
• How can I change the font of some of the
• S.O.S Sending Escape Sequence to printer
• Can a field result in a Query be formatt
• VBA Help File throws Internet Explorer S
• help me