我目前使用的设置如下所示。
Set AccessConn = CreateObject("ADODB.Connection")
sExcel = "[Excel 8.0;HDR=YES;IMEX=2;DATABASE=" & ThisWorkbook.FullName & "].[Sheet1$]"
AccessConn.Open "DSN=Foo", "", ""
sSQL = "Insert Into BarTable Select * FROM " + sExcel
AccessConn.Execute sSQL
现在,如果我想在导出中添加now()列,这可能吗?
之类的东西Set AccessConn = CreateObject("ADODB.Connection")
sExcel = "[Excel 8.0;HDR=YES;IMEX=2;DATABASE=" & ThisWorkbook.FullName & "].[Sheet1$]"
AccessConn.Open "DSN=Foo", "", ""
sSQL = "Insert Into BarTable Select *,NOW() FROM " + sExcel
AccessConn.Execute sSQL
BarTable中的最后一列是日期/时间列,我想将当前时间插入其中?
答案 0 :(得分:0)
在SQL中输入日期,您可能需要将其转换为文本 - Access本身可以处理'Now()'但不确定它是否可以在传递SQL字符串时。像下面这样的东西应该这样做。
sSQL = "Insert Into BarTable Select *,#" & format(NOW(),"MM/DD/YYYY hh:mm:ss") & "# As DateTimeStamp FROM " + sExcel
在Access中我有两个函数用于在构建SQL字符串时获取日期或日期和时间戳,我还没有测试但是它们应该转换为Excel VBA好了:
Function MakeSQLDateTime(pSourceDate As Variant) As String
Dim SQLDay As String, SQLMonth As String, SQLYear As String, SQLTime As String
SQLDay = Day(pSourceDate)
SQLMonth = Month(pSourceDate)
SQLYear = Right(Year(pSourceDate), 4)
SQLTime = Hour(pSourceDate) & ":" & Minute(pSourceDate) & ":" & Second(pSourceDate)
MakeSQLDateTime = "#" & SQLMonth & "/" & SQLDay & "/" & SQLYear & " " & SQLTime & "#"
End Function
Function MakeSQLDate(pSourceDate As Variant) As String
Dim SQLDay As String
Dim SQLMonth As String
Dim SQLYear As String
SQLDay = Day(pSourceDate)
SQLMonth = Month(pSourceDate)
SQLYear = Right(Year(pSourceDate), 4)
MakeSQLDate = "#" & SQLMonth & "/" & SQLDay & "/" & SQLYear & "#"
End Function
使用这些来完成您正在尝试的内容:
sSQL = "Insert Into BarTable Select *," & makeSQLDateTime(Now()) & " As DateTimeStamp FROM " + sExcel