📄 module1.bas
字号:
Attribute VB_Name = "Module1"
Public fMainForm As frmMain
Public UserName As String
Public reportSQL As String '定义报表设计器中的动态SQL查询语句
Public DB As String
Public Function ConnectString() _
As String
ConnectString = "Provider=MSDASQL.1;DRIVER={SQL Server};SERVER=(local);UID=sa;PWD=83833224;DATABASE=Exam;Data Provider=MSDASQL" '——无须建立.DSN文件,使用连接字符串直接连接数据库
'ConnectString = "FileDSN=Exam.dsn;UID=sa;PWD=" '——建立.DSN文件连接数据库
End Function
Public Function ExecuteSQL(ByVal SQL _
As String, MsgString As String) _
As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open ConnectString '——打开连接字符串,连接数据库
If InStr("INSERT,DELETE,UPDATE", _
UCase$(sTokens(0))) Then '——执行数据操纵语言
cnn.Execute SQL
MsgString = sTokens(0) & _
" query successful" '——判断数据操纵语言是否执行成功
Else '——执行数据查询语言
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, _
adOpenKeyset, _
adLockOptimistic '——访问数据库设置的加锁方式
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & _
" 条记录 " '——找到符合条件的记录的数目
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
ExecuteSQL_Error: '——返回查询错误信息
MsgString = "查询错误: " & _
Err.Description
Resume ExecuteSQL_Exit
End Function
'在每一个窗体的下拉式列表框中添加内容
Public Function DataList(txtSQL As String, ComboName As Object)
Dim MRC As ADODB.Recordset
Dim MsgText As String
Set MRC = ExecuteSQL(txtSQL, MsgText)
ComboName.Clear
If Not MRC.EOF Then
Do While Not MRC.EOF
ComboName.AddItem Trim(MRC.Fields(0)) '——在下拉式列表框中添加内容
MRC.MoveNext
Loop
End If
End Function
Public Function cnn_UPDATE()
Dim txtSQL, MsgText As String
Dim MRC As ADODB.Recordset
txtSQL = "update USERLOGIN set IsValid=1 where UserName='" & frmLogin.txtusername.Text & "'"
Set MRC = Module1.ExecuteSQL(txtSQL, MsgText)
End Function
Public Function JUDGE(MsgText As String, FormName As Object)
If (MsgText = "insert query successful") Or (MsgText = "INSERT query successful") Then
If (FormName.Caption = "注册用户") Then
MsgBox "添加用户信息成功,请要求系统管理员分配您的操作权限!", vbOKOnly + vbInformation, "成绩管理系统"
Else
MsgBox "通过窗体[" & FormName.Caption & "]向数据库添加数据成功!", vbOKOnly + vbInformation, "成绩管理系统"
End If
Else
MsgBox "'" + MsgText + "'", vbOKOnly + vbCritical, "警告" '——返回添加数据失败的原因
End If
End Function
Public Function Testtxt(txt As String) As Boolean
If Trim(txt) = "" Then
Testtxt = False
Else
Testtxt = True
End If
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -