Access – Bing Maps Basics

After demonstrating how easy it was to use Google Maps to display a map of an Address or Geolocation coordinates in my article

I wanted to demonstrate that the same was true for mapping with Microsoft Bing Maps.

Microsoft Access - Web Browser Control - Bing Maps

Mapping an Address With Bing Maps in Access!

To display a Bing Maps of an Address, just like with Google Maps, is very straightforward.

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
        'Option 1 - Older Format
        sURL = "https://www.bing.com/maps/?v=2&where1="
        sURL = sURL & sAddr & "&sty=c"
'        'Option 2 - Newer Format
'        sURL = "https://bing.com/maps/default.aspx?where1="
'        sURL = sURL & sAddr & "&Style = r"
        Me.WB_Map.ControlSource = "=""" & sURL & """"
        Me.WB_URL = sURL
        DoEvents
    Else
        Me.WB_Map.ControlSource = "=""about:blank"""
        Me.WB_URL = ""
    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

As you can see in the code above, there are 2 different syntax’s that can be employed, the choice is your! The link below provides you with the official documentation and all the other parameters you can specify for things like the zoom level, the style, traffic, … so be sure you take a look at it if you are wanting other features.

Helper Function

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 occurred." & vbCrLf & vbCrLf & _
            "Error Number: " & Err.Number & vbCrLf & _
            "Error Source: fAppendString" & vbCrLf & _
            "Error Description: " & Err.Description, _
            vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

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:

1600 Pennsylvania Avenue NW,Washington,DC,20500,United States

Then we concatenate it with the base URL resulting in a final URL string like:

http://www.bing.com/maps/?v=2&where1=1300 Constitution Ave. NW,Washington,DC,20560,United States&sty=c

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.

Mapping a Latitude and Longitude With Bing 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.

Public 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.bing.com/maps/?cp="
        sURL = sURL & Me.Latitude & "~" & Me.Longitude
        sURL = sURL & "&sty=c&lvl=16.75"
        Me.WB_Map.ControlSource = "=""" & sURL & """"
        Me.WB_URL = sURL
        DoEvents
    Else
        Me.WB_Map.ControlSource = "=""about:blank"""
        Me.WB_URL = ""
    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

resulting in a URL such as:

https://www.bing.com/maps/?cp=38.8912835~-77.0322396&sty=c&lvl=16.75

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 – Bing Maps – Basic” BingMaps_Basic.zip – Downloaded 6662 times – 33.89 KB

Disclaimer/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

All code samples, download samples, links, ... on this site are provided 'AS IS'.

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.

Resources on the Subject

One response on “Access – Bing Maps Basics

  1. Fady

    Hello Sir.
    Thanks a lot for this helpful information.
    I have one question though.
    What is I would add a Point Label on the map of specific coordinates, is it possible.?