2010-01-14 58 views
1

我有一个Excel表,我需要在我的Access数据库中导入。 该表如下:VBA中的ADODB记录集说excel字段为空时它不是

 
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

我然后利用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 

现在我的问题:某些字段在其中有文字。 字段值应该是item0001,但据报道为NULL

当字段有一个常规号码时,它可以正常工作。

奇怪的是:表格中还有其他文本字段,它们工作在FINE。

+0

如果您在Access中运行此代码,为什么不使用DoCmd.TransferSpreadsheet? – 2010-09-27 17:51:33

回答

4

请在您的扩展属性部分更具体(并且不要忽略内部引号)。

特别尝试Extended Properties="Excel 8.0;HDR=Yes;IMEX=1"以允许混合数字&文本数据。

更多详细信息在http://connectionstrings.com/

+0

这样做,谢谢。 – skerit 2010-01-14 18:16:43