Results 1 to 4 of 4
  1. #1
    accessnewb is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    New York, NY
    Posts
    116

    Set default value

    Hi all,


    I am trying to set a default value for a date textbox as 'mm/dd/yyyy'. And on focus of the textbox, the value needs to disappear and I would like it to reappear when the textbox loses focus. Also, when user types something into that textbox and then loses focus, then whatever the user typed needs to be displayed and not 'mm/dd/yyyy'.

    I have seen this in multiple webpages and for the life of me I can't get hte code right . I tried specifying a condition like if the form is dirty then when the textbox loses focus, display whatever the user has typed, else display 'mm/dd/yyyy'. But just because the form is dirty, doesn't mean that the particular date textbox is dirty (it could be some other fields). So now I am out of ideas.

    Any suggestions?

  2. #2
    pkstormy's Avatar
    pkstormy is offline Access/SQL Server Expert
    Windows XP Access 2003
    Join Date
    Mar 2010
    Location
    Madison
    Posts
    682
    I might add an unbound text box onto the form and then in the OnGotFocus event of your field, copy the value of your field into the unbound text box (you can make the unbound text box invisible).

    ex:
    if not isnull(me.MyField.value) then
    me.MyUnboundTextBox.value = me.MyField.value
    end if

    Then in the OnLostFocus event, copy unbound text box value into your field.
    if not isnull(me.MyUnboundTextBox.value) then
    me.MyField.value = me.MyUnboundTextBox.value
    end if

    You'll want to also make sure your field isn't formatted as a date so it can accept other type values.

    As far as the default value, you can either utilize the field's default property to set it to a date value, or the form's onload property to set the date value, or you can instead set it to populate the unbound text box and wait for the OnLostFocus event to fire after the focus is lost on the field.

    You may need to add a 'Refresh' command somewhere in your coding to refresh the screen.

    ex:
    me.MyField.value = me.MyUnboundTextBox.value
    Refresh

    (or me.requery)

    Note though that the technique of over-writing (by setting the value when they lose the focus on that field) from what someone newly enters into a field is not really the best technique.

  3. #3
    accessnewb is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    New York, NY
    Posts
    116
    Hi,
    This only works for unbound columns. For bound columns if the datatype is "date/time" and I set the default value as mm/dd/yyyy then I get the error, Run-time error '2113': The value you entered isn't valid for the field.

    So I made the datatype into a "text", but now the date-picker doesn't show for the bound textbox .

    I guess nothing can be done about it

  4. #4
    pkstormy's Avatar
    pkstormy is offline Access/SQL Server Expert
    Windows XP Access 2003
    Join Date
    Mar 2010
    Location
    Madison
    Posts
    682
    See this post: https://www.accessforums.net/code-re...ent-15774.html

    It's a replacement for the date picker and works much better with more control. Just import the CalendarPopuUp form and add a button on your form to open this form. See example coding on frmExamplePersons.

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

Similar Threads

  1. Default value
    By Hatye in forum Access
    Replies: 1
    Last Post: 06-19-2011, 05:16 PM
  2. Default Value by formula
    By sparlaman in forum Forms
    Replies: 15
    Last Post: 04-04-2011, 12:32 PM
  3. add new column and set default value
    By Juanna_v in forum Programming
    Replies: 4
    Last Post: 02-20-2011, 12:54 PM
  4. no way to set default export to .xls ?
    By yinchiao in forum Import/Export Data
    Replies: 0
    Last Post: 04-05-2010, 12:33 PM
  5. Default window
    By Tareq_1993 in forum Access
    Replies: 2
    Last Post: 11-26-2009, 09:37 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