stop functions on condition and errors (1 Viewer)

matt beamish

Registered User.
Local time
Today, 11:41
Joined
Sep 21, 2000
Messages
208
Hi people - I am really a clumsy novice with this, especially in dealing with errors, so forgive my ignorance.

I have a form which calls some VBA functions I have found and put together in an Access module, that pass a location textstring to the Google API service and return latitude and longitude results into a control named "latlngcntrl", and then other functions that split "latlngcntrl" into separate lat and lng values ("PHI" and "LAM" ) and then process those results into NGR coordinates for the UK.

The functions I have used were written for Excel vb.
The function that returns the latlong from Google is based on this which I placed in an Access module:
http://policeanalyst.com/using-the-google-geocoding-api-in-excel/http://

The functions that process the split Lat and lng results I found in an Ordnance Survey XLS spreadsheet http://www.ordnancesurvey.co.uk/docs/support/projection-and-transformation-calculations.xls

There are 8 or so constants (Pi, A, B, e0, f0, n0, Phi0, LAM0) used in the Projection calculations and I have stated these globally in a separate module and I am using functions to call the constants into the calculations.

If my textstring is not recognised by Google, my return value for "latlngcntrl" is "Not Found***". However the other functions that use this value continue to try to process it, and so I get errors that I need to learn how to control, and anyone trying to use the form will see several error messages - and I need to avoid this.

I want the other functions to stop running if Google API does not return a latitude/longitude to my control "latlongcntrl" and for a User not to get any error messages other than "Location not recognised".

In the projections code there are several functions that are interdependent.
Here is one of the functions that I have adapted from the Excel VB module, which includes some debug.print statements I included when I was adapting the code.:
Code:
Function Lat_Long_to_East(PHI, LAM, A, B, e0, f0, Phi0, LAM0)

'Project Latitude and longitude to Transverse Mercator eastings.
'Input: - _
 Latitude (PHI) and Longitude (LAM) in decimal degrees'; _
 ellipsoid axis dimensions (a & b) in meters'; _
 eastings of false origin (e0) in meters'; _
 central meridian scale factor (f0)'; _
 latitude (PHI0) and longitude (LAM0) of false origin in decimal degrees.

Dim RADPHI As Single
Dim RADLAM As Single
Dim RADPHI0 As Single
Dim RADLAM0 As Single
Dim af0 As Single
Dim bf0 As Single
Dim e2 As Single
Dim IV As Single
Dim V As Single
Dim VI As Single
Dim n As Single
Dim nu As Single
Dim rho As Single
Dim eta2 As Single
Dim P As Single

   
'Convert angle measures to radians
       
    RADPHI = [PHI] * (Pi / 180)
    RADLAM = [LAM] * (Pi / 180)
    RADPHI0 = [Phi0] * (Pi / 180)
    RADLAM0 = [LAM0] * (Pi / 180)

    af0 = A * f0
    bf0 = B * f0
    e2 = ((af0 ^ 2) - (bf0 ^ 2)) / (af0 ^ 2)
    n = (af0 - bf0) / (af0 + bf0)
    nu = af0 / (Sqr(1 - (e2 * ((Sin(RADPHI)) ^ 2))))
    rho = (nu * (1 - e2)) / (1 - (e2 * (Sin(RADPHI)) ^ 2))
    eta2 = (nu / rho) - 1
    P = RADLAM - RADLAM0
    Debug.Print "P="; P
    Debug.Print "LAM ="; LAM
    Debug.Print "Pi ="; Pi
    Debug.Print "A ="; A
    
    IV = nu * (Cos(RADPHI))
    Debug.Print "IV="; IV
    V = (nu / 6) * ((Cos(RADPHI)) ^ 3) * ((nu / rho) - ((Tan(RADPHI) ^ 2)))
    Debug.Print "V="; V
    VI = (nu / 120) * ((Cos(RADPHI)) ^ 5) * (5 - (18 * ((Tan(RADPHI)) ^ 2)) + ((Tan(RADPHI)) ^ 4) + (14 * eta2) - (58 * ((Tan(RADPHI)) ^ 2) * eta2))
    Debug.Print "VI="; VI
    
    Lat_Long_to_East = e0 + (P * IV) + ((P ^ 3) * V) + ((P ^ 5) * VI)

End Function

What should my conditional statement be to stop the function "unction Lat_Long_to_East" from running and reporting an error, if it could not find one of the two variables "PHI" and "LAM" that it needs?
thanks in advance
 

RainLover

VIP From a land downunder
Local time
Today, 20:41
Joined
Jan 5, 2009
Messages
5,041
Why not test the Values of variables "PHI" and "LAM" before they are required to be used.

What value do you put on Pi.

I may not be able to solve this but I would like to try if you can send the information that I need.
 

RainLover

VIP From a land downunder
Local time
Today, 20:41
Joined
Jan 5, 2009
Messages
5,041
RADPHI = [PHI] * (Pi / 180)
RADLAM = [LAM] * (Pi / 180)
RADPHI0 = [Phi0] * (Pi / 180)
RADLAM0 = [LAM0] * (Pi / 180)

Do all of these evaluate to "Single" Have you tested as I haven't.
 

matt beamish

Registered User.
Local time
Today, 11:41
Joined
Sep 21, 2000
Messages
208
Thanks for your reply.

Here are my constants as declared in a module:

Code:
Option Compare Database
Public Const Pi As Double = 3.14159265358979
Public Const A As Single = 6377563.396
Public Const B As Single = 6356256.91
Public Const e0 As Single = 400000
Public Const f0 As Single = 0.9996012717
Public Const n0 As Single = -100000
Public Const Phi0 As Single = 49
Public Const LAM0 As Single = -2
Function GetPi()
GetPi = Pi
End Function
Function GetA()
GetA = A
End Function
Function GetB()
GetB = B
End Function
Function Gete0()
Gete0 = e0
End Function
Function Getf0()
Getf0 = f0
End Function
Function Getn0()
Getn0 = n0
End Function
Function GetPHI0()
GetPHI0 = Phi0
End Function
Function GetLAM0()
GetLAM0 = LAM0
End Function

Should I test PHI and LAM singly or together?

thanks again
 

matt beamish

Registered User.
Local time
Today, 11:41
Joined
Sep 21, 2000
Messages
208
..and no, I haven't tested the variables as singles.....I copied the definitions from the Excel VBA where they were defined. How to do I test?
thanks
 

RainLover

VIP From a land downunder
Local time
Today, 20:41
Joined
Jan 5, 2009
Messages
5,041
All of your Data Types have a certain value range.

Like Text is 255 characters.

If you look at the bottom left (Access 2003) it will tell you what these are.

If you were to multiply a Long by a Long the answer cannot be a Single. One would think. This is because the size is too big. This is not true for everything so you may need to test.

In early computer days space was important. Hence 91, 92, 93, 94 etc not 1991, 1992, 1993 and finally 1994.

Big Scam as they talked about the Y2k compliance. Same as global Warming. This 2 digit date was purely to save space as that was important. It is not important any mre so we can use larger Data Types. I would have all your Singles as Doubles just to give me some more space to move.

Matt, I am telling you all this stuff in the hope that it may be of benefit to you. I still do not know how to solve your problem. I am just hoping I can give you enough Info that you can solve it.

Back to your second question. Testing both together or separately does not matter.

Code:
 Function Getn0()
    Getn0 = n0
End Function

 Function GetPHI0()
    GetPHI0 = Phi0
End Function
 
Function GetLAM0()
    GetLAM0 = LAM0
End Function

If you can lay our your code like this it would be easier to follow.
 

RainLover

VIP From a land downunder
Local time
Today, 20:41
Joined
Jan 5, 2009
Messages
5,041
What should my conditional statement be to stop the function "unction Lat_Long_to_East" from running and reporting an error, if it could not find one of the two variables "PHI" and "LAM" that it needs?
thanks in advance

Could the answer simply be.

Exit Function.

This should stop everything. What else would you need?
 

Users who are viewing this thread

Top Bottom