Results 1 to 8 of 8
  1. #1
    bkirsch is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Nov 2011
    Posts
    39

    Auto date based on check box


    I have a "Recevied" check box and a "RECEPTION_DATE" text box and I want RECEPTION_DATE to update when Received is checked and be null when it is unchecked...I assume the easiest way is to write the code for "on click" of "Received" but i cant seem to get it...


    Any chance someone could quickly write the correct code for me?

    Thanks

  2. #2
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    I have this code behind a command button [cmdCheckedOr_Not]

    You can put the code behind the On Click event of your check box - or in the On Lost Focus event of the check box.

    Code:
     
    Private Sub cmdCheckedOr_Not_Click()
     
    If Received.Value = True Then
        RECEPTION_DATE.SetFocus
        RECEPTION_DATE.Text = "Put the Date you want here."
    Else
        RECEPTION_DATE.SetFocus
        RECEPTION_DATE.Text = ""
    End If
     
    End Sub
    Hope this helps.

  3. #3
    bkirsch is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Nov 2011
    Posts
    39
    This is what I entered into the on click code but it is returning an error...It says Invalid use of Null...?

    Private Sub Received_Click()
    If Received.Value = True Then
    RECEPTION_DATE.SetFocus
    RECEPTION_DATE.Text = "Now()"
    Else
    RECEPTION_DATE.SetFocus
    RECEPTION_DATE.Text = "Null"
    End If
    End Sub
    Last edited by bkirsch; 01-13-2012 at 03:28 PM. Reason: I am getting an invalid use of Null...?

  4. #4
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Your Code 'looks' correct - but unless I have your db to look at - I can't tell you why you are getting the 'invalid use of Null' message.

    Can you post your db here?

    In my experience that has usually meant that there is something in the code that assumes there will be a value - but there is a Null instead.

    By the way - do you really want to put the literal text "Now()" in that text box?
    Or do you want to put the current system Date Time in there?

    If you want just the Date - use
    RECEPTION_DATE.Text = Date()

    For Date AND TIME:
    RECEPTION_DATE.Text = Now()

  5. #5
    bkirsch is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Nov 2011
    Posts
    39
    ok after playing with it I got it to work but I still get an error everytime that says the macro or function set to the beforeupdate or validationrule property for this field is preventing MS access from saving the data in the field....?


    Private Sub Received_Click()
    If Received.Value = True Then
    RECEPTION_DATE.SetFocus
    RECEPTION_DATE.Text = Now()
    Else
    RECEPTION_DATE.SetFocus
    RECEPTION_DATE.Text = ""
    End If
    End Sub
    Last edited by bkirsch; 01-13-2012 at 04:03 PM. Reason: add

  6. #6
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    The validation rule message tells me you should look in either:
    1. the design for the Table to which the field belongs or in
    2. the Property Sheet -> Data Tab -> Validation Rule [or Input Mask???] for the text box on the Form.

    one of those might have some kind of Validation set up.
    If what you're trying to put in there [Now()] doesn't match the validation - then you might get that message.
    In short - you're trying to put something in that text box or table field that is contrary to what it has been set up to receive.

    I hope this helps!!

  7. #7
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by bkirsch View Post
    This is what I entered into the on click code but it is returning an error...It says Invalid use of Null...?

    Private Sub Received_Click()
    If Received.Value = True Then
    RECEPTION_DATE.SetFocus
    RECEPTION_DATE.Text = "Now()"
    Else
    RECEPTION_DATE.SetFocus
    RECEPTION_DATE.Text = "Null"
    End If
    End Sub
    You don't need to set the focus to the field.
    You don't need ".VALUE". It is the default property.
    The ".Text" property holds the uncommitted value. Not appropriate in this case.
    You are trying to put the text string "Now()" into a date type field. (raises an error)


    If [Received] is a boolean (Yes/No) type field in the table and [RECEPTION_DATE] is a date type field, try this code in the after update event of the [Received] check box:

    Code:
    Private Sub Received_AfterUpdate()
       If Me.Received = True Then
          'if you want only the date, use
          Me.RECEPTION_DATE = Date
          'if you want date and time, use
          '      Me.RECEPTION_DATE = Now()
       Else
          Me.RECEPTION_DATE = Null
       End If
    End Sub

  8. #8
    bkirsch is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Nov 2011
    Posts
    39
    Awesome thanks so much.

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

Similar Threads

  1. Best way to check date.
    By Playerpawn in forum Access
    Replies: 1
    Last Post: 10-31-2011, 02:39 PM
  2. Date and time check
    By polis in forum Programming
    Replies: 2
    Last Post: 09-30-2011, 09:26 AM
  3. set value in combo box based off check box
    By scowens36 in forum Forms
    Replies: 2
    Last Post: 09-29-2011, 08:46 AM
  4. Replies: 1
    Last Post: 02-06-2011, 06:36 PM
  5. Queries Based On Check Boxes
    By Rubz in forum Queries
    Replies: 7
    Last Post: 05-07-2010, 03:46 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