. .net> said:
To replace standard module, programatically.
To provide user-initiated, automated program update.
Since module cannot replace itself without crashing,
something external to it must do the replacing.
Ok!
Sorry this has taken a while, but I do not have Access on this PC. Most of
my replies are from memory. Your question was a bit too tricky to answer
from memory. And I have been busy recently, so although I have continued to
answer other questions, I have not had time to investigate your question
properly.
Here is what you need.
(1) Create a database named OLD.MDB that contains a module named AAA that
contains a public procedure named Z that displays a message. (Say, "Original
Z!") Open that db, go to the debug window (ctrl-G), type "Z" (without the
quotes) and press return. The "original" message should be displayed. Now
close that database.
(2) Now create a second database NEW.MDB in the same folder as OLD.MDB. Open
NEW.MDB, and add a module named BBB that contains a public procedure also
named Z, that displays a different message. (Say, "New Z!") Go to the debug
window (ctrl-G), type "Z" (without the quotes) and press return. The "new"
message should be displayed.
(3) Now add a new module, named whatever you like, to NEW.MDB, containing
the following code:
'----- START CODE -----
Option Explicit
Option Compare Database
' This procedure replaces module AAA in database OLD.MDB, with the
' content of module BBB from the current database. It assumes that:
' o OLD.MDB is in the current directory;
' o OLD.MDB does have a module named AAA;
' o the current db does have a module named BBB.
Public Sub UPDATE()
Dim sOldDB As String
' locate the old db.
sOldDB = DBEngine(0)(0).Name
While Right$(sOldDB, 1) <> "\": sOldDB = Left$(sOldDB, Len(sOldDB) - 1):
Wend
sOldDB = sOldDB & "OLD.MDB"
msgbox "UPDATING " & sOldDB
' delete module "AAA" from that db. In doing this, I'm assuming that
' CopyObject will fail if there is an existing object of that name.
' If that is not the case - and CopyObject will overwrite an existing
' object of the same name - omit the next code.
Dim oAccess As Object
Set oAccess = CreateObject("Access.Application")
With oAccess
.OpenCurrentDatabase sOldDB
.DoCmd.DeleteObject acModule, "AAA"
' delete more modules here, if required.
End With
oAccess.Quit
Set oAccess = Nothing
' now copy module "BBB" from THIS db, to module "AAA" of the old db.
DoCmd.CopyObject sOldDB, "AAA", acModule, "BBB"
' copy more modules here, if required.
MsgBox "DONE"
End Sub
'----- END CODE -----
(4) Now close everything, to get to the situation where the user has
downloaded NEW.MDB, and is ready to use it to update his OLD.MDB.
(5) Open NEW.MDB, go to the debug window (ctrl-G), type "UPDATE" (without
the quotes) and press return. This will replace the content of module AAA in
OLD.MDB (in that folder), with the content of module BBB from NEW.MDB. You
can confirm this by closing NEW.MDB, opening OLD.MDB, going to the debug
window, then typing "z" (without the quotes) and pressing return. This will
display the NEW message (perhaps "New Z!"), not the old one ("Original Z!").
When you've got this working, you could extend it as appropriate. For
example, the two MDB's might be in different directories. And, the new & old
modules will probably have the same name - not different ones.
Also, you should consider what to do when errors occur. For example, what if
the process successfully deletes the old module(s) from OLD.MDB, but fails
to add the new module(s) from NEW.MDB? Then you would need some way to go
backwards to the original OLD.MDB, to get the system working again.
HTH,
TC