【发布时间】:2013-01-11 21:26:07
【问题描述】:
好的,所以我正在尝试编写 VBA 代码以尽可能地自动化。我需要它做的是从表中的字段中读取,如果它满足条件,则将其复制到新表中。这是为了旋转的目的。如果CurrentDate 等于NextDateOut 而不是该项目的任何值,我想转到某个表但也想更新当前表中的值。 NextDateOut 将是表中的新 LastDateOut 值,NextDateIn 将是 NextDateIn 后 10 天,NextDateOut 将是 10 天后。我可以编写此数学逻辑,它只是将我的表中的值与我现在为 CurrentDate 的常量进行比较,并在条件满足时更新值并将值写入某个表。
这是目前为止的代码,在试图弄清楚它时也有很多错误。
Option Explicit
Sub Run()
'Declarations for grabbing data from the database for the VBA
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
'Open connection to current Access database
Set db = CurrentDb()
'Declarations for variables to deal with dates
Dim CurrentDate As Date
Dim NextDateOut As Date
Dim NextDateIn As Date
Dim LastDateOut As Date
Dim LastDateIn As Date
'Setting a consistant value, technically not a constant value since there's no "const"
CurrentDate = Date
'Will take this out eventually
MsgBox (CurrentDate)
strSQL = "SELECT Next Date Out FROM Tapes Where Next Date Out = CurrentDate"
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
With rst
If .RecorCount > 0 Then
.MoveFirst
.Edit
!Next Date Out = (CurrentDate+20)
.Update
End If
End With
End Sub
提前谢谢!!!我正在取得进展,但在路上碰壁了。再次感谢!!!
【问题讨论】:
-
SQL语句有两个错误:1.你没有将值传递给
where条件; 2. 字段名不用方括号括起来:strSQL = "select [Next Date Out] from tapes where [Next Date Out]=#" & CurrentDate & "#。请注意,在 Access SQL 中,您需要将日期括在数字符号之间,并且Date值对本地配置很敏感。我更喜欢使用"select... where [Next Date Out]=" & CDbl(CurrentDate)