【问题标题】:ADODB query does not retrieve datesADODB 查询不检索日期
【发布时间】:2019-02-26 22:38:44
【问题描述】:

我遇到了一些奇怪的 Excel 行为,我无法理解这一点。

我有一个包含大量数据的工作表。为了在这张表中进行搜索,我使用 ADODB 记录集和 SQL 查询。

我的查询很简单:

strSQL = "SELECT * FROM [PIRNotes$] WHERE [PIR] = '" & WS & "'"
If rs.state = adStateOpen Then rs.Close
rs.CursorLocation = adUseClient
If cnn.state = adStateOpen Then cnn.Close
cnn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & _
        ActiveWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name
cnn.Open

rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic

我遇到的问题是,无论 WS 的值是多少,无论找到多少条记录,每条记录总是有 rs.Fields(2) = Null。

rs.Fields(2) 对应于工作表的第三列,其中包含一个表示日期的字符串。 所有其他列都可以正常检索,那里的数据也是字符串。

当我将第三列中的单元格格式化为日期并将内容转换为实际日期时,查询运行没有问题。

什么可能导致这种行为。我完全不知道从哪里开始解决这个问题。

【问题讨论】:

  • 你能展示一些你的数据吗?您的代码对我来说很好,但我可能没有与您相同的数据。
  • 一个额外的想法/评论:你为什么不使用 Microsoft ACE OLEDB 12.0 连接字符串,请参阅here
  • 经过多次尝试和错误,我现在发现问题是我在工作表上的数据格式。数据被格式化为表格。如果我删除它(将数据格式化为范围),问题就消失了。

标签: excel vba adodb


【解决方案1】:

从 Excel 到 Access!!

以下脚本在 Access 中运行。

Private Sub Command0_Click()

Dim strPathFile As String, strFile As String, strPath As String
Dim blnHasFieldNames As Boolean
Dim intWorksheets As Integer

' Replace 3 with the number of worksheets to be imported
' from each EXCEL file
Dim strWorksheets(1 To 3) As String

' Replace 3 with the number of worksheets to be imported
' from each EXCEL file (this code assumes that each worksheet
' with the same name is being imported into a separate table
' for that specific worksheet name)
Dim strTables(1 To 3) As String

' Replace generic worksheet names with the real worksheet names;
' add / delete code lines so that there is one code line for
' each worksheet that is to be imported from each workbook file
strWorksheets(1) = "Sheet1"
'strWorksheets(2) = "csco"
'strWorksheets(3) = "sbux"

' Replace generic table names with the real table names;
' add / delete code lines so that there is one code line for
' each worksheet that is to be imported from each workbook file
strTables(1) = "TableName1"
strTables(2) = "TableName2"
strTables(3) = "TableName3"

' Change this next line to True if the first row in EXCEL worksheet
' has field names
blnHasFieldNames = True

' contains the EXCEL files
strPath = "C:\your_path_here\"

' Replace 3 with the number of worksheets to be imported
' from each EXCEL file
For intWorksheets = 1 To 1

      strFile = Dir(strPath & "*.xls")
      Do While Len(strFile) > 0
            strPathFile = strPath & strFile
            DoCmd.TransferSpreadsheet acImport, _
                  acSpreadsheetTypeExcel9, strTables(intWorksheets), _
                  strPathFile, blnHasFieldNames, _
                  strWorksheets(intWorksheets) & "$"
            strFile = Dir()
      Loop

Next intWorksheets

End Sub

这将从文件夹中的多个 Excel 文件中导入第一张工作表。如果您只想从一个 Excel 文件中导入数据,请去掉循环 (For intWorksheets = 1 To 1)。

现在,如果你想从 Excel 运行代码并从 Access 推送数据,你可以这样做。

Private Sub CommandButton1_Click()
On Error GoTo errH

    Dim con As New ADODB.Connection
    Dim strPath As String
    Dim intImportRow As Integer
    Dim sql As String
    Dim strFirstName, strLastName As String

    'CHANGE PATH TO DATABASE
    strPath = "C:\your_path_here\demo_db.accdb"

    'open the connection to the database
    If con.State <> 1 Then

        con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strPath & ";"

        con.Open

    End If

    'delete all records first if checkbox checked
    If CheckBox1 Then
        con.Execute "delete from tbl_demo"
    End If

    'set first row with records to import
    'you could also just loop thru a range if you want.
    intImportRow = 3

    Do Until Cells(intImportRow, 4) = ""
        strFirstName = Cells(intImportRow, 4)
        strLastName = Cells(intImportRow, 5)
        strTheDate = Cells(intImportRow, 6)

        'insert row into database
        con.Execute "insert into tbl_demo (firstname, lastname, thedate) values ('" & strFirstName & "', '" & strLastName & "', '" & strTheDate & "')"

        intImportRow = intImportRow + 1
    Loop


    MsgBox "Done Exporting", vbInformation

    con.Close
    Set con = Nothing

Exit Sub

errH:
    MsgBox Err.Description
End Sub

我测试了两个代码示例。两者都工作得很好。确保 Access 表中的数据类型是日期/时间。

【讨论】:

  • 从 OP 的连接字符串和他们正在从 Excel 获取数据的问题中都可以清楚地看到。
猜你喜欢
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多