'DbFunc.bas
'== 标记数据库是否连接 ==
Private IsConnect As Boolean
'== 标记执行Connect()函数后,访问数据库的次数 ==
Private Connect_Num As Integer
Private cnn As ADODB.Connection '连接数据库的Connection对象
Private rs As ADODB.Recordset '保存结果集的Recordset对象
'连接数据库
Private Sub Connect()
'如果连接标记为真,则返回。否则会出错
If IsConnect = True Then
Exit Sub
End If
'关键New用于创建新对象cnn
Set cnn = New ADODB.Connection
'设置连接字符串ConnectionString属性
cnn.ConnectionString = Conn
'打开到数据库的连接
cnn.Open
'判断连接的状态
If cnn.State <> adStateOpen Then
'如果连接不成功,则显示提示信息,退出程序
MsgBox "数据库连接失败"
End
End If
'设置连接标记,表示已经连接到数据库
IsConnect = True
End Sub
'断开与数据库的连接
Private Sub Disconnect()
Dim Rc As Long
'如果连接标记为假,标明已经断开连接,则直接返回
If IsConnect = False Then
Exit Sub
End If
'关闭连接
cnn.Close
'释放cnn
Set cnn = Nothing
'设置连接标记,表示已经断开与数据库的连接
IsConnect = False
End Sub
'使用Connect_Num控制数据库连接
Public Sub DB_Connect()
Connect_Num = Connect_Num + 1
Connect
End Sub
Public Sub DB_Disconnect()
If Connect_Num >= CONNECT_LOOP_MAX Then
Connect_Num = 0
Disconnect
End If
End Sub
'强制关闭api方式访问的数据库,计数器复位
Public Sub DBapi_Disconnect()
Connect_Num = 0
Disconnect
End Sub
'执行数据库操作语句
Public Sub SQLExt(ByVal TmpSQLstmt As String)
'创建Command对象cmd
Dim cmd As New ADODB.Command
'连接到数据库
DB_Connect
'设置cmd的ActiveConnection属性,指定与其关联的数据库连接
Set cmd.ActiveConnection = cnn
'设置要执行的命令文本
cmd.CommandText = TmpSQLstmt
'MsgBox TmpSQLstmt
'执行命令
cmd.Execute
'清空cmd对象
Set cmd = Nothing
'断开与数据库的连接
DB_Disconnect
End Sub
'执行数据库查询语句
Public Function QueryExt(ByVal TmpSQLstmt As String) As ADODB.Recordset
'创建Recordset对象rst
Dim rst As New ADODB.Recordset
'连接到数据库
DB_Connect
'设置rst的ActiveConnection属性,指定与其关联的数据库连接
Set rst.ActiveConnection = cnn
'设置游标类型
rst.CursorType = adOpenDynamic
'设置锁定类型
rst.LockType = adLockOptimistic
'打开记录集
rst.Open TmpSQLstmt
'返回记录集
Set QueryExt = rst
End Function
'Variable.bas
'保存执行SQL语句的字符串
Public SqlStmt As String
'
'声明类模块变量
Public MyType As New Types '类型对象
Public MyDep As New Department '部门对象
Public MyDraw As New Draw '领用对象
Public MyList As New DrawList '明细对象
Public MyStore As New Store '库存对象
Public MyIn As New StoreIn '入库对象
Public MySupplier As New Supplier '供应商对象
Public MyUser As New Users
Public CurUser As New Users
'Const.bas
'版本号
Public Const VerNum As String = "版本 1.5"
'生成日期
Public Const GenDate As String = "日期 2018-08-29"
'定义数据库连接字符串
Public Const Conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Office.mdb;"
'一次执行connect操作可以访问数据库的次数
Public Const CONNECT_LOOP_MAX = 10