Results 1 to 12 of 12
  1. #1
    Join Date
    Jul 2023
    Posts
    5

    VBA To have a button add Date to table field

    I currently have a button that does some functions. With in it i want to add a date to a table.

    Current code of the button

    Code:
    Private Sub butChoseOrderReport_Click()On Error GoTo butChoseOrderReport_Click_Err
       DoCmd.OpenForm "frmOrderReportButtons", acNormal, "", "", , acNormal[DateOrdered].Value = Now()butChoseOrderReport_Click_Exit:   Exit Sub
    butChoseOrderReport_Click_Err:   MsgBox Error$   Resume butChoseOrderReport_Click_Exit
    End Sub


    I want it to add a date to the Order Table Called Order and the field DateOrdered in the table. Not good with VBA and can't figure out the code to make it work.

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,656
    Need more info.

    Apparently your opening a form. Is it a data entry form? Are you trying to get the default value of a field on your form to equal Now()?
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,969
    What happened to carriage returns in your code?

    Why not just set Default Value property of textbox or field in 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.

  4. #4
    Join Date
    Jul 2023
    Posts
    5
    Quote Originally Posted by moke123 View Post
    Need more info.

    Apparently your opening a form. Is it a data entry form? Are you trying to get the default value of a field on your form to equal Now()?

    The form does not have a dateordered its just in the table. I would prefer not to have it on the form and just have it add the date in the table to those unique order number..

    Yes it opens a form.

  5. #5
    Join Date
    Jul 2023
    Posts
    5
    Quote Originally Posted by June7 View Post
    What happened to carriage returns in your code?

    Why not just set Default Value property of textbox or field in table?
    Hi not sure what a carriage return is? The dateordered is only in the table and not on the form.. I would like when i hit the button the date automatically populates in the table for that order number with out having to have it on the form.

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,020
    This is more like what your code should look like.
    Code:
    Private Sub butChoseOrderReport_Click()
    On Error GoTo butChoseOrderReport_Click_Err
       DoCmd.OpenForm "frmOrderReportButtons", acNormal, "", "", , acNormal
       [DateOrdered].Value = Now()
       butChoseOrderReport_Click_Exit:
       Exit Sub
    butChoseOrderReport_Click_Err:
       MsgBox Error$   
       Resume butChoseOrderReport_Click_Exit
    End Sub
    How is anyone meant to make sense of what you posted?


    Field does not have to have a control on the form, so you can just set it to date. Now() includes Date & Time. ​So unless you meant that use Date, as if you later compare to dates only, the time element is going to throw you off, unless you are aware of it.

    I used to use code like this.
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Me.NewRecord Then
        Me.CreatedDate = Now()
        Me.CreatedBy = Environ("username")
    Else
        Me.AmendedDate = Now()
        Me.AmendedBy = Environ("username")
    End If
    End Sub
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    Join Date
    Jul 2023
    Posts
    5
    Thanks I added that in on the button and get this error Click image for larger version. 

Name:	Screenshot 2023-07-23 082703.jpg 
Views:	13 
Size:	10.8 KB 
ID:	50514
    Code:
    Private Sub butChoseOrderReport_Click()
    On Error GoTo butChoseOrderReport_Click_Err
       DoCmd.OpenForm "frmOrderReportButtons", acNormal, "", "", , acNormal
       [DateOrdered].Value = Now()
    butChoseOrderReport_Click_Exit:
       Exit Sub
    butChoseOrderReport_Click_Err:
       MsgBox Error$
       Resume butChoseOrderReport_Click_Exit
    End Sub






    Quote Originally Posted by Welshgasman View Post
    This is more like what your code should look like.
    Code:
    Private Sub butChoseOrderReport_Click()
    On Error GoTo butChoseOrderReport_Click_Err
       DoCmd.OpenForm "frmOrderReportButtons", acNormal, "", "", , acNormal
       [DateOrdered].Value = Now()
       butChoseOrderReport_Click_Exit:
       Exit Sub
    butChoseOrderReport_Click_Err:
       MsgBox Error$   
       Resume butChoseOrderReport_Click_Exit
    End Sub
    How is anyone meant to make sense of what you posted?


    Field does not have to have a control on the form, so you can just set it to date. Now() includes Date & Time. ​So unless you meant that use Date, as if you later compare to dates only, the time element is going to throw you off, unless you are aware of it.

    I used to use code like this.
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Me.NewRecord Then
        Me.CreatedDate = Now()
        Me.CreatedBy = Environ("username")
    Else
        Me.AmendedDate = Now()
        Me.AmendedBy = Environ("username")
    End If
    End Sub

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,020
    On what line does that occur?
    Note your last acNormal is incorrect for the OpenForm.
    You need to use Intellisense, that is what it is there for.

    Is the field in the source of the form?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    Join Date
    Jul 2023
    Posts
    5
    Hello no there is no field in the form.. IDK what intellisense is.. Newbie here.



    Quote Originally Posted by Welshgasman View Post
    On what line does that occur?
    Note your last acNormal is incorrect for the OpenForm.
    You need to use Intellisense, that is what it is there for.

    Is the field in the source of the form?

  10. #10
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,430
    [DateOrdered] must be a field within the form's recordsource.

    Edit: I see now that WGM as already alluded to this.

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,020
    Quote Originally Posted by NewAccessUser12345 View Post
    Hello no there is no field in the form.. IDK what intellisense is.. Newbie here.
    Might want to start watching some YouTube videos then.
    As I mentioned, the field/control does not have to be on the form, but it does have to exist in the source.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  12. #12
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,656
    your open form code
    Code:
    DoCmd.OpenForm "frmOrderReportButtons", acNormal, "", "", , acNormal
    has an incorrect window mode argument. it should be acWindowNormal
    Code:
    DoCmd.OpenForm "frmOrderReportButtons", acNormal, "", "", , acWindowNormal
    Since you dont have any filter or where criteria and you are using the defaults for all the optional arguments you only need
    Code:
    DoCmd.OpenForm "frmOrderReportButtons"
    No need to use "" as a placeholder for optional arguments.

    edit: Just noticed Gasman mentioned acWindowNormal. oops.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

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

Similar Threads

  1. Replies: 6
    Last Post: 01-16-2022, 04:47 PM
  2. Replies: 1
    Last Post: 11-16-2014, 09:10 AM
  3. Replies: 7
    Last Post: 06-20-2014, 08:25 AM
  4. Replies: 3
    Last Post: 12-24-2013, 04:20 PM
  5. Replies: 3
    Last Post: 08-16-2012, 11:25 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