Object variable not set

Freshman

Registered User.
Local time
Today, 02:02
Joined
May 21, 2010
Messages
437
selectSingleNode

Hi all,

I'm getting a "Object variable or with bock variable not set" error in the
"If xml_doc.selectSingleNode("//api_result/call_result/result").Text = "True" Then" line for the code below.
I'm not getting this in A2003 - only my PC running A2013.
I'm referencing to Microsoft XML, v6.0

Code:
Dim xml_doc As New DOMDocument
Dim nde_Asset As IXMLDOMNode
Dim nde_Dividend As IXMLDOMNode

xml_doc.Loadxml (oHttp.ResponseText)

If xml_doc.selectSingleNode("//api_result/call_result/result").Text = "True" Then
  SMSCredits = CDbl(xml_doc.selectSingleNode("//api_result/data/credits").Text)
 Else
  MsgBox "A SMS account has not been created for you. Kindly notify us about this", vbCritical, "Notice"
  SMSCredits = ""
  Exit Sub
End If
 
Last edited:
It usu means the object was not created before you tried to assign it.
So the singlenode must get created first.
 
@Ranman - thanks from what I could Google I noticed there is some change to version from 2013 and up with regards to SingleNode but I couldn't figure out how to fix it.
I would appreciate it if you could guide me with some details on how to do it
Thanks
 
will declaring it explicitly will solve your problem:

Dim xml_doc As MSXML2.DOMDocument60
Dim nde_Asset As MSXML2.IXMLDOMNode
Dim nde_Dividend As MSXML2.IXMLDOMNode

Set xml_doc = New MSXML2.DOMDocument60
 
@arnelpg

Thanks for the reply.
I now have it as below but still getting the same error. I also tried late binding but still same err.
Like I said - only getting it with A2013

Dim xml_doc As MSXML2.DOMDocument60
Dim nde_Asset As MSXML2.IXMLDOMNode
Dim nde_Dividend As MSXML2.IXMLDOMNode

Set xml_doc = New MSXML2.DOMDocument60

xml_doc.Loadxml (oHttp.ResponseText)

If xml_doc.selectSingleNode("//api_result/call_result/result").Text = "True" Then
Me.EventIDtxt = xml_doc.selectSingleNode("//api_result/send_info/eventid").Text
Else
MsgBox xml_doc.selectSingleNode("//api_result/call_result/error").Text, vbInformation, "Notice"
Exit Sub
End If
 
can you put breakpoint on the code, see where the faulty line is.
also create an error handler in your code
 
Yes as per my OP - It is the line:
If xml_doc.selectSingleNode("//api_result/call_result/result") = "True" Then

Also note that if I debug the code there is no error - only when I execute it

Hope that helps
 
how about these changes:

Dim xml_doc As MSXML2.DOMDocument60
Dim nde_Asset As MSXML2.IXMLDOMNode
Dim nde_Dividend As MSXML2.IXMLDOMNode

Set xml_doc = New MSXML2.DOMDocument60

xml_doc.Loadxml (oHttp.ResponseText)

Dim xmlElement As MSXML2.IXMLDOMNode
Set xmlElement = xml_doc.DocumentElement

If xmlElement.selectSingleNode("//api_result/call_result/result").Text = "True" Then
Me.EventIDtxt = xmlElement.selectSingleNode("//api_result/send_info/eventid").Text
Else
MsgBox xmlElement.selectSingleNode("//api_result/call_result/error").Text, vbInformation, "Notice"
Exit Sub
End If
 
Same error...grrr - gosh this is mind boggling hey
 
yes it is, how about replacing those double // with single.
 
by the way how was the the xml structured?
 
can you save the resulting xml using:

xml_doc.Save ("d:\test.xml")

then post the content of this file.
 
As to your question about the xml structure. Not sure but here is the entire Function (I left out the bit bit in my OP):
The code "log in" to the Bulk SMS service I'm subscribed to and checks the credits on my account. Works 100% in Access 2003 & 2007. Developed in 2003 and tested with Runtime version of 2007
On my 2nd machine I have full version of A2013 installed and that is where it fails.

Code:
Public Function CheckCredits()
Dim oHttp As Object
Dim strUrl, strUsername, strPassword, strMessage, strMobileNumber  As String
Set oHttp = CreateObject("Microsoft.XMLHTTP")

strUsername = "xxx"
strPassword = "xxx"

strUrl = "http://www.mymobileapi.com/api5/http5.aspx?Type=credits&username=" & strUsername & "&password=" & strPassword

oHttp.Open "POST", strUrl, False
oHttp.send

Dim xml_doc As New DOMDocument
Dim nde_Asset As IXMLDOMNode
Dim nde_Dividend As IXMLDOMNode

xml_doc.Loadxml (oHttp.ResponseText) ' response text is the xml file being returned

If xml_doc.selectSingleNode("//api_result/call_result/result").Text = "True" Then
  SMSCredits = CDbl(xml_doc.selectSingleNode("//api_result/data/credits").Text)
 Else
  MsgBox "A SMS account has not been created for you. Kindly notify us about this", vbCritical, "Notice"
  SMSCredits = ""
  Exit Function
End If

 Set xml_doc = Nothing
 Set oHttp = Nothing
End Function
 
I replaced the "IF" clause with the single line:
xml_doc.Save ("c:\test.xml")
The resulting file is empty inside...
 
If I do the same on the A2003 machine it gives me this:
- <api_result>
- <data>
<credits>12</credits>
</data>
- <call_result>
<result>True</result>
<error />
</call_result>
</api_result>
 
..
Also note that if I debug the code there is no error - only when I execute it
Then I think you code runs to fast, try putting in some DoEvents just before and after the code line in which you've the problem.
 
try this:
Code:
Public Function CheckCredits()
Dim oHttp As Object
Dim SMSCredits
Dim strUrl, strUsername, strPassword, strMessage, strMobileNumber  As String
Set oHttp = CreateObject("Microsoft.XMLHTTP")

strUsername = "xxx"
strPassword = "xxx"

strUrl = "http://www.mymobileapi.com/api5/http5.aspx?Type=credits&username=" & strUsername & "&password=" & strPassword

oHttp.Open "POST", strUrl, False
oHttp.Send

'Dim xml_doc As New DOMDocument
'Dim nde_Asset As IXMLDOMNode
'Dim nde_Dividend As IXMLDOMNode
'xml_doc.LoadXML (oHttp.ResponseText) ' response text is the xml file being returned
Dim sResult As String
Dim nPos As Long
Dim strResult As String
sResult = oHttp.ResponseText
nPos = InStr(sResult, "<call_result><result>")
If nPos <> 0 Then
    strResult = Mid(sResult, nPos + Len("<call_result><result>"))
    strResult = Left(strResult, InStr(strResult, "</result>") - 1)
End If
'If xml_doc.SelectSingleNode("//api_result/call_result/result").Text = "True" Then
If strResult = "True" Then
    nPos = InStr(sResult, "<credits>")
    strResult = Mid(sResult, nPos + Len("<credits>"))
    strResult = Left(strResult, InStr(strResult, "</credits>") - 1)

  'SMSCredits = CDbl(xml_doc.SelectSingleNode("//api_result/data/credits").Text)
  SMSCredits = CDbl(strResult)
  
 Else
  MsgBox "A SMS account has not been created for you. Kindly notify us about this", vbCritical, "Notice"
  SMSCredits = ""
  Exit Function
End If

 Set xml_doc = Nothing
 Set oHttp = Nothing
End Function
 
@arnelgp - I tried your code and at least did not get an error but

MsgBox nPos = 0
MsgBox sResult = Lots of text but not what I was expecting. Not sure where it is coming from
MsgBox strResult = Null

I created a login for you in case you want to test in on your side
strUserName and strPassword are both "arnelgp"

Thanks again

PS: Leaving the office now (time zone diff) will check in the morning
 
Last edited:

Users who are viewing this thread

Back
Top Bottom