VBA 中的 ADODB 记录集显示 excel 字段为空,但实际上不是空的

发布于 2024-08-17 11:59:11 字数 1065 浏览 6 评论 0原文

我有一个 Excel 工作表,需要导入到我的 Access 数据库中。 该工作表如下所示:

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

当字段具有常规数字时,它可以正常工作。

奇怪的是:工作表中还有其他文本字段,并且它们工作正常。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や莫失莫忘 2024-08-24 11:59:11

在扩展属性部分中更加具体(并且不要省略内部引号)。

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

更多详细信息请访问 http://connectionstrings.com/

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/.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文