ADODB recordset in VBA says excel field is empty when it's not

孤者浪人 提交于 2019-12-14 02:51:45

问题


I have an excel sheet I need to import in my Access database. The sheet looks like this:

DATE RECEPTION  DENOMINATION        ITEM N°     QUANTITE RECUE
06/01/2010  DVD-Sex & the City  PCR-PA21550167  5
06/01/2010  DVD-Avatar Natie 2  PCR-PA21550209  10

I then transfer this file into the database using adodb:


Dim rs2 As New ADODB.Recordset
Dim cnn2 As New ADODB.Connection
Dim cmd2 As New ADODB.Command
Dim intField As Integer
Dim strFile As String

strFile = fncOpenFile
If strFile = "" Then Exit Sub

With cnn2
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .ConnectionString = "Data Source=" & strFile& "; " & "Extended Properties=Excel 8.0"
    .Open
End With

Set cmd2.ActiveConnection = cnn2
cmd2.CommandType = adCmdText
cmd2.CommandText = "SELECT * FROM [PCR$]"
rs2.CursorLocation = adUseClient
rs2.CursorType = adOpenDynamic
rs2.LockType = adLockOptimistic

rs2.Open cmd2

While Not rs2.EOF
        strNaam = rs2.Fields(3).Value
Loop

Now my problem: certain fields have text in them. The field value should then be item0001, but it's reportedly NULL

When the field has a regular number it works fine.

The strange thing is: there are other text fields in the sheet and they work FINE.


回答1:


Be more specific in your Extended Properties portion (and don't omit inner quotes there).

In particular, try Extended Properties="Excel 8.0;HDR=Yes;IMEX=1" to allow mixed numbers&text data.

More details at http://connectionstrings.com/.



来源:https://stackoverflow.com/questions/2065789/adodb-recordset-in-vba-says-excel-field-is-empty-when-its-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!