ADODB查询不检索日期

时间:2019-02-25 20:05:46

标签: excel vba adodb

我面临一些奇怪的Excel行为,我无法解决这个问题。

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

我的查询非常简单:

print(hit) # (['14', 'THE UNIVERSITY OF TOKYO '], 100)
print(hit[0]) # ['14', 'THE UNIVERSITY OF TOKYO ']
print(hit[0][0]) # 14

我遇到的问题是,无论WS的价值如何,无论找到多少记录,每个记录始终都具有rs.Fields(2)= Null。

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

当我将第三列中的单元格格式化为日期并将内容转换为真实日期时,查询运行不会出现问题。

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

1 个答案:

答案 0 :(得分:-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

enter image description here

enter image description here

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