Results 1 to 14 of 14
  1. #1
    robertjleach is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    9

    Macro

    Hi Guys

    How would I write a macro (step by step guide) so when a user presses a button it inserts the current date and time into the field?



    Best wishes

  2. #2
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    Here is a VBA solution (not tested for syntax errors)

    Quote Originally Posted by robertjleach View Post
    Hi Guys

    How would I write a macro (step by step guide) so when a user presses a button it inserts the current date and time into the field?

    Best wishes
    With your form in design view, highlight the button.
    With the property sheet for the button visible, find the "on click" event. Click on the ellipsis (3 dots) and you should get a box which allows you to choose "code builder".
    A visual basic form will pop up with something like
    Sub Yourbutton_OnClick()

    End Sub

    Between the sub-end sub, put

    me!textboxname = now()

    This assumes that you are displaying a textbox on a form.

  3. #3
    robertjleach is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    9
    That's fantastic! And thanks for the step by step guide.

    But... I need it to insert a new date/time on a new line every time the button is pushed. How can I manipulate the macro to do this?

  4. #4
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    You want to insert the time on a new line???

    Quote Originally Posted by robertjleach View Post
    That's fantastic! And thanks for the step by step guide.

    But... I need it to insert a new date/time on a new line every time the button is pushed. How can I manipulate the macro to do this?
    Are you really wanting to insert the date/time into a table field? Maybe you need to put the expression in the default value of a table's field and forget the button.


  5. #5
    robertjleach is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    9
    Basically I have a 'notes' field and the user will be adding notes and dating them as they go along.

    There will be mutliple notes in the field all with multiple dates on them. How can I achieve this?

  6. #6
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    An answer and a question

    Quote Originally Posted by robertjleach View Post
    Basically I have a 'notes' field and the user will be adding notes and dating them as they go along.

    There will be mutliple notes in the field all with multiple dates on them. How can I achieve this?
    On button click
    me.textbox= me.textbox& " " & Year(Now()) & "/" & Month(Now()) & "/" & Day(Now())
    However, it seems to me that a better design would be a notes table, consisting of UserID, Date, and Note fields

  7. #7
    robertjleach is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    9
    Code:
    me.textbox= me.textbox& " " & Year(Now()) & "/" & Month(Now()) & "/" & Day(Now())
    This is returning a syntax error

  8. #8
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    OK, you caught me.

    Quote Originally Posted by robertjleach View Post
    Code:
    me.textbox= me.textbox& " " & Year(Now()) & "/" & Month(Now()) & "/" & Day(Now())
    This is returning a syntax error

    I often post from a computer that doesn't have Access on it, so my answers aren't checked for syntax. Having said that, I'm posting from that computer again, and the error that I see is
    me!textbox = me!textbox & " " & Year(Now()) & "/" & month(Now()) & "/" & Day(Now())

    Also, "textbox" is meant to be your textboxname, not literally "textbox"
    My apologies.
    Hope this works for you.

  9. #9
    robertjleach is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    9
    That's perfect, thanks so much for your time.
    One more thing, is it possible to insert each date on a new line? I've got Enter Key behaviour set to new line on this field.

  10. #10
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    Arrow Glad it worked. I'm still not sure of how you are using this.

    Quote Originally Posted by robertjleach View Post
    That's perfect, thanks so much for your time.
    One more thing, is it possible to insert each date on a new line? I've got Enter Key behaviour set to new line on this field.
    It appears that you are using the button to insert the current date into a new record.
    (As I mentioned previously, you could do this by setting the default to Now(). That wouldn't change records with blank entries or nulls, but it should show the current date on new records.)
    Are you asking if the button click will work with each new record? I think it should.
    Will it work multiple times for multiple entries into your notes? I don't know. I think it could be made to work, but I'm minimally understanding the exact sequence and your criteria.

  11. #11
    robertjleach is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    9
    On each record there is a field. At the moment pressing the button multiple times will insert the date into the field as many times as you press it. That's perfect, I'm just wondering if it's possible instead of it going [Date][Date] it'd go [Date][NewLinewithinfield][Date]

  12. #12
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    You want the button to insert the date, and start a new line?

    Quote Originally Posted by robertjleach View Post
    On each record there is a field. At the moment pressing the button multiple times will insert the date into the field as many times as you press it. That's perfect, I'm just wondering if it's possible instead of it going [Date][Date] it'd go [Date][NewLinewithinfield][Date]
    You could conceivably stretch the characters on out by continuing to add text and other control information... Adding
    & Chr(10) to the previous code would insert a newline character. I believe Chr(13) is a return character. Not sure whether you need one, both, or something else entirely.

  13. #13
    robertjleach is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    9
    Neither
    Code:
    Me!Text30 = Me!Text30 & " " & Day(Now()) & "/" & Month(Now()) & "/" & Year(Now()) & Chr(13)
    or
    Code:
    Me!Text30 = Me!Text30 & " " & Day(Now()) & "/" & Month(Now()) & "/" & Year(Now()) & Chr(10)
    is working ;'(

    Please help?

  14. #14
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    Post I need more info.

    Quote Originally Posted by robertjleach View Post
    Neither
    Code:
    Me!Text30 = Me!Text30 & " " & Day(Now()) & "/" & Month(Now()) & "/" & Year(Now()) & Chr(13)
    or
    Code:
    Me!Text30 = Me!Text30 & " " & Day(Now()) & "/" & Month(Now()) & "/" & Year(Now()) & Chr(10)
    is working ;'(

    Please help?
    Tell me where we are, i.e. a click button puts the date into a textbox field that is being stored in a table?
    But it isn't doing what ...
    putting in a date followed by a new line...
    updating the record and going to the next one...
    did you try them using
    & chr(13) & chr(10)

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

Similar Threads

  1. Macro help
    By SFC in forum Access
    Replies: 4
    Last Post: 03-09-2011, 09:05 PM
  2. Help With Macro's
    By GDubbs780 in forum Programming
    Replies: 1
    Last Post: 02-25-2011, 11:43 AM
  3. Replies: 0
    Last Post: 01-12-2011, 12:43 PM
  4. Need help with macro
    By abc123 in forum Programming
    Replies: 6
    Last Post: 06-13-2010, 12:34 AM
  5. MACRO help please
    By DianeG in forum Forms
    Replies: 2
    Last Post: 04-20-2010, 07:19 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