本窗体优化的内容:
1.班号,教室,班主任文本框的输入限制,如:只能输入数字或汉字
2.年级comboBox中禁止输入,只能选择已经加载的内容—把style属性为2
3.窗体禁止改变大小—把borderStyle属性改为1
4.使子窗体显示在中间位置
Me.Move ((fMainForm.Width - Me.Width) / 2), ((fMainForm.Height - Me.Height) / 3)
下方是本窗体的代码,仅供参考!
Private Sub Command1_Click()
Dim mrc As ADODB.Recordset
Dim MsgText As String
Dim txtSQL As String
'防止输入框为空
If Not Testtxt(txtClassno.Text) Then
MsgBox "请输入班号!", vbOKOnly + vbExclamation, "警告"
txtClassno.SetFocus
Exit Sub
End If
'添加前确保该班号不存在
txtSQL = "select * from class_Info "
Set mrc = ExecuteSQL(txtSQL, MsgText)
'数据库的记录指到第一条
mrc.MoveFirst
While (mrc.EOF = False)
If (Trim(mrc.Fields(0)) = Trim(txtClassno.Text)) Then
MsgBox "班号已经存在,请重新输入班号!", vbOKOnly + vbExclamation, "警告"
Exit Sub
txtClassno.Text = ""
txtClassno.SetFocus
Else
mrc.MoveNext
End If
Wend
'添加前确保该教室没有被占用
mrc.MoveFirst
While (mrc.EOF = False)
If (Trim(mrc.Fields(3)) = Trim(txtClassroom.Text)) Then
MsgBox "教室已经存在,请重新输入班号!", vbOKOnly + vbExclamation, "警告"
Exit Sub
txtClassno.Text = ""
txtClassno.SetFocus
Else
mrc.MoveNext
End If
Wend
'把班号,年级,班主任,教室中的数据添加到数据库中
mrc.AddNew
mrc.Fields(0) = Trim(txtClassno.Text)
mrc.Fields(1) = Trim(comboGrade.Text)
mrc.Fields(2) = Trim(txtDirector.Text)
mrc.Fields(3) = Trim(txtClassroom.Text)
mrc.Update
mrc.Close
MsgBox "添加班级信息成功!", vbOKOnly + vbExclamation, "添加班级信息"
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Load()
'先向年级(comboGrade)中加载数据
comboGrade.AddItem "初中一年级"
comboGrade.AddItem "初中二年级"
comboGrade.AddItem "初中三年级"
comboGrade.AddItem "高中一年级"
comboGrade.AddItem "高中二年级"
comboGrade.AddItem "高中三年级"
'设置窗体大小和显示的位置
Me.Height = 7875
Me.Width = 10350
Me.Move ((fMainForm.Width - Me.Width) / 2), ((fMainForm.Height - Me.Height) / 3)
End Sub
'限制文本框输入
Private Sub txtClassno_KeyPress(KeyAscii As Integer)
'只能输入数字和退格键
If KeyAscii = 8 Then
ElseIf KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End If
'文本框只能输入5个字符,如果超出则不能输入并且提示用户
If Len(txtClassno.Text) > 5 Then
KeyAscii = 0
MsgBox "班号字数超出"
End If
End Sub
'限制文本框输入
Private Sub txtClassroom_KeyPress(KeyAscii As Integer)
'只能输入数字和退格键
If KeyAscii = 8 Then
ElseIf KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End If
'文本框只能输入5个字符,如果超出则不能输入并且提示用户
If Len(txtClassroom.Text) > 5 Then
KeyAscii = 0
MsgBox "教室字数超出"
End If
End Sub
'限制文本框输入
Private Sub txtDirector_KeyPress(KeyAscii As Integer)
'只能输入汉字和退格键
If KeyAscii >= -20319 And KeyAscii <= -3652 Or KeyAscii = 8 Then
Else
KeyAscii = 0
End If
'文本框只能输入6个字符,如果超出则不能输入并且提示用户
If Len(txtDirector.Text) >= 6 Then
KeyAscii = 0
MsgBox "名字太长,这是中国版本!"
End If
End Sub