Results 1 to 5 of 5
  1. #1
    sgp667 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    17

    VBA code to add new record through a button on a form

    Hi guys this is my first piece of VBA code ever. Its meant to create new record in "Hours" table when clicking on "NewPayButton", and in the field Beginning its supposed to use today's date.
    Needles to say it fails to do its job. Debugger points to the third line[Private Sub NewPayButton_Click()], with pop up saying "Object required".
    I'm not sure what object am I missing?

    Option Compare Database


    Private Sub NewPayButton_Click()
    Dim PayrollDb As DAO.Database
    Dim WHours As DAO.Recordset
    Dim Today As Date


    Set PayrollDb = CurrentDb
    Set WHours = PayrollDb.OpenRecordset("Hours")
    Set Today = Now


    WHours.AddNew
    WHours("Begining").Value = Today
    WHours.Update


    End Sub

    I've attached the database that uses this code.


    Thanks
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Try:

    Private Sub NewPayButton_Click()
    CurrentDb.Execute "INSERT INTO Hours(Beginning) VALUES(Now());"
    End Sub

    Why is this necessary? Why don't you bind form to the table and bind textboxes to fields? Entry to textbox will pass directly to the table.
    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
    sgp667 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    17
    This is just the first procedure this I will use I am making another one to make a loop so that I can input multiple records in on click.
    This code worked perfectly.

    So the way I understand this CurrentDb.Execute "" executes SQL code?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Yes, it does. SQL action statements. Of course doesn't apply to SELECT query statement.
    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.

  5. #5
    sgp667 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    17
    Thanks June7 you have been extremely helpful with all of my past questions(this one included).

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

Similar Threads

  1. Replies: 1
    Last Post: 08-09-2012, 04:19 PM
  2. Replies: 12
    Last Post: 03-14-2012, 10:54 AM
  3. Replies: 7
    Last Post: 11-23-2011, 08:14 PM
  4. Form Button with VB Code Does Nothing
    By esoikie in forum Access
    Replies: 11
    Last Post: 09-17-2010, 01:57 PM
  5. Form/report command button code
    By max3 in forum Forms
    Replies: 1
    Last Post: 08-26-2009, 02:18 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