Results 1 to 11 of 11
  1. #1
    Summit_IT is offline Advanced Beginner
    Windows 10 Access 2003
    Join Date
    Jan 2019
    Location
    Virginia
    Posts
    47

    Need code to update record rather than Add New record. Help

    The issue is two records appear in the table. I need the task and the time on the same record.
    Click image for larger version. 

Name:	Timer form.JPG 
Views:	66 
Size:	29.0 KB 
ID:	37344Click image for larger version. 

Name:	Log Table.JPG 
Views:	66 
Size:	30.4 KB 
ID:	37345

    [Private Sub cmdEnd_Click()Dim rs As DAO.Recordset
    Me.TimerInterval = 0
    Set rs = CurrentDb.OpenRecordset("Select * From tblLog")
    rs.AddNew


    rs!StartOfTest = Me.txtSessionStart
    rs!EndOfTest = Now()
    rs!ActualMinutes = Me.txtActualMinutes
    rs!ActualSeconds = Me.txtActualSeconds
    rs.Update
    rs.Close
    Set rs = Nothing
    MsgBox "Data logged.", vbOKOnly, "Logged"
    Me.txtActualMinutes = 0
    Me.txtActualSeconds = 0
    Me.txtSessionStart = Null
    End Sub]

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,407
    If the form is bound to tblLog fields, you will have the first record. There's no reason to do the rs.AddNew, which is the problem.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I think you'd need rs.Edit instead of rs.AddNew, but you'd also want to open the recordset to a particular record. It would without when there's only a single record, but when there are more that will always update the first record.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    Summit_IT is offline Advanced Beginner
    Windows 10 Access 2003
    Join Date
    Jan 2019
    Location
    Virginia
    Posts
    47
    I changed AddNew to Edit and got runtime error 3021. "No current record"

    You mentioned "you'd also want to open the recordset to a particular record". How would I do that?

  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
    Are there no records in the table? You could test for that, but I still think you'd want to open the recordset to a particular record. Or is the form also bound to the table? That's probably what davegri meant, and I agree. Instead of manipulating a recordset, just update the values on the form.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Summit_IT is offline Advanced Beginner
    Windows 10 Access 2003
    Join Date
    Jan 2019
    Location
    Virginia
    Posts
    47

    Need code to update record rather than Add New record. Help

    Quote Originally Posted by pbaldy View Post
    Are there no records in the table? You could test for that, but I still think you'd want to open the recordset to a particular record. Or is the form also bound to the table? That's probably what davegri meant, and I agree. Instead of manipulating a recordset, just update the values on the form.
    Two fields are bound. TaskID and Task Description. I am looking to use FindFirst method.

  7. #7
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,407
    Maybe you could tell us what the form is trying to do.
    What does the Start Timing button do? Does it create a new record? Does it populate the start session textbox?
    In the same fashion, what does the End Timing button do?
    Do the Start Timing and End Timing buttons deal with only one record?
    How does the Pause button interact with the above?

    I suspect that what you want to accomplish can be done without the recordset code.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Why not bind them all? I think davegri was on the right track, I was not. If you already have that record on the form, you wouldn't normally use code to update it.

    If a recordset was the right option, then I wouldn't use FindFirst, I'd open the recordset on an SQL statement that limited the results to the desired record.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Summit_IT is offline Advanced Beginner
    Windows 10 Access 2003
    Join Date
    Jan 2019
    Location
    Virginia
    Posts
    47
    Quote Originally Posted by davegri View Post
    Maybe you could tell us what the form is trying to do.
    What does the Start Timing button do? Does it create a new record? Does it populate the start session textbox?
    In the same fashion, what does the End Timing button do?
    Do the Start Timing and End Timing buttons deal with only one record?
    How does the Pause button interact with the above?

    I suspect that what you want to accomplish can be done without the recordset code.
    Here is how the buttons work.
    The timer subform works like this;

    1. Tab 0 is Task Description. Once a task is selected and the tab button is pressed, a new record is added to tblLog. See pic 2.


    Click image for larger version. 

Name:	Timer Subform.jpg 
Views:	52 
Size:	44.1 KB 
ID:	37422
    Figure 1 Subform timer

    Click image for larger version. 

Name:	tblLog data.jpg 
Views:	52 
Size:	23.5 KB 
ID:	37423
    Figure 2 tblLog with new record.


    1. Click “Start Timing”. This starts timer and inserts date and time in Session Start (Unbound).
    2. When “End Timing” is pushed, the unbound fields should populate the blank fields in tblTimer.
      1. StartofTest =Session Start, EndofTest= date and time end button was pushed, ActualMinutes, and ActualSeconds.

    After changing AddNew to Edit the following issue’s were discovered;

    1. Original problem disappeared.
    2. New problem, after I stop a task, and start a new one. Repeat Item 1 thru 3.

    I get this. Fig 3. The time was added to the first record. I need it to edit the last record.

    I guess I need code to select the last record to edit?

    Click image for larger version. 

Name:	tblLogdata1.jpg 
Views:	52 
Size:	32.9 KB 
ID:	37424
    Figure 3

  10. #10
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,407
    I guess I need code to select the last record to edit?


    Try something like this:
    Set rs = CurrentDb.OpenRecordset("Select Top 1 * From tblLog Order By Log_ID")

    If the * doesn't work, you will need to name the fields individually.

  11. #11
    Summit_IT is offline Advanced Beginner
    Windows 10 Access 2003
    Join Date
    Jan 2019
    Location
    Virginia
    Posts
    47
    Quote Originally Posted by davegri View Post


    Try something like this:
    Set rs = CurrentDb.OpenRecordset("Select Top 1 * From tblLog Order By Log_ID")

    If the * doesn't work, you will need to name the fields individually.
    I figured out the issue.

    Thanks,

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

Similar Threads

  1. Replies: 5
    Last Post: 03-29-2017, 11:22 AM
  2. Replies: 2
    Last Post: 04-02-2015, 08:04 PM
  3. Replies: 6
    Last Post: 02-24-2015, 01:19 PM
  4. Replies: 2
    Last Post: 06-28-2013, 12:58 PM
  5. Replies: 12
    Last Post: 03-14-2012, 10:54 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