7,763
社区成员
发帖
与我相关
我的任务
分享
'------------------------------------------------------------------------------------------------------
'
' キャラクターチェック
'
' 引数:文字列、リターンコード
' 戻値:0(正常)、-1(エラー)
' リターンコード:-1(エラー)、1(半角)、2(全角)、3(混在)、4(空白)、5(Null)、9(その他)
'
'
' 作成日:2000/11/17 sakaguchi
'
'------------------------------------------------------------------------------------------------------
Function Chk_Char(p_char, p_Rc) As Integer
Dim i As Integer
Dim char_len As Integer
Dim chk_len As Integer
Dim asc_code As Integer
On Error GoTo Chk_Char_Err
Chk_Char = -1
p_Rc = -1
If IsNull(p_char) = True Then
' Null
p_Rc = 5
Chk_Char = 0
Exit Function
End If
char_len = Len(Trim(p_char))
If char_len = 0 Then
' 空白
p_Rc = 4
Chk_Char = 0
Exit Function
End If
chk_len = 0
For i = 1 To char_len
asc_code = Asc(Mid(Trim(p_char), i, 1))
If 0 <= asc_code And asc_code <= 255 Then
chk_len = chk_len + 1
Else
chk_len = chk_len + 2
End If
Next i
Select Case chk_len
Case char_len
' ASCII
p_Rc = 1
Case (char_len * 2)
' シフトJIS(漢字)
p_Rc = 2
Case char_len To (char_len * 2)
' 混在
p_Rc = 3
Case Else
' その他
p_Rc = 9
End Select
Chk_Char = 0
Exit Function
Chk_Char_Err:
MsgBox Err & " " & Error & Chr(13) & Chr(10) & "ErrPlace = Chk_Char", vbOKOnly
End Function
Option Explicit
Private Sub Text1_Validate(Cancel As Boolean)
Dim lLenUnicode As Long
Dim lLenAnsi As Long
lLenUnicode = Len(Text1)
lLenAnsi = LenB(StrConv(Text1, vbFromUnicode))
If (lLenUnicode > 8) Or (lLenUnicode <> lLenAnsi) Then
MsgBox "只能输入8位半角字符!", vbExclamation
Cancel = True
End If
End Sub
Private Sub Text2_Validate(Cancel As Boolean)
Dim lLenUnicode As Long
Dim lLenAnsi As Long
lLenUnicode = Len(Text2)
lLenAnsi = LenB(StrConv(Text2, vbFromUnicode))
If (lLenUnicode > 5) Or ((lLenUnicode * 2) <> lLenAnsi) Then
MsgBox "只能输入5位全角字符!", vbExclamation
Cancel = True
End If
End Sub
Private Function WhatChar(ByVal vStr As String) As Integer
Dim gbascii As Byte
Dim intChar As Integer
If Asc(vStr) < 0 Then
gbascii = AscB(StrConv(vStr, vbFromUnicode))
'区位码在16区之后的为汉字
If gbascii - 160 > 15 Then
intChar = 0 ' "是汉字"
Else
intChar = 2 ' "是全角符号"
End If
Else
intChar = 1 ' "是半角英文或数字"
End If
WhatChar = intChar
End Function