PDA

Click to See Complete Forum and Search --> : [RESOLVED] Forms and functions


GIS_Mike
Jul 27th, 2005, 07:35 AM
Hi all

I'm afraid I know the answer to this already, but...

I have a user form that asks for a file to open. When the user clicks the browse button, a function creates a browse window and allows the user to select a file. The function returns the file name and path to the form (i.e. C:\DooDoo\stinky.dog). When the user clicks the "Open" button this file path is passed along so the program can do all sorts of inappropriate things.

What I was wondering, is it possible to get a function to return multiple values? Ideally, I would have two variables, one for the path (C:\DooDoo) and one for the file name (stinky.dog).
When you tell me that this isn't possible, how then do I split the two? I can't foresee using some string manipulations as the length of the path and file name will vary. Unless I did some sort of backwards string counting that split the string at the backslash before the file name.
Oh bother.

Is it possible to make a function hold an array?

Oh yeah, I'm using a function because it was the only way I saw to get the values back to the form, other than public variables.
!! I just has an epiphany... Perhaps I can set the two string values to the forms variables inside the function.. something like
frmPickaFile.strFilePath = strPathfromFunction
I'll go try it out. While I'm doing that, anyone else have a suggestion??

Super thanks

Mike

dannymking
Jul 27th, 2005, 07:42 AM
Is this Excel or Access?? And What version?


'Code after the return from the dialog
Dim arrFile(1 To 2) As String
arrFile(1) = Mid(FileName,1,InStrRev(FileName,"\")
arrFile(2) = Mid(FileName,InStrRev(FileName,"\")

GIS_Mike
Jul 27th, 2005, 11:53 AM
It's neither Excel nor Access.. It's ArcGIS, which I have a feeling no one else around here uses. Preventing me from bothering too deep into specifics.

The question is more of a general VBA/VB6 syntax type question. Is it possible to return multiple values from a single function call?

RobDog888
Jul 27th, 2005, 12:04 PM
Sure it is! :D (I also have ArcView GIS and MapObjects but I havent used it in years. So its not currently on my system :D)

Here is an example I just wrote a minute ago that returns a semicolon delimited string. Then I parse the string into an array. You could probably do the same. The example is in Excel VBA.
Public Function ListSheets() As String
Dim sSheets As String
Dim i As Integer
For i = 1 To ActiveWorkbook.Sheets.Count
sSheets = sSheets & ActiveWorkbook.Sheets(i).Name & ";"
Next
ListSheets = Left$(sSheets, Len(sSheets) - 1)
End Function

Private Sub UserForm_Initialize()
Dim ar() As String
Dim i As Integer

ar = Split(ThisWorkbook.ListSheets, ";")
For i = 0 To UBound(ar)
ComboBox1.AddItem ar(i)
Next
Erase ar
End Sub

GIS_Mike
Jul 27th, 2005, 01:25 PM
Oh man I'm telling you, ArcGIS is where it's at!

Ok, now I may be reading your code incorrectly but it seems to me that you made a nifty little work around that almost embeds a function call into an array.
That's sexy. Kind of Python-esque. I like.

RobDog888
Jul 27th, 2005, 01:34 PM
Really, Thanks! I havent see Python code before. There is a way to go direct and pass an array argument but I usually use this type of method. If you want to do it direct I can search for some code thats on the forums that I know of that will also work.

GIS_Mike
Jul 27th, 2005, 02:06 PM
If you have a free moment, I'd appreciate it. I think what you've shown me would suffice but I'm always interested to learn more.

Mike

RobDog888
Jul 27th, 2005, 02:10 PM
Ok, here is a good example. Note the limitations in doing it this way. ;)

http://www.vbforums.com/showpost.php?p=2079651&postcount=4