我想自动化用户在Excel工作表上提供的开始和结束日期。以下是我到目前为止的代码:
Sub Button1_Click() ; formatted
Dim startDate As Date
Dim endDate As Date
startDate = Worksheets("sheet1").Range("B4").Value
endDate = Worksheets("sheet1").Range("B6").Value
dbConnectStr = "Provider=xxdose;User Id=" & Uname
dbConnectStr1 = "Provider=xxdose;User Id=xxtrece ;Data Source=" & DSN
Worksheets("WorkFlow").Activate
ActiveSheet.Range("B4:C30000").Clear
'Set and Execute SQL Command
Set Sql.ActiveConnection = objmyconn
Sql.CommandText = "select system_date from syit_act_log where system_date between`enter code here` 'startDate' AND 'endDate' and action_id = 15 and log_desc not like '%svc_openlink_p%' order by system_date"
Sql.CommandType = adCmdText
Sql.Execute
Next
ActiveSheet.Range("B5").CopyFromRecordset (ds)
ActiveSheet.Range("A1").Select
End Sub
答案 0 :(得分:0)
在我的评论中提到这一点,你必须转义变量,将它们作为你正在构建的SQL语句字符串的一部分添加。
"select system_date from syit_act_log where system_date >= #" & StartDate & _
"# and system_date <= #" & EndDate & "# and action_id = 15 and log_desc not like '%svc_openlink_p%' order by system_date"