Zippy <Zi***@111.1.1. 1> wrote in message news:<fn******* *******@thedess ers.com>...
Some months ago, we requested help from this newsgroup on how to replace
the library reference of a database with another library reference,
prior to creating an MDE. I got the following answer from Stephen K.
Young, which does the job:
.... Is there another way of opening the database, or of preventing the code
from running?
Zippy emailed me directly, so I am posting a reply here.
In my application startup code, I use the trick of checking the
command line parameter to see if any special processing should be
performed. For example, if the command line has a substring
";Continue; ", then my startup code knows to skip certain processing
and let automation take control. (Other substrings delimited by
semicolons can be used to perform additional branching.)
In the startup code, I have something like this:
If (InStr(Command$ (), ";Continue; ") <> 0&)
... ' skip user setup
Else
... ' perform user setup
End If
The code below is a sample programmer function that uses automation to
reset the library reference to a compiled version, prior to compiling
the application itself as a .Mde. (This step needed because you cannot
compile an application unless its library references are also
compiled.) This code resides in a separate programmer application, NOT
the user application.
In the code, the following line sets the command line substring to
control the application startup:
appAccess.SetOp tion "Command-Line Arguments", ";Continue; "
In addition, there is some automation code to close any startup forms.
(The user application uses the standard technique of opening a hidden
startup form to know when the user tries to shut down. My startup code
could skip opening the hidden form entirely, based on the command
line, and maybe it does, but I can't remember now and this code checks
for it anyway.)
Anyway, here is the code to reset a library reference. Watch for
word-wrap. I've removed a few lines relevant to me only, so I have not
actually tested this precise version.
' Replace a reference in an external Access session
' Manipulating references may lose global variables in the db, so the
calling program cannot depend on globals.
' Mde or compiled databases cannot manipulate references at all.
' Example: RefReplace "E:\MyPath\MyAp p.Mdb", "MyLib",
"E:\MyPath\MyLi g.Mde" ' change it
' Example: RefReplace "E:\MyPath\MyAp p.Mdb", "MyLib",
"E:\MyPath\MyLi g.Mdb" ' put it back
Public Function RefReplace(strD bName As String, strRefName As String,
strRefPath As String) As Boolean
On Error GoTo ErrExit
If Not isPathOk(strRef Path) Then
MsgBox "The reference path does not exist: " & strRefPath
Exit Function
End If
' open the database via automation
Dim appAccess As Access.Applicat ion
Dim ref As Reference, lngRef As Long
Set appAccess = CreateObject("A ccess.Applicati on." &
Left$(SysCmd(ac SysCmdAccessVer ), 1)) ' use correct version
appAccess.UserC ontrol = True ' this forces appAccess.Visib le = True
automatically
appAccess.SetOp tion "Command-Line Arguments", "continue;" &
cstrCmdDebug
appAccess.OpenC urrentDatabase strDbName, False
' close the hidden form that may be running and set the shutdown
flag
If appAccess.SysCm d(acSysCmdGetOb jectState, acForm, "fsysSystem ") <>
0 Then
appAccess.Forms ("fsysSystem"). gisAppShutDown = True
appAccess.DoCmd .Close acForm, "fsysSystem ", acSaveNo
End If
' close any other forms such as frmDbBrowse or frmClassif
For lngRef = appAccess.Forms .Count - 1 To 0 Step -1
appAccess.DoCmd .Close acForm, appAccess.Forms (lngRef).Name,
acSaveNo
Next lngRef
' remove the prior reference if any
'Debug.Print appAccess.Refer ences.Count
DoEvents
For lngRef = appAccess.Refer ences.Count To 1 Step -1 ' loop
backwards, one-based
Set ref = appAccess.Refer ences(lngRef)
If ref.Name = strRefName Then
appAccess.Refer ences.Remove ref
Exit For
End If
Next lngRef
Set ref = Nothing
' add new reference
DoEvents
On Error Resume Next
appAccess.Refer ences.AddFromFi le strRefPath
If Err <> 0& Then
MsgBox Err.Description , vbCritical, "RefReplace error adding
reference to " & strRefPath
Err.Clear
Else
DoEvents
appAccess.SysCm d 504, 16483 '<- undocumented call for Compile All
without a module open
If Err <> 0& Then
MsgBox Err.Description , vbCritical, "RefReplace error compiling
all"
Err.Clear
End If
If appAccess.isCom piled = False Then
MsgBox "Database " & strDbName & " was not compiled completely,
perhaps there is a compile error.", vbInformation, "Not Compiled"
End If
End If
appAccess.Quit acQuitSaveNone
If Err <> 0& Then
MsgBox Err.Description , vbCritical, "RefReplace in appAccess.Quit"
Err.Clear
End If
Set appAccess = Nothing
If Err <> 0& Then
MsgBox Err.Description , vbCritical, "RefReplace in Set appAccess =
Nothing"
End If
Exit Function
ErrExit:
MsgBox Err.Description
Stop
Resume
End Function