I am trying to pull address information from an XML document using XPath and populating fields in an access database with these values. I am able to get everything I need until I have to use predicates (I think this is the way to do it). Here is a sample of the XML:
<GeocodeResponse>
<status>OK</status>
<result>
<type>street_address</type>
<formatted_address>
Street Number Street Address, Kansas City, MO 64120, USA
</formatted_address>
<address_component>
<long_name>Street Number</long_name>
<short_name>Street Number</short_name>
<type>street_number</type>
</address_component>
<address_component>
<long_name>Street Address</long_name>
<short_name>Street Address</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Kansas City</long_name>
<short_name>KCMO</short_name>
<type>locality</type>
<type>political</type>
</address_component>
I would like to pull the text for street number, name, and city. Here is what I have in my VBA code:
rst.Edit
rst("Str_Num").Value =.SelectSingleNode("GeocodeResponse/result/address_component/long_name[1]").Text
rst.Update
rst.Edit
rst("Str_Name").Value =SelectSingleNode("GeocodeResponse/result/address_component/long_name[2]").Text
rst.Update
rst.Edit
rst("City").Value =SelectSingleNode("GeocodeResponse/result/address_component/long_name[3]").Text
rst.Update
rst.Edit
rst("Zip").Value =SelectSingleNode("GeocodeResponse/result/address_component/long_name[8]").Text
rst.Update
When ran I get error 91 Object variable or With block variable not set.
The code just above this works as I expected it to but I’m having an issue understanding the predicates. The above code:
rst.Edit
rst("frm_addr").Value = .SelectSingleNode("//formatted_address").Text
rst.Update
rst.Edit
rst("lat").Value = .SelectSingleNode("//location/lat").Text
rst.Update
rst.Edit
rst("lng").Value = .SelectSingleNode("//location/lng").Text
rst.Update
So what do I have wrong?
Thanks!
There are several
address_componentstags per result, not severallong_names peraddress_component. Modify your XPath expressions toand so on.