📄 stud1.bas
字号:
Attribute VB_Name = "Module1"
'标准模块Module1(Stud1.bas)
Option Explicit
Public gstrUser As String '全局变量存用户名
Public gblnPow As Boolean '全局变量存用户权限
Public pubCnn As New ADODB.Connection '全局连接对象供各模块使用
'建立连接
Public Sub CreateConnection()
pubCnn.CursorLocation = adUseClient
pubCnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Stud05.mdb"
End Sub
'填充班级组合框。参数为组合框对象变量
Public Sub AddClassItem(cboX As ComboBox)
Dim rsTmp As New ADODB.Recordset '临时记录集
'过滤重复记录和空字段
'方法1:利用 GROUP BY 子句
' rsTmp.Open "SELECT 班级 FROM 学籍 GROUP BY 班级 " & _
" HAVING 班级<>NULL AND 班级<>''", _
pubCnn, adOpenStatic, adLockOptimistic
'方法2:利用 DISTINCT 关键字
rsTmp.Open "SELECT DISTINCT 班级 FROM 学籍 " & _
" WHERE 班级<>NULL AND 班级<>''", _
pubCnn, adOpenStatic, adLockOptimistic
'在组合框中添加班级
cboX.Clear
Do Until rsTmp.EOF
cboX.AddItem rsTmp("班级")
rsTmp.MoveNext
Loop
Set rsTmp = Nothing
End Sub
'焦点返回文本框并反显内容。参数为文本框对象变量。
Public Sub FocusBack(txtX As TextBox)
With txtX
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
End With
End Sub
'班级组合框失去焦点时检查列表,若用户输入了新班级,添加新项
Public Sub NewClassItem(cboX As ComboBox) '参数为组合框对象变量
Dim i As Integer
Dim blnOld As Boolean '班级名称存在标志
Dim sNew As String
blnOld = False
sNew = Trim$(cboX.Text) '取用户在组合框输入的班级名称
For i = 0 To cboX.ListCount - 1
'若列表中已有该班级,设置标志,退出循环
If cboX.List(i) = sNew Or sNew = "" Then
blnOld = True
Exit For
End If
Next
If blnOld = False Then '若列表中无该班级名称,添加
cboX.AddItem sNew
End If
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -