Public Function MDIClient_SwitchObject(sObjectName As String, sClass As String) As Boolean
On Error GoTo Error_Handler
Dim oUIA As UIAutomationClient.CUIAutomation
Dim oAccess As UIAutomationClient.IUIAutomationElement
Dim oCondition As UIAutomationClient.IUIAutomationCondition
Dim oObject As UIAutomationClient.IUIAutomationElement
Set oUIA = New CUIAutomation
Set oAccess = oUIA.ElementFromHandle(ByVal Application.hWndAccessApp)
If Not (oAccess Is Nothing) Then
Set oCondition = oUIA.CreateAndCondition(oUIA.CreatePropertyCondition(UIA_NamePropertyId, sObjectName), _
oUIA.CreatePropertyCondition(UIA_ClassNamePropertyId, sClass))
Set oObject = oAccess.FindFirst(TreeScope_Subtree, oCondition)
If Not (oObject Is Nothing) Then
oObject.SetFocus
MDIClient_SwitchObject = True
Else
Debug.Print "'" & sObjectName & "' not found."
End If
Else
Debug.Print "Can't locate the database windows."
End If
Error_Handler_Exit:
On Error Resume Next
If Not oObject Is Nothing Then Set oObject = Nothing
If Not oCondition Is Nothing Then Set oCondition = Nothing
If Not oAccess Is Nothing Then Set oAccess = Nothing
If Not oUIA Is Nothing Then Set oUIA = Nothing
Exit Function
Error_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: MDIClient_SwitchObject" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Function