根据以下的代码,我想还有2个问题:
1.当输入的code变成“B”或者其他code时,超出时它不在提示msgbox。因为以下只对code=A有效。
2.能不能不用保存控件,提示完msgbox后直接无法输入,也就是让Me.ID = Null Me.code = Null Me.QTY = Null都为空的。
Private Sub Command3_Click()
Dim rst As DAO.Recordset
Dim strID As Integer
Dim strsum As Integer
strID = DLookup("qty", "表1", "id='" & Me.ID & "'") '获取和窗体id一样的表1中qty的值
strsum = DSum("qty", "表2", "ID='" & Me.ID & "' AND code='A'") '获取和窗体id一样,code为a的表2中qty的和
If Me.QTY + strsum > strID Then '两数进行比较
MsgBox "输入有误!"
Me.QTY.SetFocus
Me.QTY = Null
Exit Sub
End If
If MsgBox("您确认要保存吗?", vbOKCancel + vbInformation, "提示") = vbOK Then
Set rst = CurrentDb.OpenRecordset("表2", dbOpenDynaset)
rst.AddNew
rst("ID") = Me.ID
rst("code") = Me.code
rst("qty") = Me.QTY
rst.Update
rst.Close
Set rst = Nothing
'刷新数据
MsgBox "保存成功!", vbInformation, "提示"
Me.ID = Null
Me.code = Null
Me.QTY = Null
End If
End Sub