Results 1 to 15 of 15

Thread: For Each...Next problem...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    66

    Post For Each...Next problem...

    Here is my code. I have the Timer set at 2 seconds for now. It pulls the data in fine, sets the text for the label fine. But it only shows the last row of data and does not rotate through the rows.

    Any ideas?

    Code:
    Public Class MarqueTest
    
        Private Sub MarqueTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.AdminMessTableAdapter.Fill(Me.MarqueDataSet.AdminMess)
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
            Dim dr As DataRow
            For Each dr In Me.MarqueDataSet.AdminMess.Rows
                TitleLabel1.Text = dr.Item("DateSubmited") & " - " & dr.Item("Title") & " - POSTED BY " & dr.Item("CallSignSubmitted")
            Next
    
        End Sub
    End Class

  2. #2
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: For Each...Next problem...

    try this:

    Code:
     For Each dr In Me.MarqueDataSet.AdminMess.Rows
                TitleLabel1.Text += dr.Item("DateSubmited") & " - " & dr.Item("Title") & " - POSTED BY " & dr.Item("CallSignSubmitted").tostring & Environment.NewLine
            Next
    You keep clearing the textbox instead of adding text into it

    Code:
     TitleLabel1.Text = "Hello"  ' Clear the textbox and add the string "Hello"
     TitleLabel1.Text += " Hello" ' Adding the string "Hello" to the text that already stored inside the text box.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  3. #3
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: For Each...Next problem...

    @Motil

    A little info, use &= instead of +=

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: For Each...Next problem...

    Why? What's wrong with += vs &= .... both have been overloaded for strings and produce the same result.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: For Each...Next problem...

    I think mickey has a point it could raise an expectation if you mix strings with integers i believe don't it ?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  6. #6
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: For Each...Next problem...

    In this particular situation i think there isn't any problem, but i think it's a good practice to use & for strings and + for numbers
    Last edited by mickey_pt; Jul 28th, 2009 at 04:55 PM. Reason: my very bad english writing...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  7. #7
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: For Each...Next problem...

    you're right my friend but when you working with 3-5 languages at the same time it's hard to remember the correct syntax all the time, i'm working a lot with javascript at my work and += is the way to go in any situation
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: For Each...Next problem...

    I was aiming for a little explanation to go with it... which is what I was trying elicit, and did get... on why using &= might be preferable over += .... to some of us, it's obvious, but to others it isn't so...

    That is all. And now back to your regularly scheduled postings.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    66

    Re: For Each...Next problem...

    Thank you all for your quick replys, I will try the suggestions. I just wanted to make sure we are on the same page.

    I am intending for the first row of data to show up and as the timer refreshes it moves to the next row of data until the end and starts over. Each time setting the text as described. Thus created alternating text like a new tickers, etc.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: For Each...Next problem...

    Your problem is that EVERY time the Timer Ticks you loop through ALL the data and show EVERY row, one after the other. As such EVERY time the Timer Ticks you're going to show the first row, then replace it with the second, then replace it with the third, ... then replace it with the last. That's why you always see only the last record. All the others get replaced so quickly that you never see them.

    What you should do is set an Integer variable to 0 and then, in the Tick event handler, you get the row at that index and then increment it by 1. That means that the first Tick will show the first row, the second Tick the second row, etc., etc. You'll obviously need to check whether you're at the last row each time and Stop the Timer if you are.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    66

    Re: For Each...Next problem...

    Thank you very much for that. I am understanding the process now but can you just give an example of how code for increasing by 1 would be.

    I asume

    dim start As Integer
    start = 0

    or something like that?

  12. #12
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: For Each...Next problem...

    That start var must be declared at the level of the class.
    Something like this:
    VB.NET Code:
    1. Dim start As Integer = 0
    2.  
    3.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    4.         Dim dr As DataRow
    5.         If MarqueDataSet.Tables("AdminMess").Rows.Count > start Then
    6.             dr = MarqueDataSet.Tables("AdminMess").Rows(start)
    7.             TitleLabel1.Text = dr.Item("DateSubmited") & " - " & dr.Item("Title") & " - POSTED BY " & dr.Item("CallSignSubmitted")
    8.             start += 1
    9.         Else
    10.             Timer1.Stop()
    11.         End If
    12.     End Sub

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    66

    Resolved Re: For Each...Next problem...

    Thanks Works fine.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    66

    Re: For Each...Next problem...

    Ok, next smal issue. I your code examle and it works like a champ BUT I cannot get it to do it continuiously. I have tried Do/Loop and other methods to no resolve.

  15. #15
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: For Each...Next problem...

    Do it continuously? What are you trying yo achieve?

    If you want that code never stop instead of
    vb Code:
    1. Timer1.Stop()
    put
    vb Code:
    1. start = 0

    Rate People That Helped You
    Mark Thread Resolved When Resolved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width