Below is the code for a form that will accomplish what you want. There is
some irrelevant code, but you can delete it. Some of the code is not up to
my personal standard, but I did not do the original version, I just expanded
it to make it easier to use.
You will have to create a form and add the controls you need to make this
happen.
First, you will need the two API modules referenced in the sites below.
API Code for common dialog
http://www.mvps.org/access/api/api0001.htm
API Code for UNC path
http://www.mvps.org/access/api/api0003.htm
Form Module Code
Option Compare Database
Option Explicit
Dim UseDirName As String
Function OkToLink() As Boolean
OkToLink = Not IsNull(Me.datapath) And Not IsNull(Me.localpath)
End Function
Function NewDatabasePath(strDbType) As String
Dim varGetFileName As Variant 'Pass to Common Dialog to open workbook
Dim strDefaultDir As String 'Pass Directory to search for common dialog
Dim strfilter As String 'Limit common dialog search to excel workbooks
Dim lngFlags As Long 'Hide readonly check box on common dialog
Dim strFileName As String 'Initial File Name to Display
Dim strDialog As String
strDialog = "Select " & strDbType & " Database"
'Flags Hides the Read Only Check and Only allow existing files
lngFlags = ahtOFN_HIDEREADONLY Or ahtOFN_FILEMUSTEXIST
'Set filter to show only Access Databases
strfilter = ahtAddFilterItem(strfilter, "Access (*.mdb,*.mde)",
"*.MDB;*.MDA")
'Call the Open File Dialog
Do While True
varGetFileName = ahtCommonFileOpenSave( _
OpenFile:=True, _
Filter:=strfilter, _
Flags:=lngFlags, _
DialogTitle:=strDialog)
If varGetFileName = "" Then 'User Clicked CANCEL
If MsgBox("Retry to select a Database " & vbNewLine & "Or" _
& vbNewLine & "Cancel to Quit", vbExclamation +
vbRetryCancel, _
"No Database Selected") = vbCancel Then
Exit Do
End If
Else
Exit Do
End If
Loop
NewDatabasePath = varGetFileName
End Function
Private Sub btnDATA_Click()
Dim st As String
Dim Msg As String
Dim strPath As String
strPath = NewDatabasePath("CISCMSDATA")
If strPath = "" Then
DoCmd.Close acForm, Me.Name, acSaveNo
Else
If Left(strPath, 1) < "E" Then
Me.datapath = strPath
Else
Me.datapath = fGetUNCPath(Left(strPath, 2)) & Right(strPath,
Len(strPath) - 2)
End If
Me.cmdChangeLinks.Enabled = OkToLink
End If
End Sub
Private Sub btnLOCAL_Click()
Dim st As String
Dim Msg As String
Dim strPath As String
strPath = NewDatabasePath("LOCALDATA")
If strPath = "" Then
DoCmd.Close acForm, Me.Name, acSaveNo
Else
If Left(strPath, 1) < "E" Then
Me.localpath = strPath
Else
Me.localpath = fGetUNCPath(Left(strPath, 2)) & Right(strPath,
Len(strPath) - 2)
End If
Me.cmdChangeLinks.Enabled = OkToLink
End If
End Sub
Private Sub cmdChangeLinks_Click()
' Relink all NGCMSdata.mdb, and SharedData as specified by the user.
' (offbook.mdb is assumed to always be in the "\\iispi02\ngcms ...
\shareddata\" folder.)
Dim DataCnt As Integer
Dim datafile As Integer
Dim dbs As Database
Dim intcount As Integer
Dim LocalCnt As Integer
Dim localfile As Integer
Dim Response As Variant
Dim tdf As TableDef
Set dbs = CurrentDb()
On Error GoTo linkerror
' Be sure the user has specified all fields!
If Len(Nz(Me.datapath)) = 0 Or Len(Nz(Me.localpath)) = 0 Then
MsgBox "You must specify all the linkage paths on the form before I can
relink!", vbExclamation, "Specify linkage paths."
Exit Sub
End If
DataCnt = 0
LocalCnt = 0
'MsgBox "This will take several minutes, don't mess with anything til it
says 'files relinked'"
DoCmd.Hourglass (True)
intcount = 0
Response = SysCmd(acSysCmdInitMeter, "Relinking Tables - Please Wait ",
dbs.TableDefs.Count - 1)
For Each tdf In dbs.TableDefs
intcount = intcount + 1
Response = SysCmd(acSysCmdUpdateMeter, intcount)
If Len(tdf.Connect) > 0 Then
' Its a linked table. Re-link
datafile = InStr(1, tdf.Connect, "CISCMSdata")
localfile = InStr(1, tdf.Connect, "LocalData")
If localfile > 0 Then
tdf.Connect = ";database=" & Me.localpath
tdf.RefreshLink
LocalCnt = LocalCnt + 1
ElseIf datafile > 0 Then
tdf.Connect = ";database=" & Me.datapath
tdf.RefreshLink
DataCnt = DataCnt + 1
End If
Else
' Not a connected table; don't do anything.
End If
Next tdf
DoCmd.Hourglass (False)
Response = SysCmd(acSysCmdRemoveMeter)
MsgBox "Files relinked! " & vbCr & DataCnt & " data files and " & LocalCnt
& " local Files were relinked"
Exit Sub
linkerror:
MsgBox "error in relinking " & tdf.Name & " " & Err.Description
DoCmd.Hourglass (False)
Response = SysCmd(acSysCmdRemoveMeter)
End Sub
Private Sub cmdlink_Click()
' NOTE: This sub does not work.
Exit Sub ' ...until it is fixed or deleted.
Dim dbs As Database
Dim intcount As Integer
Dim tdf As TableDef
Dim str As String
Dim datafile As Integer
Dim localfile As Integer
Set dbs = CurrentDb()
On Error GoTo ErrorHandler
For intcount = 0 To dbs.TableDefs.Count - 1
Set tdf = dbs.TableDefs(intcount)
If Len(tdf.Connect) > 0 Then
' Its a linked table
str = tdf.Connect
datafile = InStr(1, tdf.Connect, "data.mdb")
localfile = InStr(1, tdf.Connect, "localdata")
If localfile > 0 Then
tdf.Connect = Me.localpath
ElseIf datafile > 0 Then
tdf.Connect = Me.datapath
' MsgBox " tdf.connect is " & tdf.connect
' Err = 0
' On Error Resume Next
MsgBox intcount & tdf.Name
tdf.RefreshLink
End If
Else
End If
Next intcount
MsgBox "files relinked!"
Exit Sub
ErrorHandler:
MsgBox "There was an error in linking the files"
End Sub
Private Sub Command23_Click()
On Error GoTo Err_Command23_Click
DoCmd.Close
Exit_Command23_Click:
Exit Sub
Err_Command23_Click:
MsgBox Err.Description
Resume Exit_Command23_Click
End Sub
Private Sub LockOutUsers_Click()
Dim db As Database
Dim rst As Recordset
Set db = CurrentDb()
Set rst = db.OpenRecordset("tblsettings", dbOpenDynaset)
rst.Edit
rst("logoff") = True
rst.Update
MsgBox "All users will be logged out of NGCMS and the system will be shut
down"
End Sub
Private Sub cmdUpdateContact_Click()
On Error GoTo Err_cmdUpdateContact_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmContactUpdates"
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_cmdUpdateContact_Click:
Exit Sub
Err_cmdUpdateContact_Click:
MsgBox Err.Description
Resume Exit_cmdUpdateContact_Click
End Sub
*********End Code