Error when using the Excel Acos function (1 Viewer)

ColeChaW

Charlie
Local time
Yesterday, 21:04
Joined
Aug 10, 2006
Messages
36
I am using the following:

Excel.Application.WorksheetFunction.Acos()

In order to find the ArcCosine of an equation. For some reason, it will work the first few times and then throw Runtime Error 1004, explaining that it:

Unable to get the Acos property of the WorksheetFunction class

Not sure why this would work a few times and then not work, I'm using this in a loop of 5,000 records and it appears to make it just a little of the way through before the error crops up.

What gives? :(

Code:
Private Function FindHeading(Lat1 As Double, Lon1 As Double, Lat2 As Double, Lon2 As Double, D As Double) As Double

    If Sin(Lon2 - Lon1) < 0 Then
        FindHeading = Excel.Application.WorksheetFunction.Acos((Sin(Lat2) - Sin(Lat1) * Cos(D)) / (Sin(D) * Cos(Lat1)))
    Else
        FindHeading = 2 * PI - Excel.Application.WorksheetFunction.Acos((Sin(Lat2) - Sin(Lat1) * Cos(D)) / (Sin(D) * Cos(Lat1)))
    End If
       
End Function

Called from:
Code:
    With rst
    Do Until .EOF
    If !latitudedec < (theLat - 0.05) Then GoTo NextOne
    If !latitudedec > (theLat + 0.05) Then GoTo NextOne
    If !longitudedec > (0 - theLong + 0.05) Then GoTo NextOne
    If !longitudedec < (0 - theLong - 0.05) Then GoTo NextOne
    
    dblDistance = CalculateDistance(theLat, -theLong, !latitudedec, !longitudedec)
    intHeading = FindHeading(theLat, -theLong, !latitudedec, !longitudedec, dblDistance)
    DoCmd.RunSQL ("INSERT INTO TempLocations VALUES('" & !featurename & "', " & Round(dblDistance, 2) & ")")
    strResult = "SELECT TempLocations.name, [distance] & ' miles' AS miles FROM TempLocations ORDER BY TempLocations.distance; "

NextOne:
    ' Update the progress meter.
    RetVal = SysCmd(acSysCmdUpdateMeter, Progress_Amount)
    Progress_Amount = Progress_Amount + 1
    .MoveNext
    Loop
 

Users who are viewing this thread

Top Bottom