Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过Excel VBA中的SQL进行查询访问,其中Excel变量是查询的一部分_Sql_Excel_Vba_Ms Access - Fatal编程技术网

通过Excel VBA中的SQL进行查询访问,其中Excel变量是查询的一部分

通过Excel VBA中的SQL进行查询访问,其中Excel变量是查询的一部分,sql,excel,vba,ms-access,Sql,Excel,Vba,Ms Access,我希望能够在一组数据中查找最短/第一个日期,然后在Access中运行一个查询,从那天起删除某个表中的所有数据 到目前为止,我得到的是: Sub upload() Last = ActiveSheet.UsedRange.Rows.Count Dim LastOrderDate As Date Dim LastOrderDateYear As Integer Dim LastOrderDateMonth As Integer Dim LastOrderDateDay As Integer

我希望能够在一组数据中查找最短/第一个日期,然后在Access中运行一个查询,从那天起删除某个表中的所有数据

到目前为止,我得到的是:

Sub upload()
 Last = ActiveSheet.UsedRange.Rows.Count
 Dim LastOrderDate As Date
 Dim LastOrderDateYear As Integer
 Dim LastOrderDateMonth As Integer
 Dim LastOrderDateDay As Integer

 FirstOrderDate = Range("D2")
 FirstOrderDateYear = Year(FirstOrderDate)
 FirstOrderDateMonth = Month(FirstOrderDate)
 FirstOrderDateDay = Day(FirstOrderDate)

 'Import Data to Benji's Ecommerce Database
 ssheet = Application.ActiveWorkbook.FullName
 Set acApp = CreateObject("Access.Application")
     acApp.OpenCurrentDatabase ("X:\ECommerce Database.accdb")
     acApp.Visible = True
     acApp.UserControl = True
     acApp.DoCmd.RunSQL "DELETE * FROM [OrdersDetailed] WHERE Day([OrderDate])=""&FirstOrderDateDay&"" And Month([OrderDate])=""&FirstOrderDateMonth&"" And Year([OrderDate])=""&FirstOrderDateYear&"""
    acApp.DoCmd.TransferSpreadsheet 0, 9, "OrdersDetailed", ssheet, True, "B1:V" & Last
    acApp.CloseCurrentDatabase
    acApp.Quit
Set acApp = Nothing

End Sub
但它不会删除任何内容,运行正常,但Access中会出现一个消息框,提示正在删除0条记录

这可能吗

我需要使用不同的语法来输入变量吗

谢谢


Benji

尝试将您的runSQL字符串更改为:

acApp.DoCmd.RunSQL "DELETE * FROM [OrdersDetailed] WHERE Day([OrderDate])='" & FirstOrderDateDay & "' And Month([OrderDate])='" & FirstOrderDateMonth & "' And Year([OrderDate])='" & FirstOrderDateYear & "'"

我试过了,但结果是:“编译错误:预期:语句结束”,我不得不编辑它。你试过当前的编辑吗?是的,我试过了。它不喜欢主引号中的单引号。我也尝试了双精度,但没有成功。然后检查SQL本身。我将编辑我的答案,使脚本输出到即时窗口。将其复制粘贴到数据库本身的查询中,以查找脚本的问题。哇,好了,现在它开始工作了!但我发誓这个问题看起来没什么不同。。。您在编辑中更改了什么?