VBA With Microsoft Access - Check if object exists

JMK picture JMK · Oct 19, 2011 · Viewed 52.8k times · Source

I'm using VBA with Microsoft Access.

I'm setting an object to an item inside a WebBrowserControl that sometimes exists, sometimes doesn't.

Dim myWebBrowser As Object
Dim myItemInsideWebpage As Object

Set myWebBrowser = Me.WebBrowser0.Object
Set myItemInsideWebpage = myWebBrowser.Document.GetElemendById("myDiv")

If 'myDiv' exists, awesome, if not I want Access to let me know so I can deal with it.

Answer

JimmyPena picture JimmyPena · Oct 19, 2011

I believe it would be something like

If myItemInsideWebpage Is Nothing Then
' doesn't exist
Else
' does exist
End If

You may need to preface your 'Set' statement with 'On Error Resume Next' in case an error is thrown when 'myDiv' does not exist.