Results 1 to 14 of 14
  1. #1
    make me rain is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Posts
    6

    Looping through Records in SQL

    with MS access 07 + vb.net
    ( pre designed & data loaded file, i mean not the new database development)

    kindly find the attachment herewith pl

    in a table how can i compare the next row with previous row


    I need to compare + ( alter / delete ) the field values of current row & previous row

    in the database attached i want to do
    (1) if the
    CurrentRow employeeID = PrevRow EmployeeID AND
    DutyType = null THEN
    CurrentRow DutyType = PrevRow DutyType

    (2) if the
    CurrentRow employeeID = PrevRow EmployeeID AND
    CurrentRow RoasterDate = PrevRow RoasterDate AND
    CurrentRow DutyType = any one of the casualty in casualty table AND
    PrevRow DutyType <> any one of the casualty in casualty table
    THEN DELETE THE CASUALTY ROW




    i need to do this with SQL, any help please

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    First, there is no attachment with this post.

    Second, doubt can do this with SQL. Need VBA code.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Like june said you'd have to do this with VB script, it would be next to impossible to do with queries.

    Secondly, you would have to be, extremely, extremely careful of how you sort your data while running through it. I do not advocate deleting data. What exactly are you trying to accomplish can you give examples of the data and why you are attempting to delete data? is it that you want to get rid of possible duplicates? Is there some other reason for deleting data?

  4. #4
    make me rain is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Posts
    6
    i am unable to attach my .mdb file, because it is of 620 kb

    what i am trying is
    in a table with 4 columns (say for eg A,B,C,D)

    Using SQL
    i want to compare CURRENT ROW Fields with immediate previous ROW fields
    basing on some conditions.

    how can i do it please , is it possible with any self join query's etc.. but not with VBA,
    reason is my database is having 4.85 lak records, & i am handling it with vb.net

    any advise please

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Allen Browne's example offers a way to select records based on values found in other records. Remains to be seen if it can be adapted for update or delete procedure.

    You can zip the project and attach to post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    How large is your dataset, if you are bound and determined to delete records why not just use the find duplicates query wizard? then cycle through them manually to delete them (if this is a one time project and you're just trying to combine separate datasets).

    This is what I did.

    Create a table called Tbl_DupCheck
    Create 5 Fields
    Item_ID (autonumber)
    Item_Date (date/time)
    Item_Text (text)
    Item_Number (number)
    Item_Delete (yes/no)

    Populate it with this data:
    Tbl_DupCheck Item_ID Item_Date Item_Text Item_Number Item_Delete
    1
    1/1/2011 TESTTEXT 1 No
    2
    1/1/2011 TESTTEXT 1 No
    3 1/1/2011 TESTTEXT 2 No
    4 1/1/2011 TESSTTEXT 2 No
    5
    1/1/2011 TESTTEXT 2 No

    Create this query:
    Code:
    SELECT Tbl_DupCheck.Item_Date, Tbl_DupCheck.Item_Text, Tbl_DupCheck.Item_Number, Tbl_DupCheck.Item_ID
    FROM Tbl_DupCheck
    WHERE (((Tbl_DupCheck.Item_Date) In (SELECT [Item_Date] FROM [Tbl_DupCheck] As Tmp GROUP BY [Item_Date],[Item_Text],[Item_Number] HAVING Count(*)>1  And [Item_Text] = [Tbl_DupCheck].[Item_Text] And [Item_Number] = [Tbl_DupCheck].[Item_Number])))
    ORDER BY Tbl_DupCheck.Item_Date, Tbl_DupCheck.Item_Text, Tbl_DupCheck.Item_Number, Tbl_DupCheck.Item_ID;
    Call it Qry_DupCheck_Duplicates

    Run this code:
    Code:
    Dim iCurrID As Long
    Dim dCurrDate As Date
    Dim sCurrText As String
    Dim iCurrNumber As Long
    
    Dim iPrevID As Long
    Dim dPrevDate As Date
    Dim sPrevText As String
    Dim iPrevNumber As Long
    
    Set db = CurrentDb
    Set rst_CheckList = db.OpenRecordset("Qry_DupCheck_Duplicates")
    rst_CheckList.MoveFirst
    
    Do While rst_CheckList.EOF <> True
        iCurrID = rst_CheckList.Fields(3)
        dCurrDate = rst_CheckList.Fields(0)
        sCurrText = rst_CheckList.Fields(1)
        iCurrNumber = rst_CheckList.Fields(2)
    
        Debug.Print iCurrID
        
        If dCurrDate = dPrevDate And sCurrText = sPrevText And iCurrNumber = iPrevNumber Then
            sSQL = "UPDATE Tbl_DupCheck SET Item_Delete = -1 WHERE ((Item_ID) = " & iCurrID & ")"
            db.Execute sSQL
            Debug.Print sSQL
        End If
        
        iPrevID = iCurrID
        dPrevDate = dCurrDate
        sPrevText = sCurrText
        iPrevNumber = iCurrNumber
        
        rst_CheckList.MoveNext
    Loop
    It will mark all of the potential duplicates with a checkmark in the ITEM_DELETE field. From there you can determine whether or not you want to delete everything that is marked yes or no. As I said I'm very against deleting data unless it's absolutely necessary preferring instead ot use a field that you can void a record and set criteria for queries to ignore records marked yes.

    This is my data after I ran this code:
    Tbl_DupCheck Item_ID Item_Date Item_Text Item_Number Item_Delete
    1
    1/1/2011 TESTTEXT 1 No
    2 1/1/2011 TESTTEXT 1 Yes
    3
    1/1/2011 TESTTEXT 2 No
    4 1/1/2011 TESSTTEXT 2 No
    5 1/1/2011 TESTTEXT 2 Yes

  8. #8
    make me rain is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Posts
    6
    i need to do the things with SQL here
    i am attaching the zip file, please refer

    the pseudo code is like this
    Code:
    SELECT 
    CrewRoaster.EmployeeID,
    CrewRoaster.RoasterDate,
    IIF(EmployeeID IN LAST RECORD = EmployeeID IN CURRENT RECORD
          AND
        CrewRoaster.DutyType = "" ,CrewRoaster.DutyType IN PREV RECORD,CrewRoaster.DutyType) AS modified DutyType,
    
    IIF(EmployeeID IN LAST RECORD = EmployeeID IN CURRENT RECORD
    AND
       CrewRoaster.RoasterDate = CrewRoaster.RoasterDate IN PREV RECORD
    AND
      CrewRoaster.DutyType IN CURRENT ROW IS EXISTS in Casualty table 
    AND 
    CrewRoaster.DutyType IN CURRENT ROW IS NOT EXISTS in Casualty ,"Delete this record LEAVE + WORKING CAN'T CO-EXISTS")
    I hope some i may get here

  9. #9
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I can't open access 2007 records from here.

    Did you try setting up tables and code as I did?

  10. #10
    make me rain is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Posts
    6
    Ok i have attached the images here with

  11. #11
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    So which records in your dataset are you intending to delete? You haven't noted what your criteria for finding duplicates are.

    The only ones that match by employee ID and date is Employee C (record 6 and 7)

    And again, did you try setting up the example I showed you?

  12. #12
    make me rain is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Posts
    6
    My table is consists of 4.85 lak records , in fact i simulated the table schema & filled some data database try is a mock up file.

    and the next is i want to do it in SQL, but no VBA scripts

    the conditions are as i stated above

    the simple logic i am trying is
    (1) if the duty type is NULL and EmployeeID = EmployeeID (in previous row) then
    duty type = duty type of the prev row
    (2) there can not be two duty's for one employee, i mean employeeID "C" can't be shown ABSENT / duty in any ROASTER (i mean duty) for the same calender day
    & hence here Casualty (i mean ABSENT) should be eliminated.

    i hope you can do some thing

  13. #13
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I thought it was sort of clear this would be next to impossible to do without using VB script. If you're not willing to adapt your process I can't really help you any further. Sorry.

  14. #14
    make me rain is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Posts
    6
    Rp
    you took much of your valuable time for me
    & i am grateful to you
    thank you

    yours

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 0
    Last Post: 02-13-2011, 02:55 PM
  2. Looping in Access
    By ducthang88 in forum Programming
    Replies: 2
    Last Post: 12-04-2010, 07:43 PM
  3. Looping through a tbl to find a certain value
    By cwf in forum Programming
    Replies: 1
    Last Post: 05-17-2010, 04:02 PM
  4. Looping code for printing reports
    By Lockrin in forum Access
    Replies: 2
    Last Post: 02-09-2010, 05:48 AM
  5. Looping syntax
    By fadiaccess in forum Access
    Replies: 1
    Last Post: 10-23-2009, 02:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums