Results 1 to 5 of 5
  1. #1
    data808 is offline Noob
    Windows XP Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    727

    Default value of a field

    Not sure if default value is what I need but what I want to do is just have some text in the field to let the user know that this field is optional. So I don't need the content to be active. Just want it to be in the field if nothing is entered. The table should also know that nothing is entered in the field. So if the user decides to type something in then the field becomes active and is the recorded into the table.



    Does anyone know how to do this? I checked a lot if the settings in properties but can't find the right one.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,972
    I've never done anything like that. Fields can be set as required and code can validate before the record is saved.

    Users should not be working directly with tables and queries - just forms and report.

    I suppose the simplest is to just include that in caption of label control on form.
    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.

  3. #3
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I suppose you could have a default value in a textbox control and then in the form's before update event handler check to see if the value still exists.

    if me.txtControlName.value = "DefaultText" then
    me.txtControlName.value = ""
    end if

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,972
    Nice, ItsMe.

    Only I would set the field to Null because I don't allow empty strings in tables.

    Would be a lot of coding if a lot of fields are optional. If a lot of fields are optional, that indicates a non-normalized database structure.
    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.

  5. #5
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Here's a hack for doing this kind of thing, but it does takes some doing, and can only be done on a Single View Form (not on a Datasheet or Continuous View Form):

    • Delete the Textbox’s attached Label
    • Create new Label for Textbox with the Caption of choice (such as Optional Field)
    • Set BackStyle and BorderStyle of the Label to Transparent
    • Position Label inside of the Textbox

    Now, use this code, replacing TargetFieldLabel and TargetField with the actual names of your Controls:

    Code:
    Private Sub Form_Current()
    
    If Nz(Me.TargetField, "") <> "" Then
      TargetFieldLabel.Visible = False
    Else
      TargetFieldLabel.Visible = True
    End If
    
    End Sub


    Code:
    Private Sub TargetField_GotFocus()
     
      TargetFieldLabel.Visible = False
    
    End Sub


    Code:
    Private Sub TargetFieldLabel_Click()
    
      TargetField.SetFocus
    
    End Sub


    Code:
    Private Sub TargetField_LostFocus()
    
    If Nz(Me.TargetField, "") <> "" Then
      TargetFieldLabel.Visible = False
    Else
      TargetFieldLabel.Visible = True
    End If
    
    End Sub


    I have to tell you, though, if I were doing this, I'd use the BackColor of Controls to denote their optional status, or more likely, to denote the required fields.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

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

Similar Threads

  1. Default value in field based on value in other field
    By smith.jr in forum Database Design
    Replies: 2
    Last Post: 01-08-2014, 12:29 PM
  2. Default Information for an existing field HELP
    By opinionsrfun in forum Access
    Replies: 1
    Last Post: 06-19-2013, 06:30 AM
  3. Default Value from Another Field
    By ineedaccesshelp in forum Access
    Replies: 5
    Last Post: 11-28-2012, 12:37 PM
  4. Form Field Default Setting
    By roofbid in forum Programming
    Replies: 3
    Last Post: 12-17-2010, 10:53 AM
  5. Default Value in table = Field plus 3 days
    By AmyLynnHill in forum Access
    Replies: 1
    Last Post: 08-03-2008, 01:58 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