MapPoint Forums

MapForums

Community of VE/MapPoint Users and Developers




On Error Help - VBA

This is a discussion on On Error Help - VBA within the Development forums, part of the MapPoint 2006/2009 Discussion category; ok, so I'm writing VBA code for an excel spreadsheet and Mappoint 2004, that returns the road distance between two ...


Go Back   MapPoint Forums > Map Forums > MapPoint 2006/2009 Discussion > Development

Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
  5 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 10-03-2007
Junior Member
Yellow Belt
 
Join Date: Mar 2007
Posts: 14
On Error Help - VBA

ok, so I'm writing VBA code for an excel spreadsheet and Mappoint 2004, that returns the road distance between two locations, using their Lat and Long...

The trouble is when I get to an isolated Postal Code (or Zip Code) that doesn't have road access connecting the mainland I get some error (can't even identify it) and Mappoint shuts down, the excel macro suts down, leaving me "up the creek"...

I've attached the code, and appreciate any help or hints... (My next step will be to identify manually which Zip Codes are isolated, and then have a seperate "straightline distance calculator" return a distance for the isolated location...

Here's the code:
>>>>>>>>>>>>>>>>>>>

Public Function RoadDistance(Lat1, Long1, Lat2, Long2)

Dim SysApp As New MapPoint.Application
Dim SysMap As MapPoint.Map
Dim SysRoute As MapPoint.Route
Dim SysLoc1 As MapPoint.Location
Dim SysLoc2 As MapPoint.Location


Application.ActivateMicrosoftApp xlMicrosoftMappointNorthAmerica

Application.DisplayAlerts = False

'SysApp.OpenMap (AKPath & "\LHIN.ptm")
Set SysMap = SysApp.ActiveMap
Set SysRoute = SysMap.ActiveRoute

SysApp.Units = 1

Set SysLoc1 = SysMap.GetLocation(Lat1, Long1)
Set SysLoc2 = SysMap.GetLocation(Lat2, Long2)

SysRoute.Waypoints.Add SysLoc1
SysRoute.Waypoints.Add SysLoc2

SysRoute.Calculate
On Error GoTo IsolatedPC

'Sheets("Maps").Select
RoadDistance = SysRoute.Distance
SysRoute.Clear
SysMap.Saved = True
GoTo Ending

IsolatedPC:
SysRoute.Clear
SysMap.Saved = True
RoadDistance = Posdist(Lat1, Long1, Lat2, Long2)
Ending:
End Function

>>>>>>>>>>>

any thoughts?
cheers,
andriy

Last edited by andriy; 10-04-2007 at 11:25 AM. Reason: remove unneccesary personal comments
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #2 (permalink)  
Old 10-03-2007
Senior Member
Black Belt
 
Join Date: Nov 2004
Posts: 2,055
Re: On Error Help - VBA

Hi,

the line of code where you open the map is put in comment.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3 (permalink)  
Old 10-03-2007
Junior Member
Yellow Belt
 
Join Date: Mar 2007
Posts: 14
Re: On Error Help - VBA

That's because I don't want to open a map, just the application, so that it calculates a distance for me... I should have removed that...

(Please, correct me if I'm wrong)

It runs fine, when you have road accessible zip codes, but trying to measure the road distance from Hawai to LA, returns an error... an obvious one, at that, but how to put it into VBA Mappoint language?
Are there error numbers in mappoint, just like in excel?

thanks,
andriy
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4 (permalink)  
Old 10-04-2007
Senior Member
Black Belt
 
Join Date: Nov 2004
Posts: 2,055
Re: On Error Help - VBA

Hi,

If you uncomment the line then same problem ?

To debug you can step with the debugger trough the code to find the offending line. Also you can put the whole thing in an exception block, then probably more error information.

How to do both I don't know, I don't know VBA, but sure others can jump in here.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5 (permalink)  
Old 10-04-2007
Winwaed's Avatar
Mapping-Tools.com
Red Belt
 
Join Date: Feb 2004
Posts: 787
Blog Entries: 5
Re: On Error Help - VBA

Yep MapPoint throws an exception if it cannot find a route - eg. locations on different islands, poor farm roads, etc.

You need to trap the exception and "do something appropriate" (eg. ignore it, write a " " for the distance, or whatever is appropriate for you)

Use "ON ERROR" to trap exceptions in VB6.


Richard
__________________
Winwaed Software Technology LLC
http://www.winwaed.com
See http://www.mapping-tools.com for MapPoint Tools
Pre-Order MapPoint 2009 today: http://www.mapping-tools.com/mappoint2009
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6 (permalink)  
Old 10-04-2007
Junior Member
Yellow Belt
 
Join Date: Mar 2007
Posts: 14
Re: On Error Help - VBA

For those that want to test this out on your computers:

open up excel, pop in the following into the Excel's VB Editor...

>>>>>>>>>>>>>>>>>>>

Public Function RoadDistance(Lat1, Long1, Lat2, Long2)

Dim SysApp As New MapPoint.Application
Dim SysMap As MapPoint.Map
Dim SysRoute As MapPoint.Route
Dim SysLoc1 As MapPoint.Location
Dim SysLoc2 As MapPoint.Location

Application.ActivateMicrosoftApp xlMicrosoftMappointNorthAmerica

Application.DisplayAlerts = False

Set SysMap = SysApp.ActiveMap
Set SysRoute = SysMap.ActiveRoute
SysApp.Units = 1

Set SysLoc1 = SysMap.GetLocation(Lat1, Long1)
Set SysLoc2 = SysMap.GetLocation(Lat2, Long2)

SysRoute.Waypoints.Add SysLoc1
SysRoute.Waypoints.Add SysLoc2

SysRoute.Calculate
On Error GoTo IsolatedPC

'Sheets("Maps").Select
RoadDistance = SysRoute.Distance
SysRoute.Clear
SysMap.Saved = True
GoTo Ending

IsolatedPC:
SysRoute.Clear
SysMap.Saved = True
RoadDistance = Posdist(Lat1, Long1, Lat2, Long2)
Ending:
End Function

>>>>>>>>>>>>>>>>>>>

make sure to check your references in the VB Editor, and click on Mappoint Library...

now on a sheet in A1, enter 53.62
A2 = -88.68
B1 = 46.53
B2 = -84.36

C1 = RoadDistance(A1, A2, B1, B2)
and see what happens...

hope this helps...

andriy
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7 (permalink)  
Old 10-05-2007
Senior Member
Black Belt
 
Join Date: Nov 2004
Posts: 2,055
Re: On Error Help - VBA

Hi,

Can you also tell what happens ? (not everyone has Excel or know how to use the VB thing)

Did you try the exception block as Richard proposed ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8 (permalink)  
Old 10-09-2007
Junior Member
Yellow Belt
 
Join Date: Mar 2007
Posts: 14
Re: On Error Help - VBA

so I made this minor correction, and have the On error statement before the questionable piece of code... yay the program runs...

so for those following the code on your own computers, it should read:

...
SysRoute.Waypoints.Add SysLoc2

On Error GoTo IsolatedPC
SysRoute.Calculate

RoadDistance = SysRoute.Distance
SysRoute.Clear
...

thanks for your help

I still think Mappoint shoud do this automatically, or have a flight option built in, where you take an alternative means of transport, for as short of time as possible... then travel by roads...

regards,
andriy
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.mapforums.com/error-help-vba-6497.html

Posted By For Type Date
Converting Strings Into Decimal Degree Format - MapPoint Articles - MP2K Magazine This thread Refback 10-10-2007 07:38 AM
pushpin and route calculator This thread Trackback 10-09-2007 11:07 AM
Reverse Geocoding with MapPoint 2002 - MapPoint Articles - MP2K Magazine This thread Refback 10-09-2007 08:49 AM
Reverse Geocoding, Another Method - MapPoint Articles - MP2K Magazine This thread Refback 10-08-2007 10:11 AM
The Magazine for MapPoint - MP2K Magazine This thread Refback 10-07-2007 05:34 AM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
Run-time error 2147467259 80004005 automation error ThomasB MapPoint 2006/2009 Discussion 2 02-27-2007 11:41 AM
Error using MP 2006 PC7395 MapPoint 2006/2009 Discussion 7 08-28-2006 04:43 PM
About ERROR 4-40028-1. Error or Bug ? Anonymous MapPoint 2006/2009 Discussion 12 01-11-2005 09:32 AM
I get this following error ananthdeena MapPoint 2006/2009 Discussion 3 01-01-2005 02:46 AM


All times are GMT -5. The time now is 09:16 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
MP2K Magazine
Visitor Map

Cheap Flight to Tenerife
Get a cheap flight to Tenerife and a cheap hotel in Tenerife giving you a great value holiday in Tenerife. Holiday Hypermarket can make it happen.

City Break Europe
Take a city break in Europe. Travel Counsellors can help plan and book your European city break.

Jamaica Holiday
Fancy a Jamaica Holiday? Then visit dealchecker.co.uk and find out what the big deal is. Book a bargain when you book online.

Holidays in Dubai
Holidays in Dubai are an eclectic mix of the ancient and the modern. Discover an oasis of luxury amid the Arabian desert. Book here now!

Cheap Morocco Holidays
Cheap Morocco holidays may be the answer to your cheap holiday search. With sunshine throughout most of the year it can be great value if you avoid the peak season. Why not include a trip to the small tranquil town of Chefchaouen Tangier in your visit?

Holidays Package
Make use of our service at Travel.co.uk to compare holidays. We can find you a package deal that will delight you.

Portugal Holidays
We specialise in Portugal holidays. Visit our On The Beach website for more information.


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