0

我目前使用如下所示的设置。

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 中的最后一列是日期/时间列,我想将当前时间插入其中?

4

1 回答 1

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
于 2013-04-19T02:33:58.020 回答