Friday, January 17, 2014
Access VBAのテキストボックス入力制御
' 使用例    Call chkKeyPress(KeyAscii, TextBox1, 8, "[0-9]")
    '【パターン例】
    '"[0-9]" '半角数字のみ入力可
    '[A-Z] 半角英字のみ
    '[!0-9] 半角数字以外
    '[!A-Z] 半角英字以外
' キー入力チェック関数
Public Sub chkKeyPress(ByRef KeyAscii As MSForms.ReturnInteger, _
                            txtTest As Object, _
                            maxLength As Long, _
                            pattern As String)
    Select Case KeyAscii
        Case vbKeyBack, vbKeyDelete, vbKeyEscape ' BS,Del,Escapeは除外
            Exit Sub
    End Select
    
    If Not Chr(KeyAscii) Like pattern Then
        KeyAscii = 0
        Exit Sub
    End If
    
    If LenB(StrConv(txtTest.Text, vbFromUnicode)) - txtTest.SelLength >= maxLength Then
        KeyAscii = 0
    End If
End Sub
Subscribe to:
Comments (Atom)
