如何仅通过数字验证一组文本框,不使用<> 12并且在vba中不为空

时间:2016-01-28 15:43:54

标签: vba

Private Sub txt_dd_sku12_beforeUpdate(Cancel As Integer)

''start data validation by (len), (numeric) ''

If Len(Trim(txt_dd_sku12)) <> 12 Then
    txt_dd_sku12.BackColor = RGB(116, 174, 244)
    MsgBox ("the entry must be a 12 Digit SKU only")
    Cancel = True
    Exit Sub
End If

If Not IsNumeric(Trim(txt_dd_sku12)) Then
    MsgBox " Highlighted field can not be blank. entry DD'S Sku12  to proceed further"
    txt_dd_sku12.BackColor = RGB(116, 174, 244)
    Cancel = True
    Exit Sub
End If
End Sub

我有5个文本框,我必须验证,更新插入我的表格,当我按下我的上一个文本框中输入,但如果我回到早期文本框不要插入数据,直到我按Enter键。

  1. 只有数字
  2. 不是空的
  3. 不多于或少于12位

2 个答案:

答案 0 :(得分:0)

您正在寻找的是更新活动前的表格。这将使您无法移动到新记录,直到满足您的个别字段要求。

Public Function ValidField(ctl as Control, FieldLength as integer) as Boolea
    If Not IsNumeric(ctl) then
        MsgBox "Highlighted field can not be blank. Enter an appropriate text."
        ctl.backColor = RGB(116, 174, 244)
        DoCmd.CancelEvent
        Exit Function
    End if

    if len(ctl) <> FieldLength or IsNull(ctl) or ctl = "" then
        MsgBox "The entry must be a 12 digit sku only."
        ctl.BackColor = RGB(116,174,244)
        DoCmd.CancelEvent
        Exit Function
    End if

    ValidField = true 
End Function

Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Not ValidField(txtBox1, 7) Then
        Exit Sub
    End If
End Sub

答案 1 :(得分:0)

更改

If Len(Trim(txt_dd_sku12)) <> 12

If Len(Trim(txt_dd_sku12)) <> 12 And IsNumeric(Trim(txt_ss_sku12)) And Trim(txt_ss_sku12) <> vbNullString