Results 1 to 9 of 9
  1. #1
    griztkojr is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    30

    Help creating a VBA Code to run an Update Query

    Hello!

    I don't know how to code in VBA but many have suggested that its the best way to go with what I am trying to do. I have a form named frm_Employee that captures employee work information. In this form there is a subform named subfrm_EEJobHist. When I want to create a new job I call a form named frm_AddNewJob. I need to create a button that when I am adding a new record in the frm_AddNewJob, the query grabs all the prior jobs from that employee job history table and adds the current date to the field [JobEndDate] and then adds the new job.



    Hope it makes sense!

    Thank you

    Griztko

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    youd build an append query. (say its called: qaAddNewJob)
    using the key in the master form, grab a record from the history, and append the fields you want to the target table.
    add Date() to fill in the date field.(in the query)

    the code to run the query at button click:
    docmd.openquery "qaAddNewJob"

  3. #3
    griztkojr is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    30
    Quote Originally Posted by ranman256 View Post
    youd build an append query. (say its called: qaAddNewJob)
    using the key in the master form, grab a record from the history, and append the fields you want to the target table.
    add Date() to fill in the date field.

    the code to run the query at button click:
    docmd.openquery "qaAddNewJob"

    Hi ranman. Thank you for your prompt reply. So, I created a query where "criteria' is the key from the master form and JOBENDDATE is null but i don't now how to tell the query to update the field JOBENDDATE to Now(). How do i go about this?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    Put Now() in the UpdateTo row under the relevant field. Or use only VBA instead of query object.

    CurrentDb.Execute "UPDATE EEJobHist SET JobEndDate = Now() WHERE JobEndDate Is Null AND EmpID = " & Me.EmpID;
    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
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by June7 View Post
    Put Now() in the UpdateTo row under the relevant field. Or use only VBA instead of query object.

    CurrentDb.Execute "UPDATE EEJobHist SET JobEndDate = Now() WHERE JobEndDate Is Null AND EmpID = " & Me.EmpID;
    Not to derail or hi-jack the thread but this leads me to a question I've wondered about before: Is there any reason I should be doing something like

    Code:
    Dim db AS DAO.Database
    Set db = CurrentDb
    db.execute "...", dbFailOnError
    db.close
    set db = nothing
    vs the simple one liner you proposed:
    Code:
    CurrentDb.execute "...", dbFailOnError

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    I don't bother creating variables when I use them only once.

    If you prefer to not repeatedly type CurrentDb in favor of just db then establish the object variable, otherwise I am not aware of advantage. But that could be my amateur ignorance showing.
    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.

  7. #7
    griztkojr is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    30
    Quote Originally Posted by June7 View Post
    I don't bother creating variables when I use them only once.

    If you prefer to not repeatedly type CurrentDb in favor of just db then establish the object variable, otherwise I am not aware of advantage. But that could be my amateur ignorance showing.
    June,

    Thank you for your help. The query is working perfectly. My next issue is this, when I am modifying a record, i don't want the query to run. How can i do this? This is how i coded the button.

    Code:
    Private Sub btnSave_Click()
    On Error Resume Next
    DoCmd.SetWarnings (False)
    DoCmd.OpenQuery "qry_EndDateUpdate"
    DoCmd.SetWarnings (True)
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.Close
    
    
    End Sub

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    Conditional code:

    If Me.NewRecord Then
    DoCmd.SetWarnings (False)
    DoCmd.OpenQuery "qry_EndDateUpdate"
    DoCmd.SetWarnings (True)
    End If
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.Close
    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.

  9. #9
    griztkojr is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    30
    You are AWESOME!

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

Similar Threads

  1. Creating an update query
    By nick243 in forum Access
    Replies: 23
    Last Post: 10-28-2016, 01:58 PM
  2. Question on creating an update query
    By Ayiramala in forum Access
    Replies: 4
    Last Post: 10-28-2015, 11:00 AM
  3. Replies: 12
    Last Post: 12-11-2012, 01:47 PM
  4. Creating an Update/Append Query
    By Jray9242 in forum Import/Export Data
    Replies: 2
    Last Post: 04-23-2012, 06:31 PM
  5. Creating Query from Code, Concatenate fields
    By eww in forum Programming
    Replies: 5
    Last Post: 07-18-2011, 02:19 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