moddboperate.bas

来自「一款非常适合中小学教学任务不是很繁重的管理程序」· BAS 代码 · 共 70 行

BAS
70
字号
Attribute VB_Name = "modDbOperate"
'声明全局连接
Public CN As ADODB.Connection
'数据库链接打开
Public Sub dbConn()
On Error GoTo dbErr
    Dim strConn As String
    strConn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\data\student.mdb"
    Set CN = New ADODB.Connection
    CN.Open strConn
    Exit Sub
dbErr:
    MsgBox err.Description, , "班主任管理系统"
End Sub
'数据库链接断开
Public Sub dbClose()
On Error GoTo dbErr
    CN.Close
    Set CN = Nothing
    Exit Sub
dbErr:
    MsgBox err.Description, , "班主任管理系统"
End Sub
'数据库操作
Public Sub dbOperate(strSql As String)
On Error GoTo dbErr
    dbConn
    CN.Execute strSql
    dbClose
    Exit Sub
dbErr:
    MsgBox err.Description, , "班主任管理系统"
End Sub
'数据库选择:ADODB.Recordset
Public Function dbSelect(strSql As String) As ADODB.Recordset
Dim RS As New ADODB.Recordset
On Error GoTo dbErr
    dbConn
    Set RS = CN.Execute(strSql)
    Set dbSelect = RS
    Exit Function
dbErr:
    MsgBox err.Description, , "班主任管理系统"
End Function
Sub writeMS(strSql As String, MS As MSFlexGrid)
'动态向网格中添加数据
Dim iC, iRow, iCol, i As Integer
Dim RS As New ADODB.Recordset
Dim iRows, iCols As Integer
iC = 0
Set RS = dbSelect(strSql)
iCols = RS.Fields.Count
MS.Cols = iCols
MS.Rows = 1
MS.Rows = 2
While Not RS.EOF
    iC = iC + 1
    For i = 0 To iCols - 1
        MS.TextMatrix(iC, i) = RS.Fields(i)
    Next i
    MS.Rows = MS.Rows + 1
    RS.MoveNext
Wend
'添加网格列头
For i = 0 To iCols - 1
MS.TextMatrix(0, i) = RS.Fields(i).Name
Next i
MS.TextMatrix(MS.Rows - 1, 0) = "合计:" & iC
End Sub

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?