Results 1 to 3 of 3
  1. #1
    trevor40's Avatar
    trevor40 is offline Advanced db Manager
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    402

    loop through recordset and compare against worksheet range

    Hi, all I'm trying to create a loop that will update a worksheet if an employee is not listed,
    I am currently populating the worksheet from access vba, listing names as required, I now have to check if an employee in not listed on the worksheet to add it to the next row. the code below is what I have been trying so far but it still wont work. Any ideas ?



    Code:
        Set db = CurrentDb()
        Set rs = db.OpenRecordset("Casual employees")
        With rs
            .MoveFirst
            Do While Not .EOF
                fn = rs.Fields("First Name")
                Ln = rs.Fields("Last Name")
                bc = rs.Fields("code #")
                es = rs.Fields("Employment Type")
                For i = 8 To Mid(last_cell1, 4, 3)
                    If bc = Excel_Workbook.Worksheets("Summary Sheet").Range("A8:A" & Mid(last_cell1, 4, 3)).Value Then
                        GoTo loop1
                    Else
                        Excel_Workbook.Worksheets("Summary Sheet").Range("A" & Mid(last_cell1, 4, 3) + 1).Value = bc
                        Excel_Workbook.Worksheets("Summary Sheet").Range("B" & Mid(last_cell1, 4, 3) + 1).Value = Ln & ", " & fn
                        Excel_Workbook.Worksheets("Summary Sheet").Range("C" & Mid(last_cell1, 4, 3) + 1).Value = es
                        Excel_Workbook.Worksheets("Summary Sheet").Range("J" & Mid(last_cell1, 4, 3) + 1).Formula = "=SUM($D" & Mid(last_cell1, 4, 3) + 1 & ":$I" & Mid(last_cell1, 4, 3) + 1 & ")"
                        last_cell1 = Excel_Workbook.Worksheets("Summary Sheet").Cells.SpecialCells(xlCellTypeLastCell).Address
                    End If
    loop1:
                Next i
                .MoveNext
            Loop
        End With

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Assuming the worksheet and the table are sorted differently what you would have to do is cycle through your entire excel dataset to find out if the employee exists. I don't know the format of your excel file but it may be easier/quicker to link your existing excel spreadsheet temporarily into access then creating a query comparing the employee numbers (unique key I would guess) and appending 'new' items (ones that exist in your employee table but not on your spreadsheet. Then export those items only.

  3. #3
    trevor40's Avatar
    trevor40 is offline Advanced db Manager
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    402
    I'm sorry, I forgot to mark this tread Solved.

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

Similar Threads

  1. Loop Not Progressing Through Recordset
    By nmlinac in forum Programming
    Replies: 1
    Last Post: 02-15-2013, 10:54 AM
  2. Double loop recordset
    By silverspr in forum Programming
    Replies: 7
    Last Post: 03-07-2012, 01:43 PM
  3. Loop through recordset
    By eliedebs in forum Programming
    Replies: 1
    Last Post: 01-11-2012, 12:26 AM
  4. Import excel worksheet by range with non identical headers
    By snoopy2003 in forum Import/Export Data
    Replies: 1
    Last Post: 03-14-2011, 01:04 AM
  5. Loop through a list of table names and compare
    By mikneus in forum Programming
    Replies: 1
    Last Post: 05-21-2010, 10:36 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