Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with a loop 1

Status
Not open for further replies.

Rexxx

MIS
Oct 16, 2001
47
0
0
I need to loop through the number of rows in a dataset by changing the row number of the following:

coDS.tables("company").Rows(0).Item("coname")

First, how do I stop a loop in a dataset once it reaches last record?
Second, do I change the "Rows(0)" to this "Rows(i)" with i being defined as i+1 each time through the loop?
 
Dim i as integer

For i = 0 to coDS.tables("company").Rows.Count - 1
coDS.tables("company").Rows(i).Item("coname") = whatever
Next

you dont have to worry about it coming to the end of the dataset like you used to have to with recordsets.

D'Arcy
 
Two ways to accomplish this

1. Use the for each construct
Dim row As DataRow

For each row in coDs.Tables("company")
'put code here to get column data
Next


2. Use the For i construct
dim i as integer
For i=0 to coDs.Tables("company").Count
coDS.tables("company").Rows(i).Item("coname")
Next


I prefer the for each construct myself. Another thing if you use strongly typed datasets things are much easier. Check them out at msdn.microsoft.com
That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Zarcom, using either of your suggestions gives me the following compiler error. What to do?

Compiler Error Message: BC32023: Expression is of type 'System.Data.DataTable', which is not a collection type.

Source Error:



Line 65:
Line 66: Dim row As DataRow
Line 67: For each row in coDs.Tables("company")
Line 68:
Line 69: Dim agentid As String

 
For each row in coDs.Tables("company")
'put code here to get column data
Next

should be

For each row in coDs.Tables("company").Rows
'put code here to get column data
Next

and

dim i as integer
For i=0 to coDs.Tables("company").Count
coDS.tables("company").Rows(i).Item("coname")
Next

should be

dim i as integer
For i=0 to coDs.Tables("company").Rows.Count - 1
coDS.tables("company").Rows(i).Item("coname")
Next

Mark just probably forgot to include the .Rows part. Both of his will work though.
:)

D'Arcy
 
oops my bad thanks D That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top