ACC97: No Error When AddFromFile Creates Duplicate ProcedureID: Q171072
|
Advanced: Requires expert coding, interoperability, and multiuser skills.
The AddFromFile method does not generate an error when it adds a duplicate
procedure to a module.
When the AddFromFile method inserts a procedure into a module, Visual Basic
for Applications tries to compile all code in the target module first, and
then inserts the procedure if successful.
If you use the AddFromFile method multiple times to insert the same
procedure into the same target module, Visual Basic for Applications does
not detect a compile error until the third attempt. On the first attempt,
Visual Basic for Applications successfully compiles the code, and then
inserts the procedure from the text file. On the second attempt, Visual
Basic for Applications successfully compiles the code in the target module,
which now includes one instance of the procedure from the text file; it
then inserts a second copy of the procedure from the text file. On the
third attempt, Visual Basic for Applications detects a compile error
because there are two instances of the same procedure in the target module.
To prevent the AddFromFile method from inserting a duplicate procedure, use
the Find method of the Module object to determine if the procedure already
exists in the module. To do so, follow these steps:
Function ImportProc(vbProcName As String)
Dim mdl As Module
Dim vbLineStop As Long
Set mdl = Modules(0)
vbLineStop = mdl.CountOfLines
If Not mdl.Find(vbProcName, 1, 1, vbLineStop, 1, False) Then
mdl.AddFromFile "C:\My Documents\Test.txt"
Else
MsgBox "'" & vbProcName & "' already exists in module."
End If
End Function
?ImportProc("Sub AddFromFileTest()")Note that the AddFromFileTest procedure is successfully inserted into the module.
'Sub AddFromFileTest()' already exists in module.
This behavior is by design.
Sub AddFromFileTest()
MsgBox "Hello"
End Sub
Option Explicit
Function ImportProc()
Dim mdl As Module
Set mdl = Modules(0)
mdl.AddFromFile "C:\My Documents\Test.txt"
End Function
?ImportProc()
Compile Error:
Ambiguous name detected: AddFromFileTest
For more information about using the AddFromFile method, search the Help
Index for "AddFromFile method."
For more information about using the Find method, search the Help Index
for "Find method", and then "Find Method (Microsoft Access Reference)".
Keywords : kbcode kbprg MdlProb PgmErr
Version : 97
Platform : WINDOWS
Issue type : kbprb
Last Reviewed: May 13, 1999