Public Function OpenRS(ByVal sqlstmt As String) As ADODB.Recordset
Dim rst As New ADODB.Recordset
'加入红色部分进行判断,别的可以不要改
If cnn Is Nothing Then
Set cnn = New ADODB.Connection
DBconnect '打开之后,如果没有关闭不能再打开,否则会出错,跳过本操作
End If
rst.ActiveConnection = cnn
rst.Open sqlstmt
Set OpenRS = rst
End Function
Public Function DBconnect()
cnn.ConnectionString = "Provider = microsoft.jet.oledb.4.0;persist security info = false;data source = " & PJPath & "\TestDB.mdb;jet oledb:DataBase password = "
cnn.Open
End Function
Public Function DBDisconnect()
cnn.Close
Set cnn = Nothing
End Function
Public Function OpenRS(ByVal sqlstmt As String) As ADODB.Recordset
Dim rst As New ADODB.Recordset
DBconnect
rst.ActiveConnection = cnn
rst.Open sqlstmt
Set OpenRS = rst
End Function
**********调用openrs的程序
Public Function Count_GW() 'count gross weight
On Error Resume Next
Dim Rs_Count As New ADODB.Recordset
Dim Count_f, Count_t As Single
Count_f = 0
Count_t = 0
sql_f = "select SUM([GW(kgs)]) from [fabric details]"
sql_t = "select SUM([GW(kgs)]) from [packing list]"
Set Rs_Count = OpenRS(sql_f)
If Rs_Count(0).Value > 0 Then
Count_f = Rs_Count(0).Value
End If
Set Rs_Count = OpenRS(sql_t)
If Rs_Count(0).Value > 0 Then
Count_t = Rs_Count(0).Value
End If
Count_GW = Count_f + Count_t
Rs_Count.Close
Set Rs_Count = Nothing
DBDisconnect
End Function