在vb的登录窗口中,想让文本框1中只能输入字母,文本框2中只能输入数字,我的代码是这样写的可是运行不了

Private times As Integer
Private Sub form_load()
times = 0
End Sub
Private Sub Command1_Click()
Dim i As Integer
Dim a As Integer
For i = 1 To Len(Text1.Text)
a = Asc(Mid(tex1.Text, i, 1))
If a < 65 Or (a > 90 And a < 97) Or a > 122 Then
MsgBox "输入的必须为英文字母"
Exit For
End If
Next i

If times >= 2 Then
MsgBox "您没有权限登录此系统"
Unload Me
End If

Adodc1.RecordSource = "select * from 数据 where username='" & Text1.Text & "'"
Adodc1.Refresh
If Adodc1.Recordset.EOF Then
MsgBox "没有这个用户,请重新输入正确的用户名", , "错误提示"
times = times + 1

Else
If Text2.Text = Adodc1.Recordset.Fields("password") Then
Form2.Show
Else
times = times + 1
MsgBox "你的密码错误,请输入正确的用户密码!", , "错误提示"
End If
End If
End Sub

Private Sub Command2_Click()
Unload Me
End Sub

可是运行不了,哪位高手帮忙改改,非常感谢。。。。

Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
'如果按的不是数字键
If KeyAscii <> 8 Then '如果按的不是退格键
KeyAscii = 0 '则按键无效
MsgBox "输入的必须为数字"
End If
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 65 Or (KeyAscii > 90 And KeyAscii < 97) Or KeyAscii > 122 Then
KeyAscii = 0
MsgBox "输入的必须为英文字母"
End If
End Sub
Private times As Integer
Private Sub form_load()
times = 0
End Sub
Private Sub Command1_Click()
If times >= 2 Then
MsgBox "您没有权限登录此系统"
Unload Me
End If

Adodc1.RecordSource = "select * from 数据 where username='" & Text1.Text & "'"
Adodc1.Refresh
If Adodc1.Recordset.EOF Then
MsgBox "没有这个用户,请重新输入正确的用户名", , "错误提示"
times = times + 1

Else
If Text2.Text = Adodc1.Recordset.Fields("password") Then
Form2.Show
Else
times = times + 1
MsgBox "你的密码错误,请输入正确的用户密码!", , "错误提示"
End If
End If
End Sub

Private Sub Command2_Click()
Unload Me
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-10-29
我没听说过还有end for这个语句,如果想提示完后重新回到文本框1的话,直接把焦点重新设到text1上就行了
第2个回答  2010-10-29
TextBox1属性IMEMode设置fmIMEModeOff英语模式,
使用KeyPress事件,很简单搞定,直接禁止输入非英文字母:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 65 Or (KeyAscii > 90 And KeyAscii < 97) Or KeyAscii > 122 Then
KeyAscii = 0
MsgBox "输入的必须为英文字母"
End If
End Sub
第3个回答  2010-11-02
英文在前,汉语在后:
1、先打开一个EXCEL工作表。
2、将汉英连在一起的词汇表复制到工作表中的A列。
3、在B1单元格输入如下公式: =LEFT(A1,LEN(A1)-(LENB(A1)-LEN(A1))-1) 回车。
4、在C1单元格输入如下公式: =RIGHT(A1,LENB(A1)-LEN(A1)) 回车。
5、向下复制。
6、个别修正。

汉语在前,英文在后:
3、在B1单元格输入如下公式: =LEFT(A1,LENB(A1)-LEN(A1)) 回车。
4、在C1单元格输入如下公式: =RIGHT(A1,LEN(A1)-(LENB(A1)-LEN(A1))-1) 回车。
5、向下复制。
6、个别修正。
请参考
相似回答