• Reset Form Controls to default (Acc 2003 )

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Reset Form Controls to default (Acc 2003 )

    Author
    Topic
    #453800

    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

    Viewing 0 reply threads
    Author
    Replies
    • #1124974

      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.

      • #1125033

        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

        • #1125035

          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

          • #1125037

            I don’t know if this is better, but it works if the default value is more than zero.

            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
            
            • #1125039

              John,

              Yes, I like that MUCH better!

              Thanks,
              Ken

    Viewing 0 reply threads
    Reply To: Reset Form Controls to default (Acc 2003 )

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: