Results 1 to 4 of 4
  1. #1
    AWADOS is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2019
    Posts
    5

    Recordset.MoveLast gives First record

    Hi,



    I have a table and I want to add a new record based on the last record in the recordset by doing this

    Code:
    Private Sub Comando26_Click()
    Dim dbn As Database
    Dim rstn As Recordset
    Dim lcn As IntegerDim 
    lfi As DateDim 
    lff As Date
    
    Set dbn = CurrentDb
    Set rstn = dbn.OpenRecordset("tabNomina", dbOpenTable)
    
    rstn.MoveLast
    lcn = rstn.Fields("codnom")
    lfi= rstn.fields("fecini")
    lff=rstn.fields("fecfin")
    
    lcn=lcn+1
    lfi=lff+1
    lff=lff+7
    
    rstn.AddNew
    rstn.Fields("codnom")=lcnrstn.fields("fecini")=lfi
    rstn.fields("fecfin")=lff
    
    End Sub
    The problem is that the recorset is saved as if the first record in the table was the last, so the second record in the table becomes the first in the recordset.
    How can I solve this problem? I used rstn.MoveLast and then rstn.MovePrevious that shows the real last record but i think that is not a good solution.

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,397
    two problems - you need to include

    rstn.update

    after you have assigned values

    and last on its own is meaningless in Access. you need to provide an order which you cannot do in a table (I'm assuming your 'tab' prefix means table), you need to use a query. If you happen to be getting the last record then that is a lucky coincidence

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Can use something like:

    Set rstn = dbn.OpenRecordset("SELECT * FROM tabNomina ORDER BY ID")
    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.

  4. #4
    AWADOS is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2019
    Posts
    5
    Thank you, it solved the problem.

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

Similar Threads

  1. A2003 - Me.RecordsetClone.MoveLast not working
    By GraeagleBill in forum Programming
    Replies: 3
    Last Post: 05-25-2016, 08:52 PM
  2. finding record in recordset
    By mik in forum Programming
    Replies: 1
    Last Post: 10-16-2014, 06:02 AM
  3. Movelast not working as expected
    By GraeagleBill in forum Programming
    Replies: 28
    Last Post: 08-04-2013, 01:37 PM
  4. Null values being returned when use movelast
    By ssalem in forum Programming
    Replies: 6
    Last Post: 03-15-2013, 10:25 AM
  5. doubt with MoveLast
    By fabiobarreto10 in forum Forms
    Replies: 8
    Last Post: 04-11-2012, 12:41 PM

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