我将此代码构建为测试,以根据日期范围从Access 2013数据库中删除一系列记录。我在查询表达式'START_DATE> = .....等中遇到运算符错误。我也尝试过带撇号的select语句。
注意:CALL行是实际代码中的一行。此外,如果我使用Between / AND而不是> = /< =运行CALL行,代码完成时没有错误,但没有完成任何操作。它没有找到并删除行。
Function Delete_Range()
Dim begdt As Date
Dim enddt As Date
'user inputs date range
begdt = InputBox("Enter beginning date as mm/01/yyyy", "BEGINNING DATE")
enddt = InputBox("Enter ending date as mm/01/yyyy", "ENDING DATE")
Dim objectrecordset As ADODB.Recordset
Set objectrecordset = New ADODB.Recordset
'initiate recordset object
objectrecordset.ActiveConnection = CurrentProject.Connection
Call objectrecordset.Open("select START_DATE
from TEMP_DATE_RANGE where START_DATE IS >= "
& begdt & " AND <= " & enddt, , , adLockBatchOptimistic)
While objectrecordset.EOF = False
'delete record
objectrecordset.Delete
objectrecordset.UpdateBatch
'move to next record
objectrecordset.MoveNext
Wend
End Function
谢谢大家的帮助。这是有效的代码。
DoCmd.SetWarnings (warningsoff)
'Declare variables
Dim begdt As String
Dim enddt As String
'User inputs variables
begdt = InputBox("Enter beginning date as mm/01/yyyy", "BEGINNING DATE")
enddt = InputBox("Enter ending date as mm/01/yyyy", "ENDING DATE")
'Format variable as date and error handling
If Not (IsDate(begdt) And IsDate(enddt)) Then
MsgBox "Please enter a date using a the date format", vbOKOnly
GoTo Finished
Else
begdt = Format(begdt, "\#yyyy\/mm\/dd\#")
enddt = Format(enddt, "\#yyyy\/mm\/dd\#")
End If
'Delete records from tables based upon user input date range
Dim SQL As String
Dim SQL2 As String
Dim SQL3 As String
SQL = "DELETE * FROM TEST_TBL_1 WHERE START_DATE BETWEEN " & begdt & " AND " & enddt & ""
SQL2 = "DELETE * FROM TEST_TBL_2 WHERE START_DATE BETWEEN " & begdt & " AND " & enddt & ""
SQL3 = "DELETE * FROM TEST_TBL_3 WHERE START_DATE BETWEEN " & begdt & " AND " & enddt & ""
DoCmd.RunSQL SQL
DoCmd.RunSQL SQL2
DoCmd.RunSQL SQL3
'Close form and show process complete page
DoCmd.SetWarnings (warningson)
DoCmd.Close acForm, "DELETE HISTORY", acSaveNo
DoCmd.OpenForm "COMPLETE", acNormal, "", "", , acNormal
Finished:
End Function
答案 0 :(得分:1)
首先在Access查询设计器中找出查询逻辑和语法。假设 START_DATE 是日期/时间数据类型,请为目标日期范围的开头和结尾选择几个静态值:
SELECT START_DATE
FROM TEMP_DATE_RANGE
WHERE START_DATE BETWEEN #2015-1-1# AND #2015-10-30#
根据需要进行调整。
获得正确的SQL语句后,在VBA代码中构建相应的语句文本。
Dim strSelect As String
strSelect = "SELECT START_DATE FROM TEMP_DATE_RANGE " & _
"WHERE START_DATE BETWEEN " & Format(begdt,"\#yyyy-m-d\#") & _
" AND " & Format(enddt,"\#yyyy-m-d\#")
Debug.Print strSelect '<- inspect this in Immediate window; Ctrl+g will take you there
然后您可以将 strSelect 与objectrecordset.Open
但是,由于您的目标是删除这些行,因此您实际上并不需要记录集。您只需执行DELETE
语句即可。
DELETE FROM TEMP_DATE_RANGE
WHERE START_DATE BETWEEN #2015-1-1# AND #2015-10-30#
答案 1 :(得分:1)
You are mixing up date values and string expressions for these, and it all starts with your inputbox which always returns a string:
Dim begdt As String
Dim enddt As String
' user inputs date range
begdt = InputBox("Enter beginning date as mm/01/yyyy", "BEGINNING DATE")
enddt = InputBox("Enter ending date as mm/01/yyyy", "ENDING DATE")
If Not (IsDate(begdt) And IsDate(enddt)) Then
' Show error.
Else
begdt = Format(begdt, "\#yyyy\/mm\/dd\#)
enddt = Format(enddt, "\#yyyy\/mm\/dd\#)
Dim SQL As String
SQL = "Select START_DATE From TEMP_DATE_RANGE Where START_DATE Between " & begdt & " And " & enddt & ""
Dim objectrecordset As ADODB.Recordset
Set objectrecordset = New ADODB.Recordset
'initiate recordset object
objectrecordset.ActiveConnection = CurrentProject.Connection
Call objectrecordset.Open(SQL, , , adLockBatchOptimistic)
' <Snip>
' Clean up.
End If
答案 2 :(得分:0)
在SQL中,你不写:
where a < 8 and > 7
你写道:
where a < 8 and a > 7
因此,您需要修复where
子句:
objectrecordset.ActiveConnection = CurrentProject.Connection
Call objectrecordset.Open("select START_DATE
from TEMP_DATE_RANGE where START_DATE IS >= "
& begdt & " AND START_DATE <= " & enddt, , , adLockBatchOptimistic)
-----------------^
可能还有其他问题,但这在SQL中是显而易见的。
答案 3 :(得分:-1)
您可能需要在查询字符串中的日期周围插入一些单引号。像这样:
Call objectrecordset.Open("select START_DATE from TEMP_DATE_RANGE where START_DATE BETWEEN '" & begdt & "' AND '" & enddt & "'", , , adLockBatchOptimistic)
****我编辑了这个以替换is&gt; =并使用了一个声明之间的
答案 4 :(得分:-1)
您的日期比较语法略有偏差,请移除IS >=
中的START_DATE <=
,并记住添加<=
而非仅Call objectrecordset.Open("select START_DATE
from TEMP_DATE_RANGE where START_DATE >= "
& begdt & " AND START_DATE <= " & enddt, , , adLockBatchOptimistic)
通过更正,它变为:
if(!newArray)
newArray = new Array();
newArray.push(addition);