Results 1 to 13 of 13
  1. #1
    ts_hunter is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    30

    Setting a DateTime field back to "empty"


    I have several date fields on my form. Depending on what the user selects for options, certain date fields are set to 0 and disappear as they are not needed. However, if they change their mind, I want to make the date fields reappear and have their value go back to what it was when the form initially opened.

    What is the trick to clear out the contents of a datetime field? Null does not work, 0 gives me 12:00:00am. I just want an empty box like my other untouched datetime fields.

    Any ideas?

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Depending on the purpose, I will use a default value like 1-1-1990. The date data type will store dates ranging from 1 January 100 to 31 December 9999.

  3. #3
    ts_hunter is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    30
    I tried that, but I am afraid the user will 'forget' to enter a valid date if I set it to 1/1/1900. That is why I want to set it to the value it has when the form initially opens, which is an empty box.

  4. #4
    Micron is online now Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    You don't say how the textbox gets its value after the form opens with no value being shown. Is the textbox bound? If not and you are using the Default Value property being equal to Date(), just set that value to "" in code. If it is bound, I think you can set it to "" even if the bound field is date/time data type. Regardless, if using a default date as suggested or "", you will have to code for the textbox being one of those values anyway to prevent using meaningless data, will you not?

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    A textbox bound to date/time field cannot be set to empty string. Set it to Null.

    Why does Null not work? What did you try? What happens?
    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.

  6. #6
    ts_hunter is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    30
    Quote Originally Posted by Micron View Post
    You don't say how the textbox gets its value after the form opens with no value being shown. Is the textbox bound? If not and you are using the Default Value property being equal to Date(), just set that value to "" in code. If it is bound, I think you can set it to "" even if the bound field is date/time data type. Regardless, if using a default date as suggested or "", you will have to code for the textbox being one of those values anyway to prevent using meaningless data, will you not?
    The form is being opened in add mode (acFormAdd) because it is a new record. The text box is bound to a DateTime field in my datafile. Yes, I will have data validation when the user clicks "Done".

  7. #7
    ts_hunter is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    30

    Cool

    FIGURED IT OUT!!!!!!

    When I tried to set it to Null with the code below:

    Me.AssetTransfer.Enabled = True
    Me.AssetTransfer.Visible = True
    Me.AssetTransfer.Value = Null

    I get this error





    I went back into SQL Server and changed my AssetTrasfer field to allow Nulls and now I can set it to Null and get an empty box. I will trap for users leaving it blank in my validation code under the "Done" Button. THANKS ALL!!!!

  8. #8
    Micron is online now Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Quote Originally Posted by June7 View Post
    A textbox bound to date/time field cannot be set to empty string.
    Well, in version 2007 it seems you can. I tested it first because I didn't think so, but when I cycled back and forth between the records with the navigation button, the date was gone after my command button code changed the control value to "". And yes, the table field is date/time, the form is bound to the table, and the control to the table field.
    Also, poster said That is why I want to set it to the value it has when the form initially opens, which is an empty box which I reasoned meant that it was not bound, therefore it could be set to "". Looks like I can do it anyway.
    Maybe I've overlooked something, but here's what I see after this runs and here is the field data type:
    Private Sub Command12_Click()
    Forms!form1.Text10 = ""
    End Sub
    Click image for larger version. 

Name:	dateNotEmpty.gif 
Views:	12 
Size:	3.8 KB 
ID:	21630 Click image for larger version. 

Name:	dateEmpty.gif 
Views:	12 
Size:	3.3 KB 
ID:	21629 Click image for larger version. 

Name:	tblField1.gif 
Views:	12 
Size:	2.4 KB 
ID:	21631

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Ooops! I made an assumption because date/time is a number and number fields cannot have text and I always associated empty string with text type data. But apparently Access does something 'behind the scenes' with the empty string and actually results in Null field. I typed "" into date/time and number fields and the field is Null. I also experimented with typing spaces and that does not error, Access just drops leading/trailing spaces.
    Last edited by June7; 08-13-2015 at 02:11 PM.
    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.

  10. #10
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Back in the day I used to assign empty strings to text fields and then test for empty strings during data validation. Whatever it is that Access does nowadays blew all of that out the window for me. I did not know that assigning an empty string to a Date field or Date control would result in a Null. I prefer the old way.

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I don't allow empty strings in text fields. I always test for Null, regardless of field type.
    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.

  12. #12
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by June7 View Post
    I don't allow empty strings in text fields. I always test for Null, regardless of field type.
    Not even for 5 minutes? Ideally, you would not have a Null or an empty string in a field. The point I was trying to make is that I found it easiest to write code that checked for "", 0, or 1-1-1990. This way I would not have to test for Null and "".

    I guess now we do not have to test for both. However, I do believe that there is a way for the user to assign "" to a control before a full commit to the table. So if you are trying to validate before a commit, you are still challenged with testing both Null and "". My solution is to pass the value to a function as a Variant. The function will determine the Variant type and then apply logic based on the Variant type. Not a big fan of using expensive Variants but this seems to be the only solution.

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I will be more alert to issues with checking for Null but so far not run into any.
    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.

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

Similar Threads

  1. Replies: 3
    Last Post: 04-22-2013, 06:08 AM
  2. Replies: 17
    Last Post: 06-04-2012, 05:11 PM
  3. How Do I Remove "Time" from a Datetime Field
    By James Parker in forum Queries
    Replies: 4
    Last Post: 01-06-2012, 03:05 PM
  4. Replies: 16
    Last Post: 07-22-2011, 09:23 AM
  5. replace a empty field with the word "none" how??
    By techexpressinc in forum Queries
    Replies: 1
    Last Post: 01-15-2010, 11:02 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