This is the first in a series of articles on Mapping within Access using the Web Browser control. Today, I thought I’d demonstrate how easy it is to provide a map of an address to your users using Google Maps.
Mapping an Address With Google Maps in Access!
To display a Google Maps of an Address is very straightforward.
Helper Functions
To simplify my work, I created a helper function for building the necessary string for the URL:
Function fAppendString(sString As String, sNewString As Variant, Optional sDelimiter As String = ",") As String
On Error GoTo Error_Handler
Dim sAppendString As String
If IsNull(sNewString) = False Then
If sString <> vbNullString Then
sAppendString = sString & sDelimiter & sNewString
Else
sAppendString = sNewString
End If
Else
sAppendString = sString
End If
fAppendString = sAppendString
Error_Handler_Exit:
On Error Resume Next
Exit Function
Error_Handler:
MsgBox "The following error has occured." & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: fAppendString" & vbCrLf & _
"Error Description: " & Err.Description, _
vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Function
The Main Code
To make this work, I simply use the form’s On Current event to read the address and build the necessary URL for the web browser to display, resulting in something like:
Private Sub Form_Current()
On Error GoTo Error_Handler
Dim sAddr As String
Dim sURL As String
sAddr = fAppendString(sAddr, Me.Nbr, "")
sAddr = fAppendString(sAddr, Me.Street, " ")
sAddr = fAppendString(sAddr, Me.City)
sAddr = fAppendString(sAddr, Me.Region)
sAddr = fAppendString(sAddr, Replace(Nz(Me.PostalCode, ""), " ", ""))
sAddr = fAppendString(sAddr, Me.Country)
If sAddr <> vbNullString Then
sAddr = Replace(sAddr, " ", "+")
sURL = "https://maps.google.ca/maps?q="
sURL = sURL & sAddr & "&hl=en"
Me.WB_Map.ControlSource = "=""" & sURL & """"
DoEvents
Else
Me.WB_Map.ControlSource = "=""about:blank"""
End If
Error_Handler_Exit:
On Error Resume Next
Exit Sub
Error_Handler:
MsgBox "The following error has occurred." & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: Form_Current" & vbCrLf & _
"Error Description: " & Err.Description, _
vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Sub
Note, in the above, my Web Browser control is named ‘WB_Map’. So be sure to replace it with the name of your control.
and that’s it!
How Does It Work Exactly?
Initially, it uses the fAppendString function to build a string of each address component on the main form:
- Nbr => Street Number
- Street => Street Name
- City
- Region => Province/State/…
- PostalCode => Postal Code/Zip Code/…
- Country
and creates a string like:
1300 Constitution Ave. NW,Washington,DC,20560,United States
Then we replace any spaces with a ‘+’ character and concatenate it all together resulting in a final URL string like:
https://maps.google.ca/maps?q=1300+Constitution+Ave.+NW,Washington,DC,20560,United+States&hl=en
and we pass that to the web browser control to display.
Since we are using the On Current event, whenever the user changes records, the map automatically updates itself. We could also use update event(s) in case records are changed… but I’ll let you customize things further to suit your specific need.
Retrieving Geolocation Coordinates From An Address
Once the map is render, Google generates a new URL from which we can actually parse out the Latitude and Longitude if we would like to. Below is some basic code to accomplish that.
Private Sub cmd_GetLatLong_Click()
Dim sGoogleUrl As String
Dim sCoord As String
Dim sLat As String
Dim sLong As String
'Ensure the web browser is done processing the page and Google returns the fully processed URL
Do While InStr(Me.WB_Map.LocationURL, "@") = 0
DoEvents
Loop
'Google's URL
sGoogleUrl = Me.WB_Map.LocationURL
' Debug.Print sGoogleUrl
'Parse the Latitude
sCoord = Mid(sGoogleUrl, InStr(sGoogleUrl, "@") + 1)
sLat = Mid(sCoord, 1, InStr(sCoord, ",") - 1)
'Parse the Longitude
sCoord = Right(sCoord, Len(sCoord) - (Len(sLat) + 1))
sLong = Mid(sCoord, 1, InStr(sCoord, ",") - 1)
'Push the values to the form/controls
Me.Latitude = sLat
Me.Longitude = sLong
End Sub
Mapping a Latitude and Longitude With Google Maps in Access!
Mapping using Longitude and Latitude is even simpler as we don’t need to build up an address string and escape any characters. It can all be done with a single sub routine.
Private Sub Form_Current()
On Error GoTo Error_Handler
Dim sURL As String
If IsNull(Me.Latitude) = False And IsNull(Me.Longitude) = False Then
sURL = "https://www.google.com/maps/@"
sURL = sURL & Me.Latitude & "," & Me.Longitude
sURL = sURL & "," & Me.MapZoomLevel & "z"
Me.WB_Map.ControlSource = "=""" & sURL & """"
DoEvents
Else
Me.WB_Map.ControlSource = "=""about:blank"""
End If
Error_Handler_Exit:
On Error Resume Next
Exit Sub
Error_Handler:
MsgBox "The following error has occured." & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: Form_Current" & vbCrLf & _
"Error Description: " & Err.Description, _
vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Sub
resulting in a URL such as:
https://www.google.com/maps/@38.8912835,-77.0322396,15z
that we then pass to the web browser control.
That’s it!
Download The Demo File
Feel free to download a 100% unlocked copy by using the link provided below:
Download “Access - Google Maps - Basic Demo” GoogleMaps_Basic.zip – Downloaded 7984 times – 29.97 KBDisclaimer/Notes:
If you do not have Microsoft Access, simply download and install the freely available runtime version (this permits running MS Access databases, but not modifying their design):Microsoft Access 2010 Runtime
Microsoft Access 2013 Runtime
Microsoft Access 2016 Runtime
Microsoft 365 Access Runtime
In no event will Devhut.net or CARDA Consultants Inc. be liable to the client/end-user or any third party for any damages, including any lost profits, lost savings or other incidental, consequential or special damages arising out of the operation of or inability to operate the software which CARDA Consultants Inc. has provided, even if CARDA Consultants Inc. has been advised of the possibility of such damages.


Useful information as ever Daniel.
For other readers Google has a developer site that outlines the kind of parameters that can be passed to the maps url..
https://developers.google.com/maps/documentation/urls/get-startedFor UK residents and here I m showing my age but myself and a colleague did something similar a while back. Because we were in the UK the challenging part was converting our UK centric projection to Longitude and Latitude
Anyway might be useful but here is a set of functions that can be used by UK developers to convert eastings and northings into long and lat..
https://cloudydatablog.net/?p=3321how did you avoid the internet explorer compatibility popup appearing in the browser control
I have tried the demo and it is fine, but when I try it with my DB it ops up a window in the browser control each time
Too bad we can’t get something like this for Google Earth Pro!
It renders in a web browser, so no reason why you can’t (probably needs the Modern Web Browser control however). There’s even an API for automating it (https://developers.google.com/earth-engine/reference/rest) so you truly can do almost anything.
Having problems with the “error in the script on this page” issue. (Syntax error line 1737 char 61 – also Object doesn’t support property or method ‘sc_initOneBar’ are a coup-le). It draws the map then jumps to Google support and tells me to download the latest version of a browser. I have the latest of Chrome, Edge and Firefox. I have done the registry hack. Does it not like Australians? Do i need a dll?
thanks.
I’d start by navigating to https://www.whatsmybrowser.org/ and confirming that the reg hack is working properly.
That said, over the years, google has made its code not compatible with older browsers. So it is now possible it doesn’t run on the web browser control (I haven’t confirmed) and perhaps you’re only choice is to use the newer Modern Web Browser control only available in MS365.
Thanks for this Daniel.
I am using Win10 and MS365. It has a Web Browser Control and an Edge Browser Control. The second appears to do nothing. When I navigate to the site above, what do I look for to know the hack is working properly?
Many thanks.
Graeme.
The Edge Browser for displaying a map, should be the way to go.
When you navigate to the site, it should list information regarding the browser and you should see that it is running Edge and not Internet Explorer. That would confirm that the registry hack is working properly for the old web browser control.
Thanks Daniel. I will persevere.
Thanks Daniel.
Sorry to be so thick about this. I tried the Edge Browser Control – no errors but nothing visible either. Unless I am missing something in the setup. I selected Edge Browser Control in the Form Design section and drew a control on the form, and put your code in the OnCurrent event (the other function is in a module). The form is based on a query on a table that contains addresses.
I could format up the URL and have a command button to take them to Google Maps with that URL (that bit works) but i thought it would be rather cool to have it all in Access.
Many thanks.
Graeme.
I put together a very simple demo, not sure if it is what you are after, but take a quick look at it hopefully it will help.
https://1drv.ms/u/s!AjYnefG2siYSkTJnhkM8qGBnmO_6?e=ailOoX
Daniel, I do so appreciate all the time you have given to this. You are a legend!!
Graeme.
I take it you were able to get things functional. Glad I could assist.