I've never posted a question on any site before, but after racking my head over this hurdle for days, I've caved. I'm working on a program that creates a kml file and exports it to Google Earth. In that file, I want to create a 5 mile radius circle around the placemark. To do that, I worked with the kml circlegen source I found online in php. It calculates the latitudes and longitudes of the points that make up the circle. The calculation works correrctly (as shown by printing to the immediate window or by calling the sub), but I can't get the function to return the array. As many different ways I've structured it, I keep getting a 'type mismatch'. Any help would be GREATLY appreciated! I believe I've included all the necessary function code: - Option Explicit
-
-
Private Const Radius5mls As Double = 8046.72 'in meters
-
Private Const PI As Double = 3.14159265358979
-
-
-
Public Sub FiveMileRadius(lat1 As Double, lon1 As Double)
-
Dim Arr As Variant
-
Dim i As Integer
-
Arr = FiveMileCircleGen(lat1, lon1)
-
If IsArrayAllocated(Arr:=Arr) = True Then
-
For i = LBound(Arr) To UBound(Arr)
-
Debug.Print Arr(i)
-
Next i
-
Else
-
MsgBox ("Not Allocated")
-
End If
-
-
End Sub
- Public Function FiveMileCircleGen(lat1 As Double, lon1 As Double) As Variant()
-
Dim ResultArray() As Variant
-
Dim i As Integer
-
Dim radial As Double, angle As Double
-
Dim iNumSides As Integer
-
Dim lat2 As Double
-
Dim lon2 As Double
-
Dim dlon As Double
-
Dim modNum As Double
-
Dim modDiv As Double
-
Dim distance As Double
-
-
'convert coordinates to radians
-
lat1 = Deg2Rad(lat1)
-
lon1 = Deg2Rad(lon1)
-
-
iNumSides = 360
-
-
distance = Radius5mls / 6378137 'divided by radius of earth at the equator, in meters
-
-
' Create an array to hold all of the points:
-
ReDim ResultArray(i To (iNumSides / 2))
-
-
' trigonometry to calculate circles lat/lon points:
-
For i = 0 To iNumSides Step 2
-
angle = i
-
radial = Deg2Rad(angle)
-
lat2 = fnArcSineRad(sIn(lat1) * Cos(distance) + Cos(lat1) * sIn(distance) * Cos(radial))
-
dlon = ATan2(sIn(radial) * sIn(distance) * Cos(lat1), Cos(distance) - (sIn(lat1) * sIn(lat2)))
-
modNum = (lon1 + dlon + PI)
-
modDiv = 2 * PI
-
lon2 = (modNum - modDiv * Int(modNum / modDiv)) - PI
-
-
lat2 = CStr(Rad2Deg(lat2))
-
lon2 = CStr(Rad2Deg(lon2))
-
-
ResultArray(i) = lon2 & "," & lat2 & ",0 "
-
'Debug.Print lon2 & "," & lat2 & ",0 "
-
-
Next i
-
-
FiveMileCircleGen = ResultArray ' remove this line and the resultarray(i) line and open the debug line to work
-
-
End Function
- Public Function IsArrayAllocated(Arr As Variant) As Boolean
-
On Error Resume Next
-
IsArrayAllocated = Not (IsError(LBound(Arr))) And IsArray(Arr) And (LBound(Arr) <= UBound(Arr))
-
-
End Function
-
-
-
-
' Name: ARC SINE IN RADIANS
-
' Purpose: This function determines the ArcSine of the passed argument.
-
' Inputs: vntSine The sine of an angle.
-
' Assumptions: None.
-
' Effects: Will raise "Overflow" error if vntSine is not a valid sine or
-
' if any other error occurs during execution.
-
' Returns: The angle in radians.
-
' If an error occurs fnArcSine is indeterminate.
-
'
-
Public Function fnArcSineRad(vntSine As Variant) As Double
-
On Error GoTo ERROR_ArcSine ' Trap strangeness.
-
-
Const cOVERFLOW = 6 ' "Overflow" message #.
-
-
Dim blnEditPassed As Boolean ' Edit results variable.
-
Dim dblTemp As Double ' Temporary double.
-
-
blnEditPassed = False ' It hasn't passed yet!
-
If IsNumeric(vntSine) Then ' Is arg numeric?
-
If vntSine >= -1 And vntSine <= 1 Then ' Yup, is it within limits?
-
blnEditPassed = True ' Yup, employ Derived Math Function:
-
' Arcsin(X) = Atn(X / Sqr(-X * X + 1))
-
dblTemp = Sqr(-vntSine * vntSine + 1) ' Calculate denominator.
-
If dblTemp = 0 Then ' Is it 0? Can only happen if arg is +1 or -1.
-
fnArcSineRad = Sgn(vntSine) * PI / 2 ' Yup, assign +Pi/2 or -Pi/2 based on sign of arg.
-
Else
-
fnArcSineRad = Atn(vntSine / dblTemp) ' Complete derived math function.
-
End If
-
End If
-
End If
-
-
EXIT__ArcSine:
-
If Not blnEditPassed Then Err.Raise cOVERFLOW ' Raise overflow error if arg failed edits.
-
Exit Function
-
-
ERROR_ArcSine:
-
On Error GoTo 0 ' Turn off error trapping.
-
blnEditPassed = False ' Should never happen!
-
Resume EXIT__ArcSine ' But if it does give overflow error.
-
-
End Function
10 15925 FishVal 2,653
Recognized Expert Specialist
Hello.
Variant type includes array.
So, just change FiveMileCircleG en function return value type to Variant. It should work. -
Public Function FiveMileCircleGen(lat1 As Double, lon1 As Double) As Variant
-
Regards,
Fish
Thanks Fish. When I remove the "()" from
Public Function FiveMileCircleG en(lat1 As Double, lon1 As Double) As Variant()
It then gives me a "Subscript out of range" error. To fix that I change the line to the following:
Redim ResultArray(i To iNumSides)
Now it gives me a 'run-time error 13, type mismatch'
Loren
NeoPa 32,579
Recognized Expert Moderator MVP
As far as I can tell, there is no way of defining a function explicitly to return an array.
However, it is possible to define it as a Variant, and set the return value as an array (Variants can handle arrays).
This quick function illustrates how this can be done : - Public Function Test() As Variant
-
Test = Split("A,B,C", ",")
-
End Function
Some code to illustrate that this works (and that works from the Immediate pane) is : - For A=0 to 2:?Test()(A),:Next
NeoPa 32,579
Recognized Expert Moderator MVP
I suggest you may need to build up your array first, with all the flexibility available to properly defined variables, then simply assign this array to the function return value when done.
Why not pass the array to the function (now sub)? - Dim a(1 To 3) As Long
-
Public Sub xx(ary As Variant)
-
ary(1) = 1
-
ary(2) = 2
-
ary(3) = 3
-
End Sub
-
-
Call xx(a)
-
Debug.Print a(1), a(2), a(3)
-
NeoPa 32,579
Recognized Expert Moderator MVP
The dimensions may not be known in advance of running the code OB.
In any case, the logic of it would tend to be obscured by that rather than clarified (if that makes sense). The function should be in control of what it produces - rather than being dependant on outside code.
In this case, the dimensions are known. It is a single dimension array. True, long is not the correct type, but I only posted a simple example. I could have posted: - Dim a() As Double
-
Public Sub xx(ary As Variant)
-
ReDim a(0 To 2, 1 To 3)
-
ary(0, 1) = 1
-
ary(0, 2) = 2
-
ary(0, 3) = 3
-
Debug.Print a(0, 1), a(0, 2), a(0, 3)
-
End Sub
I may not understand, but I thought that when a function was called, any parameters passed "By Reference" were passed as a pointer to the variable. Because any memory is released when a function ends, the return value must be created by the compiler/VBA as workspace within the calling routine, and a pointer to that space passed to the function.
In other words, Call subX(a,b,c) would pass 3 pointers, ptr->a, ptr->b, ptr->c. c=funcX(a,b) would pass 3 pointers, ptr->a, ptr->b, ptr->workspace. Any function could be written as a subroutine if the last line of the subroutine were an assignment to the last argument.
My function is as much in control as any function. When I enter the function line - ... Function (arg1, arg2, ...) As Type
where type is a valid data type, I am I not telling the compiler what workspace it needs to generate before it invokes the function? The function is not in control, the calling code is.
I was looking for a solution to lasmith329's problem. There should be no error 13 mismatch with this. That was all I was attempting to do.
Thanks for everyone's input. With your help and finding just the right tutorial I was able to finally get the function to return an array. FYI, here's the final code: - Public Function FiveMileCircleGen(lat1 As Double, lon1 As Double) As String
-
-
Dim ArrayReturned As String
-
Dim i As Integer
-
Dim radial As Double, angle As Double
-
Dim lat2 As Double
-
Dim lon2 As Double
-
Dim dlon As Double
-
Dim modNum As Double
-
Dim modDiv As Double
-
Dim distance As Double
-
Dim lngFN As Long
-
-
-
'convert coordinates to radians
-
lat1 = Deg2Rad(lat1)
-
lon1 = Deg2Rad(lon1)
-
-
-
Const iNumSides = 360
-
-
distance = Radius5mls / 6378137 'divided by radius of earth at the equator, in meters
-
-
'// Create an array to hold all of the points:
-
'ReDim ResultArray (361)
-
Dim ResultArray(iNumSides) As String
-
-
ArrayReturned = ""
-
'// Use trigonometry to points:
-
For i = 1 To iNumSides
-
angle = i
-
radial = Deg2Rad(angle)
-
lat2 = fnArcSineRad(sIn(lat1) * Cos(distance) + Cos(lat1) * sIn(distance) * Cos(radial))
-
dlon = ATan2(sIn(radial) * sIn(distance) * Cos(lat1), Cos(distance) - (sIn(lat1) * sIn(lat2)))
-
modNum = (lon1 + dlon + PI)
-
modDiv = 2 * PI
-
lon2 = (modNum - modDiv * Int(modNum / modDiv)) - PI
-
-
lat2 = Rad2Deg(lat2)
-
lon2 = Rad2Deg(lon2)
-
-
ResultArray(i) = CStr(lon2 & "," & lat2 & ",0 ")
-
ArrayReturned = ArrayReturned & ResultArray(i)
-
-
Next i
-
-
FiveMileCircleGen = ArrayReturned
-
-
End Function
NeoPa 32,579
Recognized Expert Moderator MVP @OldBirdman
I'm sorry OB. I'm afraid I have a tendency to treat things very literally. In this case I took this as a Request For Comment.
My response was not intended to be critical, and certainly not to imply you shouldn't have posted it. It was simply my opinions of the general approach illustrated in your proposed solution (with supporting rationale).
It's not my habit to offer such opinions unrequested (unless I feel that somehow the code suggested may cause real issues - certainly not the case in this instance), but as I say, I am prone to treat things quite literally. Please ignore if not interested.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Laura |
last post by:
Help. Below is my code. Getting Type mismatch error on
the noted line. I'm trying to send an array (aryNewD)
with 4 columns and x rows to a function to save all the
array info into a SQL Server table via a stored
procedure. Keep getting this error. Any suggestions?
Code:
'ASP:
if blnNewD then
|
by: mmethe |
last post by:
The following snipplet produces "Data type mismatch in criteria
expression". It must be someting simple. I will ultimately need this
where condition to contain 3 clauses. I am posting it with 2 thinking
I cna manage to get the 3rd on there with a little help from all of
you. The objective is to open a report based on 3 values supplied in a
form. Lemme know if you need more info. Here is the code and thanks
in advance.....
Private...
|
by: jg |
last post by:
in VB 2005 the class myClass, I have
Dim someStringArrary() As String
---
sub New()
MyBase.New()
... 'values assigned to elements of someStringArrary
End Sub
......
|
by: Ayoa |
last post by:
I am attempting to call a web service from Access 2003 (using the built-in
web services reference tool). The web service should return an array of
integers.
Example web method (C#)
public int GetIntArray()
{
int a = new int;
|
by: lalbee |
last post by:
I am trying to use the Dcount function but receive a data type mismatch
error, can someone help me determine the cause?
Count: DCount("","qry-final everhome results"," <'0' ")
Thanks!
| |
by: cyberdwarf |
last post by:
Hi
Any idea why the following (cut down) function gives a "Type Mismatch" error?
I've tried almost everything, in terms of arrays, etc, but I need to pass back 3 values (int, int, double)
"GradeData" is simply a user-defined type containing 2 integers and a double..
Public Function GroupGradeCount(GroupIdent As Integer, GradeIdent As Integer, ColNum As Integer) As GradeData
Dim Res1 As Integer
Dim Res2 As Integer
Dim Res3 As Double...
|
by: Davros9 |
last post by:
Trying to get Regular Expressions working.......
----------------
Public Function SepString(InField As String) As String
''seperates on space and comma
Dim RE As New RegExp
Dim Matches As Match
RE.IgnoreCase = True
|
by: psychomad |
last post by:
Please, can someone help me out to solve this error, i've been
searching throughout my codes and yet i didnt succeed in finding the
error!!!!
The Error is:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Data type mismatch in criteria expression.
|
by: ross m. greenberg |
last post by:
I'm trying to create a function in JavaScript using Dreamweaver8 such that when a user hits the ' F1' key when a text box is selected a separate "pop-up" window will open, with text of my choice.
Does anybody have any pointers or even some source code? I use ASP on my server.
Thanks!
Ross
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |