Advertisement
Dotterbart

Open con db

Nov 28th, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VisualBasic 3.86 KB | Source Code | 0 0
  1.  all first Parts in a Module
  2. First Part A
  3.  
  4. 'Verweis auf Microsoft Office Object Library nicht vergessen!!!
  5.  
  6. Public Function GetDateiname(strInitialFileName As String) As String
  7. Dim f As Office.FileDialog
  8. On Error GoTo GetDateiname_Error
  9. Set f = Application.FileDialog(msoFileDialogFilePicker)
  10. With f
  11.     .Title = "Mein Dialog"                    'Fenstertitel
  12.    .AllowMultiSelect = False                 'Nur eine Datei auswählbar
  13.    .ButtonName = "Auswählen"                 'Button Beschriftung
  14.    .Filters.Clear                            'erst alle Filter löschen
  15.    '.Filters.Add "Excel-Dateien", "*.xl*"     'dann eigene anlegen
  16.    '.Filters.Add "Word-Dateien", "*.do*"
  17.    .Filters.Add "Access-Dateien", "*.mdb; *.accdb"
  18.     .FilterIndex = 2                          'einen Filter vorselektieren
  19.    .InitialFileName = strInitialFileName                 'Startverzeichnis
  20.    .Show
  21. End With
  22.  
  23. If f.SelectedItems.Count > 0 Then
  24.     GetDateiname = f.SelectedItems(1)
  25. Else
  26.     GetDateiname = ""
  27. End If
  28. GetDateiname_Exit:
  29. Set f = Nothing
  30. Exit Function
  31. GetDateiname_Error:
  32. MsgBox Err.Number & " " & Err.Description, vbOKOnly, "Fehler"
  33. GoTo GetDateiname_Exit
  34. End Function
  35.  
  36. First Part B
  37.  
  38. Public Function ConnectDB() As Boolean
  39. On Error GoTo ConnectDatabase_Err
  40. Dim strFilePath As String
  41. Dim TD As DAO.TableDef
  42. Dim NTD As DAO.TableDef
  43. Dim DB As DAO.Database
  44. Dim NDB As DAO.Database
  45. Dim TDExist As Boolean
  46. Set DB = CurrentDb
  47.  
  48. strFilePath = GetDateiname(Application.CurrentProject.Path)
  49.  
  50. If strFilePath <> "" Then
  51.  
  52. Set NDB = OpenDatabase(strFilePath)
  53.     For Each TD In DB.TableDefs
  54.         If TD.Connect <> "" Then
  55.             TDExist = False
  56.             For Each NTD In NDB.TableDefs
  57.                 If TD.Name = NTD.Name Then
  58.                     TDExist = True
  59.                     Exit For
  60.                 End If
  61.             Next NTD
  62.         End If
  63.     Next TD
  64.     If Not TDExist Then
  65.         GoTo ConnectDatabase_Err
  66.     End If
  67.     strFilePath = "MS Access;PWD=Nelke;DATABASE=" & strFilePath
  68.     For Each TD In DB.TableDefs
  69.         If TD.Connect <> "" Then
  70.             TD.Connect = strFilePath
  71.             TD.RefreshLink
  72.         End If
  73.     Next TD
  74.     ConnectDB = True
  75. Else
  76.     ConnectDB = False
  77. End If
  78. NDB.Close
  79. Set NDB = Nothing
  80. Set NTD = Nothing
  81.  
  82. ConnectDatabase_Res:
  83. Set DB = Nothing
  84. Set TD = Nothing
  85.  
  86. Exit Function
  87. ConnectDatabase_Err:
  88. ConnectDB = False
  89. GoTo ConnectDatabase_Res:
  90. End Function
  91.  
  92. 'First Part C
  93.  
  94. 'control  the DB is a Connected DB
  95.  
  96. Public Function isDBConnected() As Boolean
  97. On Error GoTo isDBConnected_Error
  98. Dim DB As DAO.Database
  99. Dim TD As DAO.TableDef
  100. Dim RS As DAO.Recordset
  101. Dim strTemp As String
  102. Set DB = CurrentDb
  103. 'hier erfolgt auch die fehlerüberprüfung wenn es die falsche db ist geht db.openrecordset in einen error
  104. For Each TD In DB.TableDefs
  105.     If TD.Attributes = dbAttachedTable Then
  106.         Set RS = DB.OpenRecordset("SELECT * FROM " & TD.Name)
  107.     End If
  108. Next TD
  109. isDBConnected = True
  110.  
  111. isDBConnected_Exit:
  112. If Not RS Is Nothing Then RS.Close
  113. Set DB = Nothing
  114. Set TD = Nothing
  115. Set RS = Nothing
  116. Exit Function
  117. isDBConnected_Error:
  118. isDBConnected = False
  119. 'MsgBox Err.Number & " " & Err.Description, vbOKOnly, "Fehler"
  120. GoTo isDBConnected_Exit
  121. End Function
  122.  
  123. 'Second Part for the Form
  124.  
  125. Private Sub BtnOpen_Click()
  126. On Error GoTo BtnOpen_Click_Error
  127. If ConnectDB = False Then
  128.     MsgBox "Verbindung fehlgeschlagen", vbOKOnly + vbCritical, "Fehler bim offnen der Datei"
  129.     Exit Sub
  130. End If
  131.  
  132. Application.Echo False
  133. DoCmd.Hourglass True
  134. Forms!Navigationsformular!Navigationsunterformular.Form.Refresh
  135. Forms!Navigationsformular!Navigationsunterformular!UFrmKasse.Form.Refresh
  136. BtnOpen_Click_Resume:
  137. Application.Echo True
  138. DoCmd.Hourglass False
  139. Exit Sub
  140. BtnOpen_Click_Error:
  141. MsgBox Err.Number & " " & Err.Description, vbCritical + vbOKOnly, "Fehler!"
  142. GoTo BtnOpen_Click_Resume
  143. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement