Results 1 to 6 of 6
  1. #1
    4 Star Lounger
    Join Date
    Jan 2003
    Location
    Central Florida, USA
    Posts
    505
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Reset Form Controls to default (Acc 2003 )

    Is there a way to access a controls default value?

    I have a form with several unbound controls. After the user is is finished with a specific record, and the data is saved, I want to reset ALL 30 controls on the form to their "default". 28 of the 30 are text boxes, and two are check boxes.

    I prefer not to set up lines of VB code to for each control, but instead would like to loop through the controls in a for/next loop, something like a:
    For each control in . . . .loop

    Thanks in advance for your ideas.

    Ken

  2. #2
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Reset Form Controls to default (Acc 2003 )

    Try this

    Dim ctrl As Control
    For Each ctrl In Me.Controls
    If ctrl.Tag = "Reset" Then
    ctrl.Value = ctrl.DefaultValue
    End If
    Next ctrl
    Set ctrl = Nothing

    Because the collection of controls includes all the labels etc, you don't want to do this for every control. You could test the control type etc, but instead I have just used the tag property of the control to indicate which ones I want this to apply to - I have put Reset into the tag property.
    Regards
    John



  3. #3
    4 Star Lounger
    Join Date
    Jan 2003
    Location
    Central Florida, USA
    Posts
    505
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Re: Reset Form Controls to default (Acc 2003 )

    John,

    AWESOME! Thanks.

    I did end up with a challenge I don't know how to address. For text fields that are formatted as currency, it no longer displays "$0.00", but instead displays "0". Is there a property I can access that display the default value as "$0.00"?

    Thanks

    Ken

  4. #4
    4 Star Lounger
    Join Date
    Jan 2003
    Location
    Central Florida, USA
    Posts
    505
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Re: Reset Form Controls to default (Acc 2003 )

    John,

    I think I found a work around.

    Dim ctrl As Control
    For Each ctrl In Me.Controls
    If Left(ctrl.Name, 3) = "txt" Then
    ctrl.Value = ctrl.DefaultValue
    If ctrl.Value = 0 And ctrl.Format = "Currency" Then
    ctrl.Value = "$0.00"
    End If
    End If
    Next ctrl
    Set ctrl = Nothing

    Is there a better approach?

    Thanks!

    Ken

  5. #5
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Reset Form Controls to default (Acc 2003 )

    I don't know if this is better, but it works if the default value is more than zero.
    <pre>Dim ctrl As Control
    For Each ctrl In Me.Controls
    If Left(ctrl.name, 3) = "txt" Then

    If ctrl.Format = "Currency" Then
    ctrl.Value = CCur(ctrl.DefaultValue)
    Else
    ctrl.Value = ctrl.DefaultValue
    End If
    End If
    Next ctrl
    Set ctrl = Nothing
    </pre>

    Regards
    John



  6. #6
    4 Star Lounger
    Join Date
    Jan 2003
    Location
    Central Florida, USA
    Posts
    505
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Re: Reset Form Controls to default (Acc 2003 )

    John,

    Yes, I like that MUCH better!

    Thanks,
    Ken

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •