Microsoft KB Archive/286126

= You may receive the &quot;Run-time error -2147467259&quot; error message when you open a form by using Automation in Visual Basic .NET =

Article ID: 286126

Article Last Modified on 3/29/2007

-

APPLIES TO


 * Microsoft Office Access 2003
 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q286126



Moderate: Requires basic macro, coding, and interoperability skills.

This article applies only to a Microsoft Access database (.mdb).

For a Microsoft Access 2000 version of this article, see 209157.



SYMPTOMS
When you open Microsoft Access and a form through Automation and set focus to the form's custom menu bar, you receive the following Microsoft Visual Basic error message:

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 that the Automation code is executing.



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 by using code behind the form instead of the Automation code. For example, set the form's TimerInterval to 1000 (the value assigned may vary), and 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
The run-time error that is mentioned in the &quot;Symptoms&quot; section occurs only when you use 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 the Behavior
 Start Access. Create a new database named Db1.mdb in the C:\ folder. In the Database window, click Macros under Objects, and then click New.  Under Action, add the AddMenu action, and under Action Arguments, set the following arguments:          Menu Name: Message Menu Macro Name: Macro2 Status Bar Text: </li> Save the macro as Macro1, and then close it.</li> Create another new macro.</li> On the View menu, click Macro Names.</li>  Under Macro Name, type Message, under Action, add MsgBox, and under Action Arguments, set the following arguments: <pre class="fixed_text">   Message: Hello Beep: Yes Type: None Title: </li> Save the macro as Macro2, and then close it.</li> In the Database window, click Forms under Objects, click New, click Design View, and then click OK.</li> If the property sheet is not already displayed, click Properties on the View menu.</li> Click the Other tab in the property sheet, and then scroll down to the MenuBar property.</li> Set the MenuBar property to Macro1.</li> Save the form as Form1, and then close the form.</li> On the File menu, click Close, and then on the File menu again, 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(&quot;Access.Application&quot;)

objAccess.Visible = True objAccess.OpenCurrentDatabase (&quot;C:\Db1.mdb&quot;) objAccess.DoCmd.OpenForm &quot;Form1&quot; 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. Save the module as Module1.</li> Click anywhere within the AutomationTest function, and then click Run Sub/UserForm on the Run menu.</li></ol>

NOTE: After the second instance of Access is generated by the Automation code, click Microsoft Visual Basic on the taskbar to see the error message that is mentioned in the &quot;Symptoms&quot; section.

Additional query words: run time error 2147467259 80004005 method setfocus of object commandbarpopup failed ACC2002 ACC2003

Keywords: kbprogramming kbautomation kberrmsg kbprb KB286126

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.