LarryE
Well-known member
- Local time
- Today, 07:36
- Joined
- Aug 18, 2021
- Messages
- 1,112
I use the following code to select a backend file and connect to it:
Maybe it will help you.
Also, ACCESS keeps the current connection information in the MSysObjects table. It can be viewed with this query:
SELECT DISTINCT MSysObjects.Database
FROM MSysObjects
GROUP BY MSysObjects.Database, MSysObjects.ParentId, MSysObjects.Name, MSysObjects.Type
HAVING (((MSysObjects.Name) Not Like "*TMP*") AND ((MSysObjects.Type)=6));
I named my query CurrentConnection
Code:
Public Function ConnectFileDialog()
On Error GoTo ConnectFileDialog_Error
Dim ConnectDataFileDialog As FileDialog
Set ConnectDataFileDialog = Application.FileDialog(msoFileDialogFilePicker)
Dim SelectedFile As Variant
Dim db As DAO.Database
Set db = CurrentDb
With ConnectDataFileDialog
.AllowMultiSelect = False
.Title = "Select A Backend File"
.ButtonName = "Connect"
.Filters.Clear
.Filters.Add "Access Files", "*.accdb", 1
.FilterIndex = 1
If .Show = -1 Then 'If user selected a file
For Each SelectedFile In .SelectedItems
'MsgBox SelectedFile
Dim tdf As DAO.TableDef
Dim BackEnd As String
BackEnd = ";Database=" & SelectedFile & ""
If Len(BackEnd) > 1 Then
GoTo FinishConnection
Exit Function
Else
Exit Function
End If
FinishConnection:
For Each tdf In db.TableDefs
If Len(Left$(tdf.Connect, 1)) > 0 Then
Set tdf = db.TableDefs(tdf.Name)
tdf.Connect = BackEnd
tdf.RefreshLink
End If
Next tdf
Next
Else 'If user cancelled
Set FileOpenDialog = Nothing
If Forms.Count > 0 Then
Exit Function
Else
DoCmd.Quit
End If
End If
Set FileOpenDialog = Nothing
End With
Exit Function
ConnectFileDialog_Error:
DoCmd.CancelEvent
'msgbox Err.DESCRIPTION
Resume Next
Exit Function
End Function
Maybe it will help you.
Also, ACCESS keeps the current connection information in the MSysObjects table. It can be viewed with this query:
SELECT DISTINCT MSysObjects.Database
FROM MSysObjects
GROUP BY MSysObjects.Database, MSysObjects.ParentId, MSysObjects.Name, MSysObjects.Type
HAVING (((MSysObjects.Name) Not Like "*TMP*") AND ((MSysObjects.Type)=6));
I named my query CurrentConnection
Last edited: