jtequia
asked on
VBA script to toggle on and off database link
I have an access db front end app and I want to link to more than 1access db back end. I want my system ( Front end ) to be able to establish the link once a certain module is open ( application form ) and remove link once the form is closed. So i need to be able to write down a script ( VBA routine ) to turn the link on and off. I know that i can do it with refresh link using a connect property, but i want to know how to establish the link for the first time using VBA from a password-protected database.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Public Function MDMM_BuHstEvLogLinkCtl(sOp
Dim sFx As String: sFx = "MDMM_BuHstEvLogLinkCtl"
Select Case sOp
Case "UnLink"
On Error Resume Next
DoCmd.DeleteObject acTable, "YourTableNameHere"
If Err.Number = 0 Or Err.Number = 7874 Then
'ignore; Err 0 means no error occurred, Err 7874 means object not found (nothing to delete)
Else
GoTo MDMM_BuHstEvLogLinkCtl_Err
End If
Case "Link"
'be sure link does not already exist ... call this same function with the UnLink op
Me.MDMM_BuHstEvLogLinkCtl ("UnLink")
'Now create the link ...
Dim sPath As String
On Error GoTo MDMM_BuHstEvLogLinkCtl_Err
sPath = "YourFolderPathNameHere"
DoCmd.TransferDatabase acLink, "Microsoft Access", sPath & "MDMMData.mdb", acTable, "YourTableNameHere", "YourTableNameHere"
End Select
MDMM_BuHstEvLogLinkCtl_Exi
Err.Clear
Exit Function
MDMM_BuHstEvLogLinkCtl_Err
MsgBox "An unexpected error has occured!" & vbCrLf & vbCrLf & _
"Error Information --------------------" & vbCrLf & _
"Number: " & Err.Number & vbCrLf & _
"Description: " & Err.Description & vbCrLf & _
"Procedure: " & sFx & vbCrLf & _
"Operation: " & sOp & vbCrLf & vbCrLf & _
"PLEASE ... take a screen shot of this error message showing as much of the screen as possible and email it to the Database Administrator.", 48, "MASS Database Maintenance"
GoTo MDMM_BuHstEvLogLinkCtl_Exi
End Function
Now ... I forget exactly how to include the password ... but form Help ... it shows this:
DoCmd.TransferDatabase acLink, "ODBC Database", _
"ODBC;DSN=DataSource1;UID=
& "DATABASE=pubs", acTable, "Authors", "dboAuthors"
So, if you can merge that in, I'm thinking it might work ...
mx