处理Ms访问表单,该表单设计为以数据表模式显示,允许同时查看和添加多行数据。
一旦用户将他们的数据输入到表中,我有一个按钮,他们必须在离开表单之前单击。我希望在运行宏之前,该按钮验证选项1或2是否为真,否则会出现一个消息框提示用户。
- 或者(Eps OR Clm#不为空)并且
LPymtDate
不为空 - 所有四个字段都必须有数据且不能为空(
MdID
或AdmitDt
或DischrgDt
或LPymtDate
)
下面是我一直在使用的代码,但它只检查一行数据。是否有人提出建议或代码允许我验证所有行?
Option Compare Database
Private Sub Command12_Click()
If Not IsNull([Eps]) Or Not IsNull([Clm#]) And Not IsNull([LPymtDate]) Then
DoCmd.RunMacro ("CheckingAccts")
ElseIf Not IsNull([MdID]) And Not IsNull([AdmitDt]) And Not IsNull([DischrgDt]) And Not IsNull([LPymtDate]) Then
DoCmd.RunMacro ("CheckingAccts")
Else
MsgBox "Input Required Fields"
End If
End Sub
对于用户来说,在保存前验证每一行会更简单,也更好,而且不需要按钮。
为此,请使用以下表单的BeforeUpdate事件:
Cancel = IsNull([Eps] & [Clm#]) Or IsNull([LPymtDate] + [MdID] + [AdmitDt] + [DischrgDt] + [LPymtDate])
If Cancel = True Then
MsgBox "Please input required fields."
Else
DoCmd.RunMacro ("CheckingAccts")
End If
Option Compare Database
Private Sub Command12_Click()
Dim myConnection As ADODB.Connection
Set myConnection = CurrentProject.Connection
Dim myRecordset As New ADODB.Recordset
Dim f As Integer
Dim ans As Integer
Set rst = myRecordset
myRecordset.ActiveConnection = myConnection
myRecordset.Open "Local_Holder_CoverInfo_3_Accounts", , adOpenStatic, adLockOptimistic
rst.MoveFirst
Do Until rst.EOF()
If (Not IsNull([Eps]) Or Not IsNull([Clm#])) And Not IsNull([LPymtDate]) Then
DoCmd.GoToRecord , , acNext
ElseIf Not IsNull([MedID]) And Not IsNull([AdmitDt]) And Not IsNull([DischrgDt]) And Not IsNull([LPymtDate]) Then
DoCmd.GoToRecord , , acNext
Else
f = 1
End If
rst.MoveNext
Loop
If f >= 1 Then
MsgBox "Input Required Fields"
Else
DoCmd.RunMacro ("CheckingAccts")
End If
End Sub