1.TextBox输入控制
只能输入数字(包括粘贴),可以使用空格、退格、回车键。
2.修改用户资料时,先判断文本框的文本是否(全部)与原来相同,如果相同,不保存。这中间需要丢掉一些键盘符(否则随便一个空格就绕过检测了)
技穷,想都想不出来,请高手指点。
只能输入数字(包括粘贴),可以使用空格、退格、回车键。
2.修改用户资料时,先判断文本框的文本是否(全部)与原来相同,如果相同,不保存。这中间需要丢掉一些键盘符(否则随便一个空格就绕过检测了)
技穷,想都想不出来,请高手指点。
调试欢乐多
-----------------------Private Sub Form_Load()
Me.KeyPreview = True
End SubPrivate Sub Text1_Change()
If Len(Text1.Text) > 0 And Text1.Text Like "*[!0-9 ]*" Then MsgBox "Err!": Text1.Text = ""
End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 8, 32, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57
Exit Sub
Case Else
KeyAscii = 0
End Select
End Sub
2.修改用户资料时,先判断文本框的文本是否(全部)与原来相同,如果相同,不保存。
--------------------
全局变量记录文本框的内容
Case 8, 13,32, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57
If Trim(Text1.Text)="" or (Trim(Text1.Text)<>"" and IsNumeric(Trim(Text1.Text)) = False) Then
MsgBox "请输入数字"
Exit Sub
End If
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
With rs
Set .ActiveConnection = cn '你的数据库连接字符串
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Open "select * from table1 where 字段1 = '" & Trim(Text1.Text) & "'"
If .RecordCount > 0 Then
'不保存
Exit Sub
Else
'新增(insert into table1 values(?)……)
End If
End With
只能输入数字(包括粘贴),可以使用空格、退格、回车键。 意思是只能输入数字+空格+换行是吗?
如果是这样,你必须拆开一个个字符来判断,在Validate里面判断就行了2.修改用户资料时,先判断文本框的文本是否(全部)与原来相同,如果相同,不保存。这中间需要丢掉一些键盘符(否则随便一个空格就绕过检测了)
技穷,想都想不出来,请高手指点。为什么要判断呢?相同的话保存也无所谓啊,判断还要增加代码。
如果你一定要判断,可以将现在的文本跟ado的值作对比。
Select Case keyascii
Case 8, 9, 13, &H30 To &H39
Digit_Only = keyascii
Case Else
Digit_Only = 0
End Select
End FunctionPrivate Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = Digit_Only(KeyAscii)
End Sub好处是在所有需要同样控制的 TextBox 的 KeyPress 事件中,都只需这同样的一个语句。
Private Sub Text1_Change()
Static isSelfChange As Boolean
If isSelfChange Then Exit Sub
isSelfChange = True
Dim s As String, ch As String
Dim iLen As Long, iSelStart As Long
Dim i As Long, bChanged As Boolean
s = Text1.Text
iLen = Len(s)
iSelStart = Text1.SelStart
i = 1
While i <= iLen
ch = Mid$(s, i, 1)
Select Case ch
Case "0" To "9"
'合法
i = i + 1
Case Else
s = Left$(s, i - 1) & Mid$(s, i + 1)
iLen = iLen - 1
bChanged = True
If i <= iSelStart Then
iSelStart = iSelStart - 1
End If
End Select
Wend
If bChanged Then
Text1.Text = s
Text1.SelStart = iSelStart
End If
isSelfChange = False
End Sub
2.如果你能保证原文本也只有数字,那么直接文本比较就可以了。
Private Sub Text1_Change()
If IsNumeric(Text1) Then
a = Text1.Text
Else
Text1.Text = a
End If
End Sub
private valTemp as stringprivate sub Set_Temp_Val(byval strVal as string)
on error goto err
valTemp = strVal
exit sub
err:
end sub
private sub txtTest_Change()
on error goto err
if isnumeric(txtTest.text) then
call Set_Temp_Val(txtTest.text)
else
txtTest.text = valTemp
end if
exit sub
err:
end sub我用过的方法,可以允许修改,支持任意录入,不是数字时忽略输入