Results 1 to 4 of 4

Thread: clean controls from form with error

  1. #1

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    clean controls from form with error

    Hi

    I'm trying to clear the form using the code below

    Code:
     
    Function ClearAll(frm As Form)
    Dim ctl As Control
    
    For Each ctl In frm.Controls
       Select Case TypeName(ctl) 'ctl.ControlType
          Case "TextBox"
               ctl.Value = ""
          Case "OptionGroup", "ComboBox", "ListBox"
              ctl.Value = Null
          Case "Checkbox"
             ctl.Value = False
          'Case ac
       End Select
    Next
    End Function
    But return error Runtime error 438

    Name:  erro-vb.jpg
Views: 313
Size:  19.3 KB
    error occurs because my combobox is Style = Dropdown List

  2. #2
    Fanatic Member
    Join Date
    Nov 2011
    Posts
    804

    Re: clean controls from form with error

    Quote Originally Posted by mutley View Post
    Hi

    I'm trying to clear the form using the code below

    Code:
     
    Function ClearAll(frm As Form)
    Dim ctl As Control
    
    For Each ctl In frm.Controls
       Select Case TypeName(ctl) 'ctl.ControlType
          Case "TextBox"
               ctl.Value = ""
          Case "OptionGroup", "ComboBox", "ListBox"
              ctl.Value = Null
          Case "Checkbox"
             ctl.Value = False
          'Case ac
       End Select
    Next
    End Function
    But return error Runtime error 438

    Name:  erro-vb.jpg
Views: 313
Size:  19.3 KB
    error occurs because my combobox is Style = Dropdown List
    Put "ComboBox" as a seperate case.
    then make sure the combobox has a blank item and either set that as "" or set listindex = 0

  3. #3
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: clean controls from form with error

    just clear that error..
    you do want all Contols blank

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Call clearForm(Me)
    End Sub
    
    Public Sub clearForm(ByRef myForm As Object)
        On Error GoTo EH
        Dim objCtrl As Control
    
        For Each objCtrl In myForm.Controls
                
                If TypeOf objCtrl Is TextBox Then
                     objCtrl = ""
                End If
    
               If TypeOf objCtrl Is CheckBox Then
                     objCtrl = 0
                End If
             
             If TypeOf objCtrl Is ComboBox Then
                     objCtrl = ""
                End If
        Next
    EH:
        If Err.Number = 438 Then
            Resume Next
        End If
    End Sub
    
    
    Private Sub Form_Load()
    Combo1.AddItem "Test"
    End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: clean controls from form with error

    To clear a combo with set to drop down list set the listindex to -1

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
  •  



Click Here to Expand Forum to Full Width