Results 1 to 4 of 4
  1. #1
    scottdg is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2015
    Posts
    31

    loop and add from query to table

    I have a table that on button click needs to have all the records removed and then new records added from a query. The table is a list of store_name, store_id, employee_name and address, and amount_owed. I want the code to loop through and check each time to make sure that a record does not already exist. If it does rather than write to the fields listed above the name and address should be written to fields called employee_name2 and address2. I can delete the data from the table with no problem. But I am not sure how to add. In my code below I tried just writing the store_ID (primary key) to the table and I do not receive an error but nothing is written. I didn't want to add any additional logic until I make sure I am writing it to the table correctly.
    Dim rs As adodb.Recordset
    Dim rs2 As adodb.Recordset
    Dim tbl As String
    Dim qry As String


    tbl = "Select * from myTable"
    qry = "Select * from myQuery"
    Set rs = New adodb.Recordset
    Set rs2 = New adodb.Recordset
    'Delete records from myTable
    With rs
    .Source = tbl
    .ActiveConnection = CurrentProject.Connection
    .CursorType = adOpenDynamic
    .LockType = adLockOptimistic
    .Open

    Do Until .EOF
    .Delete
    .MoveNext
    Loop
    End With
    'End Delete

    With rs
    Do Until rs.EOF
    Debug.Print rs.Fields("Store_ID")
    rs.AddNew
    rs.Fields(3) = rs2!AgentNo
    rs.Update
    rs.MoveNext
    Loop
    End With

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Wow. Okay, rs is already at EOF when you try to add, so I doubt it would add anything anyway. rs2 is never set to anything, so I don't see how it could have a value. I'd empty with simply:

    CurrentDb.Execute "DELETE * FROM TableName"

    You can add records with an append query, you don't need to loop. If you did need to, you'd loop the query and add to the table.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    you dont loop thru records to delete them,
    instead you make a delete query and do it all at once.
    Add criteria if needed.

  4. #4
    scottdg is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2015
    Posts
    31
    Yea - I know. This was being converted from an old VB6 application I was having trouble getting to work. I realized the delete and forgot to change it before I posted.

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

Similar Threads

  1. Replies: 27
    Last Post: 12-29-2015, 08:30 AM
  2. Replies: 9
    Last Post: 05-08-2015, 02:36 PM
  3. Replies: 3
    Last Post: 03-10-2013, 07:04 AM
  4. create table using something like loop query
    By learning_graccess in forum Queries
    Replies: 20
    Last Post: 04-18-2012, 09:52 AM
  5. Loop a table to use field in query
    By jdubp in forum Programming
    Replies: 0
    Last Post: 03-04-2008, 11:48 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