A colleague has found the following Excel function online
It asks for an X coordinate, a Y coordinate and an array of X and Y coordinates.
It then loops through the array and where it finds that the passed X and Y coordinates are within the array's X and Y values, it returns TRUE.
This works fine in Excel and my manager has asked me to try to translate it into doing the same thing in Access.
My attempt is as follows.
1. The function is being called correctly.
2. It's passing in X, Y and the name of the table to use to populate the array.
3. The array is being populated correctly (I tested with a msgbox at point 1).
HOWEVER
I get a subscript out of range message on the red line.
I'm not familiar with Arrays at all and trying to work out if the problem is to do with trying to adapt Excel VBA into Access or something else.
1. Can anyone spot the issue?
2. Is there anything else obviously wrong, that I should correct now? I figured using a function that was working as my base would make things easier, but now I'm doubting myself.
Code:
Public Function PtInPoly(Xcoord As Double, Ycoord As Double, Polygon As Variant) As Variant
Dim x As Double, m As Double, b As Double, Poly As Variant
Dim LB1 As Double, LB2 As Double, UB1 As Double, UB2 As Double, NumSidesCrossed As Double
Poly = Polygon
For x = LBound(Poly) To UBound(Poly) - 1
If Poly(x, 1) > Xcoord Xor Poly(x + 1, 1) > Xcoord Then
m = (Poly(x + 1, 2) - Poly(x, 2)) / (Poly(x + 1, 1) - Poly(x, 1))
b = (Poly(x, 2) * Poly(x + 1, 1) - Poly(x, 1) * Poly(x + 1, 2)) / (Poly(x + 1, 1) - Poly(x, 1))
If m * Xcoord + b > Ycoord Then NumSidesCrossed = NumSidesCrossed + 1
End If
Next
PtInPoly = CBool(NumSidesCrossed Mod 2)
End Function
It then loops through the array and where it finds that the passed X and Y coordinates are within the array's X and Y values, it returns TRUE.
This works fine in Excel and my manager has asked me to try to translate it into doing the same thing in Access.
My attempt is as follows.
1. The function is being called correctly.
2. It's passing in X, Y and the name of the table to use to populate the array.
3. The array is being populated correctly (I tested with a msgbox at point 1).
HOWEVER
I get a subscript out of range message on the red line.
I'm not familiar with Arrays at all and trying to work out if the problem is to do with trying to adapt Excel VBA into Access or something else.
1. Can anyone spot the issue?
2. Is there anything else obviously wrong, that I should correct now? I figured using a function that was working as my base would make things easier, but now I'm doubting myself.
Code:
Public Function PtInPoly(ByVal Xcoord As Long, ByVal Ycoord As Long, ByVal DataTable As String, Db As Database) As Variant
Dim x As Long ' long column, col 1
Dim y As Long ' lat column, col 2
Dim m As Long
Dim b As Long
Dim LowerLong As Long
Dim UpperLong As Long
Dim LowerLat As Long
Dim UpperLat As Long
Dim ArrayPoly As Variant
Dim vItem As Variant
Dim NumSidesCrossed As Long
Dim RstPoly As Recordset
Dim liRecCount As Integer
Set RstPoly = Db.OpenRecordset(DataTable)
liRecCount = RstPoly.RecordCount
With RstPoly
ArrayPoly = .GetRows(.RecordCount)
End With
For Each vItem In ArrayPoly
MsgBox vItem ' - 1
Next
For x = LBound(ArrayPoly) To UBound(ArrayPoly) - 1
MsgBox ArrayPoly(x, 1) ' - 2
MsgBox ArrayPoly(x + 1, 1) ' - 3
[COLOR=red][B]If (ArrayPoly(x, 1) > Xcoord) Or (ArrayPoly(x + 1, 1) > Xcoord) Then[/B][/COLOR]
m = (ArrayPoly(x + 1, 2) - ArrayPoly(x, 2)) / (ArrayPoly(x + 1, 1) - ArrayPoly(x, 1))
b = (ArrayPoly(x, 2) * ArrayPoly(x + 1, 1) - ArrayPoly(x, 1) * ArrayPoly(x + 1, 2)) / (ArrayPoly(x + 1, 1) - ArrayPoly(x, 1))
If m * Xcoord + b > Ycoord Then
NumSidesCrossed = NumSidesCrossed + 1
End If
End If
Next
PtInPoly = CBool(NumSidesCrossed Mod 2)
End Function