How can I get Yahoo Geocodes into Access?

E

efandango

I want to get a list of geocodes into my address database using Yahoo's
geocoding service.

I have the database.
I have the Yahoo geo
I have the username for the geocoding service
I even have an Excel spreadsheet that will do exactly what I want to do with
Excel VBA Macros

but I can't figure out how to 'convert' the excel rows looping into my
Access recordset. There's more that I can't figure out, but that gives you an
outline of exactly where I am at the moment, which I guess is at the foot of
the mountain...

I have tried to get my head around the Excel VBA (which looks seductively
simple...) but just get brain freeze when I try and 'frame' the whole thing
in my mind

any help or guidance would be gratefully appreciated.

( I can paste the Excel VBA, but didn't want to just 'dump' it here without
putting out the question first.)

thanks in advance

Eric
 
T

Tom Wickerath

Hi Eric,

I'm not familiar with Yahoo's Geocodes but, if you can post a copy of an
Excel file that has the data in it that you want to bring into Access, then
perhaps I or someone else can assist you further. Do you have a personal web
space available at your ISP, to upload a small .xls file?


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 
E

efandango

What I want to do is:

1. pull a set of addresses from a query
2. geocode them with Yahoo's geocoder
3. write the Lat and Lon back to the respective records within the table.

at the moment, the spreadsheet works on excel rows; but I want to
'translate' that action into Access records (fields).

regards
Eric
 
T

Tom Wickerath

Hi Eric,

I downloaded the spreadsheet file, but there are no latitude and longitude
values shown in cells A6:B8, for your three test records. I attempted to
click on "Geocode all rows", but I get prompted for a Yahoo ID that I do not
have.

However, if you are seeing valid latitude and longitude values in your copy
of the spreadsheet, then you should be able to easily get these values into
your Access table. I highly recommend that when you export your query in
Access, to create the spreadsheet that needs the latitude and longitude
values looked up, that you include the primary key field for the table in
question. If your table does not currently include a primary key field, then
add a new field (perhaps name it pkCustomer or whatever looks reasonable).
This can simply be an autonumber data type.

After retrieving your latitude and longitude data, I would create a named
range within the Excel spreadsheet, which includes the four fields: Latitude,
Longitude, Precision (if you care about this field), and pkCustomer. Are you
familiar with the process of selecting rectangular blocks of data within
Excel, in order to create named ranges? Working with named ranges helps to
ensure that you only get exactly what you want to get, and nothing more.

Now, you simply need an UPDATE query, to update your existing records.
Create a linked table to the named range in your Excel spreadsheet. Create a
new query. Select your existing table that needs to be updated, along with
the linked Excel file. Drag a join line between the two primary key fields,
to prevent a cartesian product query result. Add appropriate criteria for the
table in your database, so that only records that need to be updated are
included in the resulting recordset. This might include, for example, using
an Is Null criteria for the Latitude or Longitude fields (make it an OR so
that records with either null Latitude or null Longitude [or both] are
included in your recordset. When you get this SELECT query returning the
desired records, convert it to an UPDATE query (Query | Update Query, if you
are using Access 2003 or lesser version). You should see a new Update To row
in the QBE (Query By Example) grid. Type in the fully qualified names of the
fields from your linked table (a fully qualified name includes the name of
the table plus the name of the field, so try to use a short name for your
linked table). For example:

Field Name: Latitude
Update To: [MyExcel].[Latitude]
Criteria: Is Null

Field Name: Longitude
Update To: [MyExcel].[Longitude]
Criteria: Is Null

Does this help?


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 
T

Tom Wickerath

PS. Regarding the Is Null criteria I suggested, you really don't need this
for the Update query, since an Inner Join line between your table and the
linked range in your Excel file should be sufficient to select only the
records intended for updating. However, you may wish to include the Is Null
criteria such that any records with a null latitude OR null longitude are
included in your initial SELECT query, which is used to populate the Excel
spreadsheet.


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
http://www.access.qbuilt.com/html/expert_contributors.html
 
E

efandango

Tom,

I think I have not explained myself clearly enough, and fear that you have
been unintentionaly misled. I don't want to import the data from Excel.

Instead I want to replicate the actual VBA procedure 'engine' that exists
within the Excel spreadsheet into an MS Access VBA 'engine', mainly centering
around the excel 'geocoderAddressLookup' procedure. I don't need any of the
other stuff in there such as the address cleaning procedure because my Access
data is clean and ready to go. I already have my tables/queries set up in
Access, I just need someone to tell me how to re-engineer the bit where excel
looks at rows of addresses and parses them to yahoo, end then brings them
back into excel as geocodes. My Access version would do the same, only it
would instead of addressing rows like excel, it would instead address
records.

Does that make sense?

regards

Eric






Tom Wickerath said:
Hi Eric,

I downloaded the spreadsheet file, but there are no latitude and longitude
values shown in cells A6:B8, for your three test records. I attempted to
click on "Geocode all rows", but I get prompted for a Yahoo ID that I do not
have.

However, if you are seeing valid latitude and longitude values in your copy
of the spreadsheet, then you should be able to easily get these values into
your Access table. I highly recommend that when you export your query in
Access, to create the spreadsheet that needs the latitude and longitude
values looked up, that you include the primary key field for the table in
question. If your table does not currently include a primary key field, then
add a new field (perhaps name it pkCustomer or whatever looks reasonable).
This can simply be an autonumber data type.

After retrieving your latitude and longitude data, I would create a named
range within the Excel spreadsheet, which includes the four fields: Latitude,
Longitude, Precision (if you care about this field), and pkCustomer. Are you
familiar with the process of selecting rectangular blocks of data within
Excel, in order to create named ranges? Working with named ranges helps to
ensure that you only get exactly what you want to get, and nothing more.

Now, you simply need an UPDATE query, to update your existing records.
Create a linked table to the named range in your Excel spreadsheet. Create a
new query. Select your existing table that needs to be updated, along with
the linked Excel file. Drag a join line between the two primary key fields,
to prevent a cartesian product query result. Add appropriate criteria for the
table in your database, so that only records that need to be updated are
included in the resulting recordset. This might include, for example, using
an Is Null criteria for the Latitude or Longitude fields (make it an OR so
that records with either null Latitude or null Longitude [or both] are
included in your recordset. When you get this SELECT query returning the
desired records, convert it to an UPDATE query (Query | Update Query, if you
are using Access 2003 or lesser version). You should see a new Update To row
in the QBE (Query By Example) grid. Type in the fully qualified names of the
fields from your linked table (a fully qualified name includes the name of
the table plus the name of the field, so try to use a short name for your
linked table). For example:

Field Name: Latitude
Update To: [MyExcel].[Latitude]
Criteria: Is Null

Field Name: Longitude
Update To: [MyExcel].[Longitude]
Criteria: Is Null

Does this help?


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________

efandango said:
What I want to do is:

1. pull a set of addresses from a query
2. geocode them with Yahoo's geocoder
3. write the Lat and Lon back to the respective records within the table.

at the moment, the spreadsheet works on excel rows; but I want to
'translate' that action into Access records (fields).

regards
Eric
 
E

efandango

Tom,

These are the main procedures within the Excel macro.

GEOCODING is done using the following layers

geocodeSelectedRows
(for each row call geocodeRow)

geocodeRow(r)
(check that row is geocodable, pass to geocode, parse results)

geocode(street,city,state,zip)
(clean all variables, pass url to geocoderAddressLookup,
if no result then try different permuatations of address)

geocoderAddressLookup
(query geocoder.us, return result, marshal results)

As I see it, I only really need to somehow convert one of them:

geocoderAddressLookup (with the functionality of 'geocodeSelectedRows')
 
T

Tom Wickerath

Hi Eric,

Yes, I did misunderstand. I thought you wanted to use the application, as
is, to export your address data to the existing Excel spreadsheet, get the
latitude and longitude data, and then bring this data back into your Access
application. I don't know if it is even possible to use the same, or a
modified version, of this code in your Access application. And, I don't have
the time or energy to try to pursue that effort. Sorry.


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 
E

efandango

ok,

in my naivety i assumed it would not be too difficult, thanks for taking the
time to look at this for me.

regards

Eric
 
T

Tom Wickerath

Hi Eric,

It may be very easy....I simply don't know at this point. I do know that I'd
first need to sign up for an account, in order to throughly test any
converted code.

In the meantime, I went ahead and plugged in some phony latitude and
longitude numbers, into the three records, and was able to run the update
query as planned.


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 
E

efandango

Tempting as the update query scenario is for me personally, I am mindful that
eventually other users will use this database, and will not want to maintain
the data in Excel as well as Access, so it makes sense to use the already
exisitng data in Access, which is why I was so keen to translate that
essential procedure into something that would work in Access, I am not a
seasoned VBA coder, and as such do not know or understand all of the various
keywords/functions that are available to me in VBA; but somehow, I'm going to
try my best to make it happen.

regards


Eric
 
T

Tom Wickerath

Hi Eric,
...and will not want to maintain the data in Excel as well as Access...

You would not need to do this. You'd simply use Excel whenever you needed to
get new latitude and longitude data. There's no reason to have to maintain
the data in Excel, once the latitude, longitude and precision data is brought
into the database.
I am not a seasoned VBA coder, and as such do not know or understand
all of the various keywords/functions that are available to me in VBA...

The Me keyword in Access VBA is a reference to a form or report. The default
property, if not specified, is the name of the object (ie. the name of the
form or report). Therefore, this keyword is not valid in a stand-alone module
(a module that is not associated with a form or report).

In general, you can select a keyword in the VBE (Visual Basic Editor), and
press F1 to open context-sensitive help on the keyword. Make sure to include
Option Explicit as the second line of code in all modules:

Always Use Option Explicit
http://www.access.qbuilt.com/html/gem_tips.html#VBEOptions

In fact, the author of the sample Excel spreadsheet did not do this, and the
code does not compile properly if one inserts these two very important words
into the existing Excel VBA code.

When working with VBA code, it is good to get in the habit of compiling your
code on a fairly frequent basis. This way, you catch any compile-time errors
as you are working, instead of letting them build up. I also recommend that
you remove the default checks in the VBE options for Background compile and
Compile on demand.

Here are a few resources to get you going on using VBA code:

http://www.seattleaccess.org/downloads.htm
Look for this download:
DAO - Back To Basics Compilation/Demo
by Tom Wickerath, Jan/Feb 2007 --
Download (448 kb) App and Word Doc

and

Access Basics
http://www.accessmvp.com/Strive4Peace/Index.htm


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 
E

efandango

Tom,

Appreciate the feedback on this problem, and the comments about not having
to maintain Excel. There were other reasons why I wanted to keep it all
within Access. Witht that in mind, as I said, I persevered (albeit
blindly...), and I actually got it to return a correct geocode on a single
record from my table/query:

I used this procedure:

******************
Private Sub btn_Runs_Geocode_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rs As DAO.Recordset
Dim strRun_No As String
Dim lngFN As Long
Dim strFileName As String

lngFN = FreeFile()

strStartRun_No = InputBox("Enter the lower Run No")
strEndRun_No = InputBox("Enter the higher Run No")

strFileName = "Run" & "_" & "Points_" & strStartRun_No & "-" & strEndRun_No

If Len(strStartRun_No) > 0 Then
Set db = CurrentDb()
Set qdf = db.QueryDefs("Generate_KML_Points_Groups")
qdf.Parameters("StartRun") = strStartRun_No
qdf.Parameters("EndRun") = strEndRun_No

Set rs = qdf.OpenRecordset(dbOpenDynaset)

Do Until rs.EOF = True
Call YahooAddressLookup(Run_point_Address, Run_Point_Postcode)
'Call sSleep(2000)
rs.MoveNext
Loop

rs.Close

End If

Close #lngFN
MsgBox ("Done")

End Sub

******************

That called this function:

*******************
Function YahooAddressLookup(Run_point_Address As String, Run_Point_Postcode
As String) As String


' perform RESTian lookup on Yahoo
Dim response As String

'url =
"http://local.yahooapis.com/MapsService/V1/geocode?appid=efandango&location="
& Run_point_Address & ", " & Run_Point_Postcode & ", London"



'Create Http object
If IsEmpty(http) Then Set http =
CreateObject("WinHttp.WinHttpRequest.5.1")

'Send request To URL
http.Open "GET", url

http.send
'Get response data As a string

response = http.responseText

' capture the latitude by regex matching the values in the tags
<geo:lat> and <geo:long>
lat = RegExMatch(response, "<Latitude>([\.\-0-9]+)</Latitude>")
lng = RegExMatch(response, "<Longitude>([\.\-0-9]+)</Longitude>")
precision = RegExMatch(response, "precision=""([a-z0-9+]+)""")

' return a comma delimited string
' if values not found, this will return ","
YahooAddressLookup = lat & "," & lng & "," & precision
If (precision <> "" And precision <> "state") Then
[address_latitude] = lat
[address_longitude] = lng
Else
[address_latitude] = "not found"
[address_longitude] = "not found"
End If

End Function
*******************

But for some reason I cannot get it to loop and go get the geocode for the
next record, I guess if i'm honest, I am just stumbling around trying to
conquer this problem while also trying to learn VBA, it's frustrating being
in this chicken and egg situation because when I ask for help, i don't know
enough to provide the right kind of question (ref: my original question), and
I somestimes sound like I know what I'm doing, when in fact I don't really,
most succes so far has been more by accident than design.

regards

Eric
 
T

Tom Wickerath

Hi Eric,

Congratulations on the success that you've had so far!

Tell you what....I can go ahead and take a look at your Access application,
if you are willing to send me a compacted and zipped copy by e-mail. I'll
also need the credentials (username / password) to run your code (since I
really don't have the time or energy at this point to establish an account
for myself).

If you are interested, send me a private e-mail message with a valid
reply-to address. My e-mail address is available at the bottom of the
contributor's page indicated below. Please do not post your e-mail address
(or mine) to a newsgroup reply. Doing so will only attract the unwanted
attention of spammers.


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________

efandango said:
Tom,

Appreciate the feedback on this problem, and the comments about not having
to maintain Excel. There were other reasons why I wanted to keep it all
within Access. Witht that in mind, as I said, I persevered (albeit
blindly...), and I actually got it to return a correct geocode on a single
record from my table/query:

<snip>
 
E

efandango

Tom,

That is very nice of you to make the offer, which I will gratefully take you
up on. As regards my database I have completely seperated the geocoding
elements into a new DB for you, So there is simply 1 table, 1 Form, 1 Query.
They are very basic rough n ready forms because I was more concerned with
getting the gecoding engine 'looping working'

I have filled you in with the other details you requested in the email that
I have now sent to you

regards

Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top