Hello:(for each next statement)
what is this statement does and
how to use and plz example of if
Bye
Printable View
Hello:(for each next statement)
what is this statement does and
how to use and plz example of if
Bye
Hi,
U usually use 'for' loops this way:
for iCounter=... to ...
'code to be repeated
Next i
This is especially useful when U know how many times U want to repeat some code and/or when the code is dependent on iCounter.
But in some cases U need to do some operation on every object in a collection, doesn 't matter how many objects are there
Then 'for each ...' statement is very helpful.
The syntax is:
For each (var1 - usually object type) in (collection of objects of var1 type)
'repeated code
next (var1)
Want an example? Here is one:
Dim rst As Recordset
Dim fld As Field
Set rst = New ADODB.Recordset
rst.OpenRecordset "Customers", dbOpenDynaset 'Customers is a name of a table in a database
'(A)
For Each fld In rst.Fields
MsgBox fld.Name
Next fld
rst.Fields.Delete rst.Fields(3).Name
'(B)
For Each fld In rst.Fields
MsgBox fld.Name
Next fld
in the example above, in part A, U get names of all fields in rst (columns' names);
then U remove one column, third one
and again U use for each statement to obtain names of all fields after deleting.
To do the same with 'for i = ... to ...' U should know how many fields there are in the recordset before and after deleting
The 'for each' statement is very nice, I think U'll like it
Best wishes,
Syl
For each can also be used with arrays stored in variants.
Code:For each item in VariantArray
...
Next item
i didn't know you could do that!Quote:
Originally posted by kedaman
For each can also be used with arrays stored in variants.
Code:For each item in VariantArray
...
Next item
that helps me out with some other code too!
Have fun ;)
how would i go about setting values of an array?
Well guys, I really need your help..
I am looking for a way to do
for each ...
but for all the animations in a microsoft agent character.
any idea ?
set YourArray(index)=yourobject
da_silvy, that's what you meant?
try this:
tell me if it works!Code:For Each AnimationName In Character.AnimationNames
List1.AddItem AnimationName
Next
yes, but it does not work for meQuote:
Originally posted by kedaman
set YourArray(index)=yourobject
da_silvy, that's what you meant?
why not?Code:for i = 1 to 20
myArray(i) = i
next i
thank you da_silvy !!
I don't get it, this property does not appear in the drop down ..
anyway, thank you it is working.
that's a pleasure, but i don't know why it doesn't appear in the drop down box, must be a little secret of microsoft's
Well, probably, anyway good to have people like you around :-)
Thanks
what are you using the agent for?
just make a "virtual firend" on the desktop ..
It reads me stuff that I put in the clipboard, and from time to time do/say stuff randomly.
I have another question that I just posted, maybe you can help me, I need to know how to use the microsoft voice recognition .., I can't make it "listen" to me (I know that you can use the commands with the characters, but I want to use the voice recognition, it seems more flexible).
dasilvy, it should. What type of array are you using?
i don't know anything about it???
my last message was for kedaman!
and now for asabi
you could read up about recognition on the microsoft developers website, and i will try to put some code together for you. From memory, you have to add a function to the menu, then have code for it, and assign a voice command to it.Quote:
Originally posted by asabi
just make a "virtual firend" on the desktop ..
It reads me stuff that I put in the clipboard, and from time to time do/say stuff randomly.
I have another question that I just posted, maybe you can help me, I need to know how to use the microsoft voice recognition .., I can't make it "listen" to me (I know that you can use the commands with the characters, but I want to use the voice recognition, it seems more flexible).
i would also suggest downloading the complete documentation, as that is very technical (too hard in some places) and will help you will an answer.
Let's make a test da_silvy:
does this work for you?Code:Dim A as variant, B() as byte, C as variant
A=Array(1,3,7)
Redim B(2)
B(2)=A(2)
C=B
Msgbox A(2) & "," & B(2) & "," & C(0)
thanks guys .. :-)
that example works, but how do i fix this?
i want a(20) to = 20Code:Dim A As Variant
Private Sub Command1_Click()
For Each Item In A
List1.AddItem A
Next
End Sub
Private Sub Form_Load()
For i = 0 To 20
A(i) = i
Next
End Sub
but how?
try something like that:
orCode:Dim a(20) As Variant
For i = 0 To 20
a(i) = i
Next i
Code:Dim a() As Variant
ReDim Preserve a(1) As Variant
For i = 0 To 20
ReDim Preserve a(UBound(a) + 1) As Variant
a(i) = i
Next i
yes, thanks, that was what i was looking for
That's an array of variants, not an array stored in a variant! not really what you wanted or?
Anyway here's the array variant, in your code:
note, arrays of variants are really slow, don't use them, i made it a integer array for your purpose.Code:Dim A As Variant
Private Sub Command1_Click()
For Each Item In A
List1.AddItem Item'not A
Next
End Sub
Private Sub Form_Load()
Dim B(20) as integer
For i = 0 To 20
B(i) = i
Next
A=B
End Sub
thanks kedaman