使用VBA

时间:2016-10-08 11:01:24

标签: vba ms-access insert

我目前正在为我的工作创建一个出勤表。我正在使用MS Access。 我在插入功能方面遇到了麻烦。

我在插入查询上有点迷失了。 “登录”按钮现已完成。我需要的是一个注销按钮,所以查询将是这样的"插入到tblLogin(ExpectedTimeout)值(来自txtTime的值),其中 CurrentDay =我.txtToday和 NID = me.txtNID

以下是我的登录代码btw:

Private Sub btnTimeIn_Click()
Dim tme As String
Dim test1 As Date 'variable for logintime
Dim test2 As Date 'var for expected timein
Dim test3 As Date 'var for time difference

'tme = Time
'txtTime.Value = tme

'test1 = tme
'test2 = txtExpected

'test3 = test1 - test2
'txtRemarks.Value = test3

Dim db As Database
Dim rec As Recordset

Set db = CurrentDb
Set rec = db.OpenRecordset("Select * from tblLogin")

rec.AddNew
rec("CurrentDay") = Me.txtToday
rec("UserName") = Me.cboUname.Column(1)
rec("NID") = Me.txtNID
rec("Function") = Me.txtFunction
rec("Shiftdays") = Me.txtShift
rec("LoginTime") = Me.txtTime
rec("MinutesLate") = Me.txtLate
rec.Update

Set rec = Nothing
Set db = Nothing

MsgBox ("Logged In")

End Sub

1 个答案:

答案 0 :(得分:0)

看起来您确实想要更新在LogIn中创建的现有记录,而不是插入新记录。因此,请考虑使用包含在.Edit循环和Do语句中的Recordset If模式:

Set rec = db.OpenRecordset("tblLogin")

Do Until rec.EOF 
   If rec("CurrentDay") = Me.txtToday And rec("NID") = Me.txtNID Then
       rec.Edit
       rec("ExpectedTimeOut") = Me.txtTime 
       rec.Update
   End If
   rec.MoveNext
Loop