Excel ADODB查询不检索日期
我面临着一些奇怪的Excel行为,我无法理解这一点 我有一份包含大量数据的工作表。为了在该表中进行搜索,我使用ADODB记录集和SQL查询 我的问题很简单:Excel ADODB查询不检索日期,excel,vba,adodb,Excel,Vba,Adodb,我面临着一些奇怪的Excel行为,我无法理解这一点 我有一份包含大量数据的工作表。为了在该表中进行搜索,我使用ADODB记录集和SQL查询 我的问题很简单: strSQL = "SELECT * FROM [PIRNotes$] WHERE [PIR] = '" & WS & "'" If rs.state = adStateOpen Then rs.Close rs.CursorLocation = adUseClient If cnn.state = adStateOpen
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)对应于工作表的第三列,其中包含表示日期的字符串。
所有其他列都可以很好地检索,其中的数据也是字符串
当我将第3列中的单元格格式化为日期并将内容转换为实际日期时,查询运行时不会出现问题
是什么导致了这种行为。我根本不知道从哪里开始解决这个问题。从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到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
Private子命令按钮1\u单击()
关于错误转到错误
Dim con作为新的ADODB连接
将strPath设置为字符串
Dim intImportRow作为整数
将sql设置为字符串
Dim strFirstName,strLastName作为字符串
'更改数据库的路径
strPath=“C:\your\u path\u here\demo\u db.accdb”
'打开与数据库的连接
如果符合第1条,则
con.ConnectionString=“Provider=Microsoft.ACE.OLEDB.12.0;数据源=“&strPath&;”
未结
如果结束
'如果选中复选框,则首先删除所有记录
如果选中复选框1,则
con.执行“从tbl_演示中删除”
如果结束
'设置要导入的记录的第一行
如果你愿意,你也可以在一个范围内循环。
intImportRow=3
直到单元格(intImportRow,4)为止
strFirstName=单元格(intImportRow,4)
strLastName=单元格(intImportRow,5)
strTheDate=单元格(intImportRow,6)
'将行插入数据库
con.执行“插入到tbl_演示(firstname、lastname、thedate)值(‘“&strFirstName&’,‘“&strLastName&’,‘“&strheDate&’)”
intImportRow=intImportRow+1
环
MsgBox“完成导出”,vbInformation
结案
设置con=Nothing
出口接头
呃:
MsgBox错误说明
端接头
我测试了两个代码样本。两个都做得很好。确保访问表中的数据类型为日期/时间。能否显示一些数据?您的代码对我来说运行良好,但我可能没有与您相同的数据。另一个想法/注释:为什么不使用Microsoft ACE OLEDB 12.0连接字符串,请参阅。经过多次尝试和错误,我现在发现问题在于工作表上数据的格式。数据已格式化为表。如果我删除这个(将数据格式化为一个范围),问题就消失了。从OP的连接字符串和问题中可以清楚地看出,他们正在从Excel中获取数据。