Results 1 to 7 of 7
  1. #1
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352

    VBA to return a textbox to default value


    I have a button to reset a form (undo data entry) and would to reset text boxes to their default values since I have a date box that shows today's date by default. Is there a command that does this? This is my current code behind the button. It does its job and resets all the fields to null, but I want the default date to persevere.
    Code:
    Private Sub btnReset_Click()
    Dim ctl As Control
    For Each ctl In Me.Controls
    If ctl.ControlType = acTextBox Then
    ctl.Value = ctl.OldValue
    ElseIf ctl.ControlType = acComboBox Then
    ctl.Value = Null
    End If
    Next ctl
    End Sub

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I didn't test this it, but
    Code:
    ctl.Value = ctrl.DefaultValue
    should work......

  3. #3
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    I get the error "the value you entered isn't valid for this field."
    all my field types are text, one is a memo, and one is date/time. I don't see why there is a problem.

    Maybe there is a way to call out my date textbox individually to reset it?

  4. #4
    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
    Maybe I'm reading this incorrectly, but I believe that you're over thinking this. To undo entered data, all you need is

    Me.Undo

    Controls that are empty, when a New Record is initiated, will be returned to Null, and any Controls that have Default Values will be populated with their Defaults.

    Linq ;0)>

  5. #5
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    Ok modified my code a bit and called out the date field specifically since I have some restrictions on other fields preventing a zero string value.
    Code:
    Private Sub btnReset_Click()Dim ctl As Control
    For Each ctl In Me.Controls
    If ctl.Name = "DateEntry" Then
    ctl.Value = ctl.DefaultValue
    ElseIf ctl.ControlType = acTextBox Then
    ctl.Value = ctl.OldValue
    ElseIf ctl.ControlType = acComboBox Then
    ctl.Value = Null
    End If
    Next ctl
    End Sub
    However, now the textboxes do not reset. The date successfully resets to the defualt value, and the combo boxes reset to nothing. But the text boxes do not.

  6. #6
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    Quote Originally Posted by Missinglinq View Post
    Maybe I'm reading this incorrectly, but I believe that you're over thinking this. To undo entered data, all you need is

    Me.Undo

    Controls that are empty, when a New Record is initiated, will be returned to Null, and any Controls that have Default Values will be populated with their Defaults.

    Linq ;0)>
    Wow, can't believe I over thought it that much. Thank you!

  7. #7
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I prefer to use "Select Case" instead of "IF...End If" , but I don't get any errors when I run this code:
    Code:
    Private Sub btnReset_Click()
        Dim ctl As Control
        For Each ctl In Me.Controls
            Select Case ctl.ControlType
                Case acTextBox
                        If ctl.DefaultValue = "Date()" Then
                            ctl.Value = Date
                        Else
                            ctl.Value = ctl.OldValue
                        End If
                Case acComboBox
                    ctl.Value = Null
    
            End Select
    
            '        For Each ctl In Me.Controls
            '        ElseIf ctl.ControlType = acComboBox Then
            '        End If
    
        Next ctl
    End Sub


    EDIT: Of course Linq is right.. I thought.... well, I don't know what I was thinking!

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

Similar Threads

  1. change default value of a textbox
    By bbxrider in forum Forms
    Replies: 4
    Last Post: 12-22-2014, 12:57 PM
  2. Default in Textbox
    By drunkenneo in forum Forms
    Replies: 7
    Last Post: 11-18-2013, 04:10 AM
  3. Replies: 3
    Last Post: 11-15-2012, 06:59 PM
  4. Textbox default value with datetimeyear
    By Perkowski in forum Access
    Replies: 8
    Last Post: 08-09-2012, 01:37 PM
  5. Replies: 13
    Last Post: 02-07-2012, 04:09 AM

Tags for this Thread

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