Microsoft KB Archive/209157

= ACC2000: Access Automation Causes Run-Time Error -2147467259 =

Article ID: 209157

Article Last Modified on 6/23/2005

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q209157



For a Microsoft Access 2002 version of this article, see 286126.



SYMPTOMS
While opening Microsoft Access and a form through Automation and setting focus to the the form custom menu bar, the following Microsoft Visual Basic error message occurs:

Run-time error '-2147467259 (80004005)':

Method 'SetFocus' of object 'CommandBarPopup' failed



CAUSE
SetFocus fails because the control, in this case the menu bar, is not visible in the user interface at the time the Automation code executes.



RESOLUTION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To work around this behavior, use either of the following methods:

Method 1: Trap for the Error
Enter a function similar to the following:

Function AutomationTest On Error GoTo AutomationErr



AutomationExit: Exit Function

AutomationErr: If Err.Number = -2147467259 Then Resume   'until control is visible. Else End If End Function

Method 2: Insert a Time Delay
Set focus using code behind the form instead of the Automation code. For example, set the form TimerInterval to 1000 (the value assigned may vary). Then add the following code to the form:

Private Sub Form_Timer Me.TimerInterval = 0 Application.CommandBars.ActiveMenuBar.Controls(1).SetFocus End Sub



MORE INFORMATION
This run-time error occurs only while using Automation. The occurrence varies based on whether or not Access is idle after the form opens, but before the Automation code attempts to set focus to a control. Variations in CPU speeds, program timing, and operating systems may cause this behavior to occur on some computers, but not others.

Steps to Reproduce Behavior
 Start Access. Create Db1.mdb in the C:\ folder. Click Macros on the Database window and then click New.  Add the AddMenu action with the following arguments:          Menu Name: Message Menu Macro Name: Macro2 Status Bar Text: </li> Close the macro and save it as Macro1.</li> Open another new macro.</li> On the View menu, click Macro Names.</li>  Type the Macro Name Message, and then add the MsgBox action with the following arguments: <pre class="fixed_text">   Message: Hello Beep: Yes Type: None Title: </li> Close the macro and save it as Macro2.</li> In the Database window, click Forms, click New, and with Design View selected, click OK.</li> If the Property window is not already open, click Properties on the View menu.</li> On the Other tab in the Property window, scroll to the MenuBar property.</li> Assign Macro1 to the MenuBar.</li> Close the form and save it as Form1.</li> On the File menu, click Close, and on the File menu, click New Database to create another new database.</li> Create Db2.mdb in the C:\ folder.</li> In Db2.mdb, click Modules in the Database window, and then click New.</li>  Add the following code: Option Compare Database Option Explicit

Dim objAccess As Access.Application

Function AutomationTest

Set objAccess = CreateObject("Access.Application")

objAccess.Visible = True objAccess.OpenCurrentDatabase ("C:\Db1.mdb") objAccess.DoCmd.OpenForm "Form1" objAccess.CommandBars.ActiveMenuBar.Controls(1).SetFocus

End Function </li> Click Compile Db2 on the Debug menu and then click Save Db2 on the File menu, saving the module as Module1.</li> Click anywhere within the function AutomationTest and then click Run Sub/UserForm on the Run menu.</li></ol>

NOTE: Once the second instance of Access is generated by the Automation code, click Microsoft Visual Basic on the taskbar to see the error message described in the Symptoms section.

<div class="references_section">