Welcome to MapForums!

Register, sign in, or use Facebook Connect above to join in and participate in the forum.

When you are signed in, this message, the ads in this row, and the red-underlined link ads all go away.

Subscribe to receive our newsletter.
Subscribe Unsubscribe
Results 1 to 3 of 3

How to handle route calculation error

This is a discussion on How to handle route calculation error within the MapPoint Desktop Discussion forums, part of the Map Forums category; Hi all. I am trying to write a simple VB script that is executed from Access to calculate the driving ...

  1. #1
    rberick is offline Junior Member White Belt
    Join Date
    Nov 2003
    Posts
    2

    How to handle route calculation error

    Hi all.

    I am trying to write a simple VB script that is executed from Access to calculate the driving distance from one zip code to another and write the distance back to a table. I have the base script (see below) working, but I am running into a problem that when a good route can not be calculate by MapPoint the script bombs.

    I am looking for a away to write a default number (i.e. 999999) as the distance when a route can not be calculated so that the script does not bomb each time.

    Any ideas (and examples) would greatly be appreciated.

    Thanks

    Rolf


    MapPoint Error:

    "Unable to get directions from 71, East Lyme, CT 06333 to 330th St, White Hall, AR 71602. One of the areas may not contain necessary connectors such as ferry routes or main roads. Move one or both of the stops, and try again."

    Script:

    Private Sub Command0_Click()

    Dim objApp As New MapPoint.Application
    Dim objMap As MapPoint.Map
    Dim objRoute As MapPoint.Route
    Dim strfromcity As String
    Dim strfromstate As String
    Dim strtocity As String
    Dim strtoState As String
    Dim strfromlocation As String
    Dim strtolocation As String
    Dim strfromzip As String
    Dim strtozip As String
    Dim fromLong As Double
    Dim fromLat As Double
    Dim toLong As Double
    Dim toLat As Double
    Dim CalcTime As Date


    Dim dbs As Database
    Dim rstmilage As DAO.Recordset
    Dim n As Double

    'Set up the application
    Set objMap = objApp.ActiveMap
    Set objRoute = objMap.ActiveRoute
    objApp.Visible = False
    objApp.UserControl = True

    'Open Route Stops Table and interates through Table
    Set dbs = CurrentDb
    Set rstmilage = dbs.OpenRecordset("tblMilage", dbOpenTable)

    With rstmilage
    .MoveFirst
    Do While Not rstmilage.EOF

    strfromzip = Nz(![fromzip])
    strtozip = Nz(![tozip])

    'Add route stops and calculate the route
    objRoute.Waypoints.Add objMap.FindResults(strfromzip).Item(1)
    objRoute.Waypoints.Add objMap.FindResults(strtozip).Item(1)

    CalcTime = Now()

    If objRoute.Waypoints.Count > 1 Then

    objRoute.Calculate

    m = objRoute.Directions.Count

    rstmilage.Edit
    rstmilage("MPZipDist") = objRoute.Directions.Item(m).ElapsedDistance
    rstmilage("Create_Time") = CalcTime


    objRoute.Clear

    rstmilage.Update
    rstmilage.MoveNext
    objRoute.Clear

    Else
    rstmilage.Edit
    rstmilage("MPZipDist") = "999999"
    rstmilage("Create_Time") = CalcTime

    objRoute.Clear

    rstmilage.Update
    rstmilage.MoveNext
    objRoute.Clear

    End If

    Loop
    rstmilage.Close
    End With



    objApp.Quit

    MsgBox "All Done"


    End Sub

  2. #2
    Martel is offline Junior Member Yellow Belt
    Join Date
    Feb 2003
    Posts
    28
    This should give you some ideas:
    (additions commented and marked with asterisks)


    Code:
    Private Sub Command0_Click()
    .
    .declarations were here
    .
    '**** add error handler ****
    On Error GoTo ProcessError:
    '************************
    
    'Set up the application
    Set objMap = objApp.ActiveMap
    .
    .set up stuff here
    .
    
    
    With rstmilage
    .MoveFirst
    Do While Not rstmilage.EOF
    
    strfromzip = Nz(![fromzip])
    strtozip = Nz(![tozip])
    
    .
    . route stuff
    .
    
    rstmilage.Edit
    rstmilage("MPZipDist") = objRoute.Directions.Item(m).ElapsedDistance
    rstmilage("Create_Time") = CalcTime
    
    '**** add label so error handler knows where to return ****
    SKIP_TO_NEXT:
    '*************************************************
    
    objRoute.Clear
    rstmilage.Update
    rstmilage.MoveNext
    objRoute.Clear
    
    Else
    .
    .  else stuff here
    .
    End If
    
    Loop
    rstmilage.Close
    End With
    
    
    objApp.Quit
    
    MsgBox "All Done"
    
    '*********************************
    '***  Add subroutine exit so sub does not 
    '***  fall into error code
    '*********************************
    Exit_:
      Exit Sub
    
    
    '********************************
    '*** code that handles the error
    '********************************
    ProcessError:
      'if the error number is equal to that generated when
      'connectors not available 
       If Err.Number = "-2147221503" Then
          'mark recordset for editing
          rstmilage.Edit
          'set values
          rstmilage("MPZipDist") = "999999"
          rstmilage("Create_Time") = CalcTime
          'go to label
          Resume SKIP_TO_NEXT
        Else
          'otherwise show error and exit
          MsgBox "ERROR: " & Err.Number & vbCrLf _
                & Err.Description & vbCrLf _
                & "Module: Module1" & vbCrLf _
                & "Subroutine: TestRoute" & vbCrLf & vbCrLf _
                & "Exiting Now"
          GoTo Exit_
        End If
    End Sub

  3. #3
    rberick is offline Junior Member White Belt
    Join Date
    Nov 2003
    Posts
    2
    Thank you for your help. Code worked like a champ!

    Rolf

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Automate large list of drive time & distance calculation
    By onegalacticwino in forum MapPoint Desktop Discussion
    Replies: 1
    Last Post: 12-13-2004, 02:14 PM
  2. Distance Calculation
    By jburgess in forum MapPoint Desktop Discussion
    Replies: 2
    Last Post: 11-06-2004, 09:33 PM
  3. Replies: 6
    Last Post: 08-19-2004, 07:10 PM
  4. hiding directions after route calculation in a MP controll
    By ruyasan in forum MapPoint Desktop Discussion
    Replies: 4
    Last Post: 05-19-2004, 06:19 PM
  5. Is it possible to get a direct HANDLE to the Map? ....
    By Anonymous in forum MapPoint Desktop Discussion
    Replies: 2
    Last Post: 06-19-2002, 10:05 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96