我在Access with ONE按钮中有一个表单.按钮1应该将值插入(UserID,Now(),Date())到tbl字段中(UserID,Time_In,WorkDate).
按钮2应该将值插入(Now())到tbl字段中(Time_Out).
我能想到的最佳方法是使用insert/update语句; 伪语法:
If UserID = UserID and WorkDat e= Date() THEN SET Time_Out = Now()
ELSE
Insert into tbl (UserID,WorkDate,Time_In).
Run Code Online (Sandbox Code Playgroud)
尽管我已经读过Access不能很好地处理Upserts,有没有办法可以做到这一点?
我读过这个:
在MS访问中进行upserting
我不太明白这里建议的答案: MS Access UPSERT(更新/插入)SQL
提前致谢,
Jof.
使用UserID = UserID和WorkDate = Date()打开记录集,其中condition,如果Recordcount> 0,则使用Edit方法,否则使用AddNew方法并更新记录集.像这样的东西:
Dim rst As Recordset
Set rst = DBEngine(0)(0).OpenRecordset("select * from TimeTable where UserID = " & UserId & " and WorkDate= #" & Format(Date, "mm\/dd\/yyyy") & "#")
With rst
If .RecordCount > 0 Then
.Edit
!Time_Out = Now()
Else
.AddNew
!UserId = UserId
!WorkDate = Date
!Time_In = Now
End If
.Update
End With
rst.Close
Set rst = Nothing
Run Code Online (Sandbox Code Playgroud)