Results 1 to 11 of 11
  1. #1
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    136

    Clearing values only from text boxes in Form

    Hi,



    I am trying to clear the values in the text boxes in my Access Form.

    I am working with Access 2016 and this is the code that I am using:

    Code:
    Private Sub Command55_Click()
    
    Dim ctl As Control
    For Each ctl In Me.Controls
        If ctl.ControlType = acTextBox Then
            ctl.Value = ""
        End If
    Next ctl
    
    
    End Sub
    but I am getting an error saying
    You can't assign value to this object
    I then tried changing
    Code:
    ctl.Value = ""
    to
    Code:
    ctl.Value = vbNullString
    and no luck.

    Any ideas?

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725

  3. #3
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    136
    It is bound to a table

  4. #4
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,799
    One or more of those controls are probably calculated. You can't assign a value to a calculated control. I would use the Tag property and base the selection on that and the type, rather than the type alone.
    EDIT
    Maybe I should have followed the link first...
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  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
    Quote Originally Posted by Micron View Post

    ...You can't assign a value to a calculated control...
    Same thing applies if you have an Autonumber Field that appears on the Form..

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

    All posts/responses based on Access 2003/2007

  6. #6
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    136
    Yes, only one of my fields is an Autonumber Field.
    How do I determine this in VBA i.e. is there anything like if clt.DataType = "Autonumber" then .....

    This is what I am trying to achieve:

    Code:
    Private Sub Command55_Click()
    
    
    Dim ctl As Control
    For Each ctl In Me.Controls
        If ctl.ControlType = acTextBox Then
            If ctl.DataType <> Autonumber Then
                ctl.Value = ""
            End If
        End If
    Next ctl
    
    
    End Sub

  7. #7
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,410
    If any error occurs, you can do this:
    Code:
    Private Sub cmdClear_Click()
    
    
    Dim ctl As Control
    On Error Resume Next
    For Each ctl In Me.Controls
        If ctl.ControlType = acTextBox Then
            ctl.Value = ""
        End If
    Next ctl
    On Error GoTo 0
    End sub
    Or trap specific errors like this:
    Code:
    Private Sub cmdClear_Click()
    On Error GoTo cmdClear_Click_Error
    Dim ctl As Control
    For Each ctl In Me.Controls
        If ctl.ControlType = acTextBox Then
            ctl.Value = ""
        End If
    Next ctl
    cmdClear_Click_EXIT:
        Exit Sub
    cmdClear_Click_Error:
        Select Case Err
            Case 2448   'ignore
                Resume Next
            Case Else
                MsgBox Err.Number & ", " & Err.Description
                Resume cmdClear_Click_EXIT
        End Select
    End Sub
    Last edited by davegri; 03-03-2018 at 12:47 PM. Reason: logic

  8. #8
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    136
    Cool, thanks davegri!

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,915
    If you don't want to have to touch every control, another method is to name controls with similar names, like tbx1, tbx2, tbx3, etc. Then looping code can address only those controls:

    For x = 1 to 10
    Me.Controls("tbx" & x) = Null
    Next

    How many controls are there? If only a few, maybe just addressing each one directly would be just as efficient.
    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.

  10. #10
    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
    That'd work, of course, but names like tbx1, tbx2, tbx3 isn't very descriptive, is it?

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

    All posts/responses based on Access 2003/2007

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,915
    Just an example. I have controls named like:

    tbxSetup1, tbxSetup2, tbxSetup3, etc.

    and

    tbxPost1, tbxPost2, tbxPost3, etc.

    etc.

    And yes, db violates normalization to some degree. Also, have forms to allow users to input numbers to do calcs and in some cases both raw data and calculated results are committed to record and in other situations only the calculated results are committed. Laboratory testing, results cannot be allowed to change if a procedure or formula is altered - which is extremely rare but not impossible.
    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.

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

Similar Threads

  1. Replies: 8
    Last Post: 10-22-2017, 12:54 PM
  2. Clearing multiracial text box when form opens
    By Jacqueline in forum Forms
    Replies: 14
    Last Post: 09-01-2015, 05:26 PM
  3. Replies: 5
    Last Post: 04-01-2013, 11:49 AM
  4. Replies: 4
    Last Post: 01-04-2013, 04:03 PM
  5. Clearing certain boxes on form for next record using VB
    By justinwright in forum Programming
    Replies: 3
    Last Post: 07-21-2010, 12:16 PM

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