代码之家  ›  专栏  ›  技术社区  ›  Scott Kevern

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

  •  0
  • Scott Kevern  · 技术社区  · 9 年前
    • 使用Access 2010
    • 我知道最少的VB

    我有一张桌子叫 tbl原材料 其中包含2个重要字段: 零件号 直径 .

    我正在用一张表格填写不同的表格,其中包含各种信息。重要信息如下: 零件号 抗拉强度

    在表单中,我允许用户选择 零件号 来自具有行源的组合框 tbl原材料 。然后他们必须在标准文本框中输入抗拉强度。

    我需要一些有效的验证规则,它可以改变 抗拉强度 基于在组合框中选择的零件号的直径。

    例如:用户选择 零件号 000001,直径2“,可接受的抗拉强度>150。用户选择 零件号 000002,直径为6“,可接受的抗拉强度大于130

    我不能使用级联组合框,因为用户需要在 抗拉强度 盒我已经尝试过在表达式生成器中使用DLookUp()并创建宏,但我一直被卡住。谢谢你的帮助。

    1 回复  |  直到 9 年前
        1
  •  0
  •   PractLogical    9 年前

    尝试在组合框中添加另一列以获得直径。 确保相应地更新列计数和列宽。( 1. 用于列计数和 1.0 宽度)

    确保TS文本框的格式为 通用编号 因此Access可以处理非数字问题。

    我不会把时间浪费在 更新后 , 改变 ,等事件验证,因为他们在保存记录时可能不会输入任何内容。

    在代码尝试将数据保存到其他表之前立即进行验证。我建议创建一个返回布尔值的验证函数。此时可以检查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