Results 1 to 10 of 10
  1. #1
    streub is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Oct 2017
    Posts
    42

    Clearing all controls on a form

    I came across this code for clearing a text box.

    Code:
    Sub ClearFormText(frm As Form) Dim ctl As Control Dim txtBx As TextBox For Each ctl In frm.Controls If ctl.ControlType = acTextBox Then Set txtBx = ctl If txtBx.Tag = "YourFancyResetTextTag" Then txtBx.Value = Null End If End If Next ctl End Sub
    Is there a method to clear all controls without using a tag or control button when exiting a form?



    I use a form to locate a record with a subform to display the record information.

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    you can use a select case statement to reset a control depending on its type

    along the lines of,,

    select case ctl.ControlType

    case acTextBox

    case acListbox

    etc...

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    I assume this is to clear UNBOUND controls used to input search criteria and there are no BOUND controls on the form.
    If you are exiting (closing?) form, why do you need to bother clearing?


    Can combine the Case statements.

    Case acTextBox, acListBox, acComboBox
    ...

    How many controls? Maybe just as easy and fast to reference explicitly instead of looping.
    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.

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,388
    Careful. If the textboxes are bound to a table/query, you could blank out the selected record fields.

  5. #5
    streub is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Oct 2017
    Posts
    42
    The search criteria text box is unbound. However, the subform contains only bound controls.

    The criteria is entered in the text box located on the primary form. When the control button is clicked a subform come to view with the results. I can then modify and save the record.

    Ideally, I want the subform to close after the record has been saved and the primary form to display the text box as blank. What you all are suggesting is above my skill level and that is okay. I am a novice.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    What do you mean by 'come to view' - are you actually opening another form?

    You have only one control - a textbox - as filter criteria?

    Might find this of interest http://allenbrowne.com/ser-62.html
    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.

  7. #7
    streub is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Oct 2017
    Posts
    42
    Yes, a second form opens. I have only one text box for search.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Can do like this:

    ...
    DoCmd.OpenForm "formname", , , "fieldname=" & Me.txtBx, acDialog
    Me.txtBx = Null
    ...

    The acDialog parameter will suspend code execution on the main form until the second form closes.
    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.

  9. #9
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    FWIW, the textbox declaration is not necessary & kinda pointless.
    Code:
    Sub ClearFormText(frm As Form)
    Dim ctl As Control
       For Each ctl In frm.Controls 
          If ctl.ControlType = acTextBox And ctl.Tag = "YourFancyResetTextTag" Then ctl = Null
       Next ctl
    End Sub
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    streub is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Oct 2017
    Posts
    42
    Thank you all for the assistance and patience.

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

Similar Threads

  1. Clearing Form Data
    By s.carter in forum Forms
    Replies: 5
    Last Post: 06-28-2015, 05:32 PM
  2. Clearing Sub Form
    By eagle670 in forum Forms
    Replies: 19
    Last Post: 06-23-2014, 02:31 PM
  3. Error When Clearing Controls on a Form
    By dccjr in forum Programming
    Replies: 4
    Last Post: 09-20-2013, 11:10 AM
  4. Clearing a Form
    By dabatman in forum Forms
    Replies: 4
    Last Post: 01-20-2012, 11:51 AM
  5. clearing a form
    By macsterling in forum Forms
    Replies: 0
    Last Post: 07-31-2008, 10:38 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