根据用户输入更改 Access 表单中的验证规则?

Changing validation rule in an Access form, based on user input?

我有一个名为 tblRawMaterials 的 table,它包含 2 个重要字段:PartNumberDiameter.

我正在使用表格填写不同的 table 各种信息。重要信息是:PartNumberTensileStrength

在表单中,我允许用户从具有行源 tblRawMaterials 的组合框中选择 PartNumber。然后他们必须将抗拉强度输入到标准文本框中。

我需要一些影响验证规则的东西,该规则根据在组合框中选择了部件号。

例如:用户选择 PartNumber 000001,直径为 2",acceptable 抗拉强度>150。用户选择 PartNumber 000002 直径为 6",acceptable 抗拉强度>130

我无法使用级联组合框,因为用户需要在 TensileStrength 框中输入十进制数据。我曾尝试在表达式生成器中使用 DLookUp() 并创建宏,但我一直被卡住。感谢您的帮助。

尝试在您的组合框中添加另一列以显示直径。 请务必相应地更新列数和列宽。 (1 用于列数,1;0 用于宽度)

确保 TS 文本框的格式为通用数字,以便 Access 可以处理非数字问题。

我不会在 AfterUpdateChange 等事件验证上浪费时间,因为他们可能不会输入任何内容在他们保存记录的时候。

在您的代码尝试将数据保存到不同 table 之前立即进行验证。我建议创建一个 returns 布尔值的验证函数。您可以在那个时候检查 TS 空值。您还可以参考组合框中的新列。

Function SaveTheRecord() As Long

  If DataIsValid = False Then Exit Function

  'Your current code to save record to the other table

End Function

Function DataIsValid() As Boolean

  DataIsValid = False 'default to false so you can just jump out of the function if criteria is not met

  If IsNull(txtTS) Then
    MsgBox "Please Enter a valid Tensile Strength", vbExclamation, "Validation Error"
    Exit Function
  End If

  'txtTS not null at this point

  '1st senerio
  If myCombo.Column(1) < 2 Then 'diameter < 2" (this is the new column for diameter)
    If txtTS < 150 Then
      MsgBox "Please enter a Tensile Strength that is >= 150", vbExclamation, "Validation Error"
      Exit Function
    End If
  End If

  'Check any other scenerios

  DataIsValid = True 'if we've reached this point, then everyting must be ok

End Function

您可能想要在 TS 文本框旁边创建一个标签,以告知用户范围限制,以便他们在尝试保存之前知道。您可以在组合框的 AfterUpdate 事件中执行此操作。这只是一个一般性的例子,可以让您了解一下。

Private Sub myCombo_AfterUpdate()
  Call ShowPartRange
End Sub

function ShowPartRange() as long

  'if logics based on myCombo.Column(1)
  lblRange.caption = "Must be > 150"
  'etc

End Function