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

1 comment:


  1. ' キー入力制御
    Public Sub keyChange(txtTarget As Object, _
    maxLength As Integer)
    With txtTarget
    If LenBA(.Text) > maxLength Then
    .Value = StrConv( _
    LeftB(StrConv(.Text, vbFromUnicode), maxLength), _
    vbUnicode)
    If Asc(Right(.Value, 1)) = 0 Then
    .Value = LeftB(.Value, (maxLength - 1) * 2)
    End If
    .SelStart = maxLength
    End If
    End With
    End Sub

    ReplyDelete