I’m trying to import an excel file Excel97-2003 (the first sheet only) into an Access database. for that I’m using the following code :
Dim cnnExcel As New ADODB.Connection
Dim rsExcel As New ADODB.Recordset
With cnnExcel
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" & strFileSelected & ";" & "Extended Properties=Excel 8.0"
.CursorLocation = adUseClient
.Open
End With
rsExcel.Open "SELECT * FROM [Sheet1]", cnnExcel
All the columns in the excel file are string and have type “General” (no special format) when you open it with Excel2003) and the 3 other columns are dates.
My problem is I get null for value which exist in the cell.For exemple if the first value is string than if there’s an integer in that columns it will be returned as null and vice-versa
Is there any solution for this?
Thank you
Solution 1
Using your code, I was able to properly load data from an Excel Sheet without problem.
However, please check your SQL Query, it should be something like:
The rules for the
FROMpart as as follow:SELECT * FROM [SheetName$], note the$SELECT * FROM [SheetName$A1:C5]SELECT * FROM NameRangeSELECT * FROM ['This;is.My SheetName$']Code working on my machine:
Solution 2
You may have better luck with manipulating the Excel workbook directly.
Let’s assume that you have a table
MyTablein your Access database where you want to import in the fieldsmyA,myBandmyC(that have the proper datatype you expect!) the content of your ExcelSheet 1that has corresponding columns.The simplified VBA code would look like this: