表紙へ

テキストボックス

5.入力できる文字を制限する

 

1.半角英数だけ入力できるようにする例

   

Private Sub Text1_KeyPress(KeyAscii As Integer)

    If Not Chr(KeyAscii) Like "[a-zA-Z0-9]" Then
        KeyAscii = 0
    End If

End Sub
 

 

2.半角数字のみ入力できるようにする例

 

Private Sub Text1_KeyPress(KeyAscii As Integer)

    If Not Chr(KeyAscii) Like "[0-9]" Then
        KeyAscii = 0
    End If

End Sub

 

3.半角カタカナのみ入力できるようにする例

 

Private Sub Text1_KeyPress(KeyAscii As Integer)

    If Not Chr(KeyAscii) Like "[ア-ン゙゚ーッャュョァィゥェォ]" Then
        KeyAscii = 0
    End If

End Sub

 

4.半角文字のみ入力できるようにする例

 

Private Sub Text1_KeyPress(KeyAscii As Integer)

    If LenB(StrConv(Chr(KeyAscii), vbFromUnicode)) = 2 Then
        KeyAscii = 0
    End If

End Sub

 

5.バックスペースと半角英数のみ入力できるようにする例

 

Private Sub Text1_KeyPress(KeyAscii As Integer)

    If KeyAscii = vbKeyBack Then Exit Sub

    If Not Chr(KeyAscii) Like "[a-zA-Z0-9]" Then
        KeyAscii = 0
    End If

End Sub