[英]Export Excel Spread Sheet to Access Table with A Timestamp?
I currently use a set up like the one below. 我目前使用类似以下的设置。
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 if I wanted to add a now() column in the export is this possible? 现在,如果我想在导出中添加now()列,这可能吗? something like
就像是
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
Where the last column in BarTable is a Date/Time column and I want to insert the current time into it? BarTable的最后一列是Date / Time列,我想在其中插入当前时间吗?
to enter the date in SQL you may need to convert it into text - Access itself can handle 'Now()' but not sure if it can when being passed a SQL string. 要在SQL中输入日期,您可能需要将其转换为文本-Access本身可以处理'Now()',但不确定在传递SQL字符串时是否可以。 Something like the below should do it.
像下面这样的事情应该做。
sSQL = "Insert Into BarTable Select *,#" & format(NOW(),"MM/DD/YYYY hh:mm:ss") & "# As DateTimeStamp FROM " + sExcel
In Access I have two functions that I use for getting the date or date and time stamp when building SQL strings, I haven't tested but they should translate into Excel VBA okay: 在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
To use these to complete what you are trying: 要使用这些来完成您正在尝试的操作:
sSQL = "Insert Into BarTable Select *," & makeSQLDateTime(Now()) & " As DateTimeStamp FROM " + sExcel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.