ongke0711 > 23-09-19, 04:09 PM
Private Sub Form_Open(Cancel As Integer)
DoCmd.OpenForm "frmKeepOpenConn", acNormal, , , , acHidden
End Sub
Public rsKeepOpenConn As DAO.Recordset
Private Sub Form_Close()
rsKeepOpenConn.Close
Set rsKeepOpenConn = Nothing
End Sub
Private Sub Form_Open(Cancel As Integer)
Set rsKeepOpenConn = CurrentDb.OpenRecordset("tblKeepConn")
End Sub
Sub OpenAllDatabases(pfInit As Boolean)
' Mo ket noi toi tat ca các Database dùng trong linked table, giu ket noi den khi dóng chuong trinh .
' pfInit: TRUE - Mo ket noi (Goi khi khoi chay chuong trình)
' FALSE - Dóng ket noi (Goi khi thoat chuong trình)
'Nguon: Total Visual SourceBook
'----------------------------------------------------------------------
' Khai bao tong so BE database kêt noi
Const cintMaximumDatabases As Integer = 2
Dim X As Integer, i As Integer
Dim strName As String
Dim strMsg As String
Dim rsDBName As DAO.Recordset
Dim SourcePath(cintMaximumDatabases) As Variant
'Lay toan bo duong dan file database BE luu vào mang
Set rsDBName = DBEngine(0)(0).OpenRecordset("tblLinkedDBName", dbOpenDynaset)
If rsDBName.EOF And rsDBName.BOF Then
MsgBox "Khong tim thay duong dan file database ket noi", vbCritical, "Thông báo"
Exit Sub
End If
rsDBName.MoveFirst
i = 0
Do Until rsDBName.EOF
SourcePath(i) = CurrentProject.Path & "\Data\" & rsDBName!DBName
i = i + 1
rsDBName.MoveNext
Loop
' Liet ke toan bo databases ket noi vào mang
Static dbsOpen() As DAO.Database
If pfInit Then
ReDim dbsOpen(1 To cintMaximumDatabases)
For X = 1 To cintMaximumDatabases
' Duyet tung databases
strName = SourcePath(X - 1)
strMsg = ""
On Error Resume Next
Set dbsOpen(X) = OpenDatabase(strName, False, False, ";PWD=123456")
If Err.Number > 0 Then
strMsg = "Loi ket noi database: " & strName & vbCrLf & _
"Kiem tra lai duong dan file." & vbCrLf & _
"Error: " & Err.Description & " (" & Err.Number & ")"
End If
On Error GoTo 0
If strMsg <> "" Then
MsgBox strMsg
Exit For
End If
Next X
'MsgBox "Mo Ket noi xong."
Else
On Error Resume Next
For X = 1 To cintMaximumDatabases
dbsOpen(X).Close
Next X
End If
End Sub
HoangHoa > 23-09-19, 04:33 PM
Linh lê > 12-05-21, 04:17 PM