Link to home
Start Free TrialLog in
Avatar of DragonRebornRand
DragonRebornRand

asked on

Missing node results in object variable not set error

I am parsing through an xml file using VBA in MS access, and sometimes a particular node isnt in the xml file, and sometimes it is.

when I use the code below, if the "Phone" node isnt there, then I recieve an error that the Object Variable is not set. Is there a way to skip the code line if the object doesnt exist, or skip the line if the error occurs?

FundPhone = xmlDoc.documentElement.selectSingleNode("Phone").text
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Try error handling around that line:

ON Error goto EH
FundPhone = xmlDoc.documentElement.selectSingleNode("Phone").text 

' The rest of your code goes here


Exit Sub ' Or function  '<--- Exit normally if no errors

EH:
   If Err.Number = {enter the error number here}  then resume next '<-***** You need to substitute the error code here
   msgbox Err.description
End sub

Open in new window

Can you post the entire code please?

Sometimes you need a syntax like this:

Dim x as SomeObject
Set x=SomeValue

So presuming that you declared xmlDoc somewhere (and declared it correctly), ...you might need to do this:

Set FundPhone = xmlDoc.documentElement.selectSingleNode("Phone").text

ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My suggestion is to check for null before you attempt to use the variable:

Set node = xmlDoc.documentElement.selectSingleNode("Phone")

If Not node Is Nothing Then
    FundPhone = .text
End If

Open in new window

Line 4 should have included "node":

FundPhone = node.text

Open in new window

Avatar of DragonRebornRand
DragonRebornRand

ASKER

Error Handling works. Thanks mbizup