学生管理系统总结(四)

来源:互联网 发布:excel自动生成数据 编辑:程序博客网 时间:2024/06/01 16:45

1. 添加完信息之后清空文本框

  '清空文本框信息 Dim ctrl As Control      

‘清空所有文本框中的内容(混用)

'方式一:(只能清空TEXT文本框中内容) For Each ctrl In Me.Controls   If TypeOf ctrl Is TextBox Then    '判断是否为文本框textbox     ctrl.Text = ""   End If Next
'方式二:    'txtSID.Text = ""    'txtName.Text = ""     comboSex.Clear    'txtBorndate.Text = ""     comboClassNo.Clear         'txtTel.Text = ""    'txtRudate.Text  ""    'txtAddress.Text = ""    'txtComment.Text = ""

2. 长度限制

‘电话长度只能为11位

  If Len(txtTel) <> 11 Then    MsgBox "电话号码为11位!", 64, "警告"    txtTel.Text = ""    txtTel.SetFocus    Exit Sub  End If

3. 文本框中不能输入特殊字符

Private Sub txtSID_KeyPress(KeyAscii As Integer)    If ((KeyAscii >= 33 And KeyAscii <= 47) Or (KeyAscii >= 58 And KeyAscii <= 64) Or (KeyAscii >= 91 And KeyAscii <= 96) Or (KeyAscii >= 123 And KeyAscii <= 126)) Then      MsgBox "不能输入特殊字符,请重新输吧!", vbOKOnly + vbExclamation, "警告"      KeyAscii = 0      Exit Sub    End IfEnd Sub

4. 添加日历

‘需添加Monthview控件(选择:工程–部件–控件–Microsoft Windows CommonControls–2.6.0–添加MonthView控件)

Private Sub Form_Load()  MonthView1.Visible = False  MonthView2.Visible = FalseEnd SubPrivate Sub MonthView1_DateClick(ByVal DateClicked As Date)  txtBorndate.Text = MonthView1.Year & "-" & MonthView1.Month & "-" & MonthView1.Day  MonthView1.Visible = FalseEnd SubPrivate Sub MonthView2_DateClick(ByVal DateClicked As Date)  txtRudate.Text = MonthView2.Year & "-" & MonthView2.Month & "-" & MonthView2.Day  MonthView2.Visible = FalseEnd SubPrivate Sub txtBorndate_Click()  MonthView1.Visible = True  MonthView2.Visible = FalseEnd SubPrivate Sub txtRudate_Click()  MonthView2.Visible = True  MonthView1.Visible = FalseEnd Sub

5. 入学日期小于出生日期

‘出生和入校时间

  Dim borndate As Date  Dim getdate As Date  borndate = Trim(txtBorndate.Text)  getdate = Trim(txtRudate.Text)  If getdate <= borndate Then    MsgBox "入学时间不能小于出生时间,请重新输入!", vbOKOnly + vbInformation, "提示"    txtRudate.SetFocus    Exit Sub  End If

6. 登录对话框中

  对于这段代码:ok为全局变量,用户登录成功,OK被赋值为True,系统自动进入“创建学生管理系统的主窗体”一旦三次输入密码均不正确,OK被赋值为flase,公用模块的Main过程将根据OK的值决定是退出还是进入系统

7. 使Tap键按顺序进行

主要应用控件的TABINDEX属性

txtSID.TabIndex = 0  txtName.TabIndex = 1  comboSex.TabIndex = 2  txtBorndate.TabIndex = 3  comboClassNo.TabIndex = 4  txtRudate.TabIndex = 5  txtTel.TabIndex = 6  txtAddress.TabIndex = 7  txtComment.TabIndex = 8  cmdOK.TabIndex = 9  cmdCancel.TabIndex = 10

8. 设置窗体的大小和位置

    Me.Width = 7600    Me.Height = 6700    Me.Left = Screen.Width / 2 - Me.Width / 2    Me.Top = Screen. Height / 2 - Me.Height / 2

9. 查找新添加记录是否与已有记录重复

 从数据库中查找已有记录存放到记录集中,记录集非空时,从第一条记录开始判断是否重复,若重复则exit sub;若不重复则继续判断下一条.
        txtSQL = "select* from user_info"        Set mrc = ExecuteSQL(txtSQL,MsgText)        While (mrc.EOF = False)         If Trim$(mrc.Fields("user_name").value) = Trim$(TxtUserName.Text)Then                  MsgBox "用户已存在,请重新输入用户名!",vbOKOnly + vbExclamation, "警告"                  TxtUserName.SetFocus                  TxtUserName.Text = ""                  TxtPassword1.Text = ""                  TxtPassword2.Text = ""                  Exit Sub        Else                  mrc.MoveNext        EndIf        Wend

mrc.EOF = False说明mrc记录集不为空,经常用mrc.BOF和EOF的值判断记录集是否为空

10. VB的程序窗口固定大小

更改窗体的BorderStyle属性为FixedSingle或Fixed Dialog或Fixed ToolWindow
也可以使用代码Me.BorderStyle = 1或 Me.BorderStyle = 3或 Me.BorderStyle = 4
一般使用FixedSingle,即 Me.BorderStyle = 1

11. “0”

添加课程信息窗口出现了一个问题,数据库中该表中的课程编号的数据类型是int,当你输入001时,它自动保存的是1,虽然会有判断是否重复记录,但是出入的和保存的判断不出来,因为输入的是001,表中保存的是1,所以这样做的后果是表中会有重复的课程编号。原因在于VB中int类型保存时会自动舍去第一位是0的位数。

12. 如何让combobox不能输入字符

combobox控件的Style属性为0

Private Sub comBograde_KeyPress(KeyAscii As Integer)    KeyAscii = 0End Sub
原创粉丝点击