I dont think that is posible..
Is there a work around?
Seahag
Printable View
I dont think that is posible..
Is there a work around?
Seahag
Why not a disconnected / virtual ADO or DAO recordset ?
Idea was to load from recordset into array .. than manipulate
as I desire.
Problem is I want to use class mods to handle all the calculating, and all the info returned is in groups of 6
??
I've not used them really, but what about a collection object, would that maybe be better ?
I think I am getting what your saying.
Use an object instead of Array>?
The way a couple of the guys explained it to me - that the collection was similar to an array, although the collection is, yes, an object so it'd take more memory up.
You can't return an array from a function, but thinking about it you could just use a sub & declare the array outside of it (private or public) :
Code:Option Explicit
Dim aryNumbers() As Integer
Private Sub Form_Load()
Dim i As Integer
For i = 0 To 11
ReDim Preserve aryNumbers(i)
aryNumbers(i) = i
Next i
End Sub
Private Sub Command1_Click()
Dim i As Integer, strTemp As String
For i = 0 To UBound(aryNumbers) - 1
strTemp = strTemp & aryNumbers(i) & vbCrLf
Next i
MsgBox strTemp
End Sub
I like your record set idea better. Makes more sense.
(I hate Variables.)
Thanks
Seahag
you can return and array....
[Highlight=VB]
Private Sub mySub()
Dim x
x = myFunction
msgbox x(0)
msgbox x(1)
msgbox x(2)
msgbox x(3)
End Sub
'---------------------------------------------------------------------------
Private Function myFunction()
Dim y() As String
ReDim y(3)
y(0) = "One"
y(1) = "Two"
y(2) = "Three"
y(3) = "Four"
myFunction = y
End Function
True, if you left it as a variant, then you could.
Why don't you send it as a paramter ByRef
Private Sub ChangeArrayData(ByRef MyArray as String())
'The code
End Sub
Don't know why it work?
But thanks..
Whats the ByRef thing do?
Not positive about the specifics but it means
By Reference as opposed to By Value.
It basically make a variable local to the function.
It's really useful with controls.
Yep, well done there jcfowl, I didn't even think of that one!
This is your best option seahag. There are 2 ways of passing a variable.
1) ByRef or By Reference passes in the actual value, so you can manipulate the source straight away (like you're doing)
2) ByVal or By Value is a copy of the variable & has a fixed value which you cannot alter (bit like a constant).
Bit confused.. could you show an example
using a Function? Pls
Thanks
VB Code:
Private Function ChangeData(p_MyVar as String) As String ChangeData = p_MyVar & "I changed it" End Function Private Sub ChangeDataByRef(ByRef p_MyVar as String) p_MyVar = p_MyVar & "I changed it" End Sub Private Sub Main() Dim MyVariable, MyVariable2 as String MyVariable = "Hello " MyVaribale2 = "Hello " MyVariable = ChangeData(MyVariable) ChangeDataByRef(MyVariable2) End Sub
Both MyVariable and MyVarible2 will equal the same thing.
"Hello I changed it"
Beat me to it - sorry SeaHag, had a long lunch there (hic)! ;)
Quote:
Private Sub Form_Load()
'set up variables
Dim x As String, y As String
x = "Hello World"
y = "Hello World"
'show the altered values returned from the function
MsgBox myfunction(x, y), , "Function values"
'now show the original values again
MsgBox "ByVal x variable = " & x & vbCrLf & vbCrLf & _
"ByRef y variable = " & y, , "Original Values"
Unload Me
Set Form1 = Nothing
End Sub
Private Function myfunction(Optional ByVal x As String, Optional ByRef y As String) As String
x = Replace(x, "l", "_CHANGED_")
y = Replace(y, "l", "_CHANGED_")
myfunction = "ByVal x variable = " & x & vbCrLf & vbCrLf & _
"ByRef y variable = " & y
End Function
Now time for My lunch
:)
... u can return an array from a function ...
Ahhh, I thought we'd argued over this in the past, but couldn't find the post & thought I was dreaming, c'mon then where's the sample code to prove it ('cause I'm interested to know this too :D )!!!
yeah i was about to slap together some code, but ive to find a windows xp pro product key for the corporate edition like 10 minute ago ...
VB Code:
Private Function returnArray() As Long() Dim x() As Long, i As Long: ReDim x(9) For i = 0 To 9 x(i) = i Next returnArray = x End Function Private Sub Form_Load() Dim a() As Long, i As Long a = returnArray For i = 0 To UBound(a) Debug.Print a(i) Next End Sub
You can't do it with Fixed Length Arrays though
right?
Dim A(18) as Long
Just pass it by reference then.
If i remember, I had trouble doing that.
Will it work through class mods (by using properties?)
use Redim PreserveQuote:
You can't do it with Fixed Length Arrays though
Um you can't ReDim Preserve a fixed array I don't think
humm you have to declare you variable like : myArray()
and under it you just use Redim Preserve myArray(8)
I always do that because when it fixed you cant change it so with only 1 line you can change it like you want so I think it's better.
Just to clear this one up - You can do a redim preserve, but NOT on a fixed array.
- Dim arySample()
Is declaring a dynamic array - an open one which you can alter the dimensions of.- Dim arySample(5)
Is declaring a fixed - "you can't resize me I have 6 elements - bugger off" array.
yes that about what I just said ;)