Filling in Data Table Using Loop: Check to see if Data Changes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nico3334
    New Member
    • Aug 2007
    • 28

    Filling in Data Table Using Loop: Check to see if Data Changes

    I'm filling in a Report with SQL data using VB code. I'm using LOOP and MoveNext. Before using MoveNext, I would like to be able to check whether the new data is equal to the previous data that was added. I'm not sure if MovePrevious is the best way to accomplish this. Here's an example of my code:

    With rs
    .MoveFirst
    strData = Trim(!data)

    Do While Not .EOF

    If Not .BOF Then
    .MovePrevious
    strDataPrev = Trim(!data)
    .MoveNext
    End If

    If (strData <> strDataPrev) Then

    'The record is new

    End If

    .MoveNext
    Loop
    .Close
    End With

    Will this work? Or is another method better?
  • Tig201
    New Member
    • Mar 2007
    • 103

    #2
    [QUOTE=nico3334]I'm filling in a Report with SQL data using VB code. I'm using LOOP and MoveNext......Q UOTE]

    The first thing I would Note is that you are not setting “strData” After the first Record. Next I would recommend setting “strDataPrev = strData” after you check to see if they are equal as apposed to moving backwards and forwards through the table.

    Comment

    • QVeen72
      Recognized Expert Top Contributor
      • Oct 2006
      • 1445

      #3
      Hi,

      Open a New Recordset for the condtion and check with the data.. Why open whole recset and loop thru and do the "Moves"..?

      Regards
      Veena

      Comment

      • nico3334
        New Member
        • Aug 2007
        • 28

        #4
        Originally posted by QVeen72
        Hi,

        Open a New Recordset for the condtion and check with the data.. Why open whole recset and loop thru and do the "Moves"..?

        Regards
        Veena
        Sorry, I added in some extra code to try to describe my problem better.

        I'm retrieving data from a sql table using .movenext and loop. When it is inserting the data, I would like it to check whether the next record it will be adding is equal to the last record it added. If it not is equal, I want to add an occurence to an array. Does this code look ok? Thanks.
        ''''''''''''''' '''''''
        Dim arrData() As String
        ReDim arrData(Col, 0)

        With rs
        .MoveFirst
        strData = Trim(!Data)

        Do While Not .EOF

        If Not .BOF Then
        .MovePrevious
        strDataPrev = Trim(!Data)
        .MoveNext
        End If

        If (strData <> strDataPrev) Then

        'Add Occurence to Array
        ReDim Preserve arrData(UBound( Col) + 1)

        End If

        arrData(Col, 0) = strData

        .MoveNext
        Loop
        .Close
        End With

        Comment

        Working...