Clear form button and the code to make it work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joshapalooza
    New Member
    • Feb 2008
    • 11

    Clear form button and the code to make it work

    I am using a form to sort a report, but I can't seem to get the "clear form" button to work. I used the wizard when I installed the button, using the clear form option under the form option list. What I want to be able to do is clear the information on the drop down combo box in case I want to change the sort order. This is what the code looks like now:

    Option Compare Database

    Code:
    Private Sub close_Click()
    On Error GoTo Err_close_Click
    
    
        DoCmd.close
    
    Exit_close_Click:
        Exit Sub
    
    Err_close_Click:
        MsgBox Err.Description
        Resume Exit_close_Click
        
    End Sub
    
    Private Sub cmdSetSort_Click()
    
        Dim strSQL As String, intCounter As Integer
        'Build strSQL String
        For intCounter = 1 To 4
            If Me("cboSort" & intCounter) <> "" Then
            strSQL = strSQL & "[" & Me("cboSort" & intCounter) & "]"
            If Me("Chk" & intCounter) = True Then
                strSQL = strSQL & " DESC"
            End If
            strSQL = strSQL & ", "
        End If
        Next
    
        If strSQL <> "" Then
            'Strip Last Comma & Space
            strSQL = Left(strSQL, (Len(strSQL) - 2))
            'Set the OrderBy property
            Reports![rptPersonnel].OrderBy = strSQL
            Reports![rptPersonnel].OrderByOn = True
        Else
            Reports![rptPersonnel].OrderByOn = False
            
        End If
    
    End Sub
    
    
    Private Sub Form_Open(Cancel As Integer)
    ' Open the report maximized, in Print Preview
        DoCmd.OpenReport "rptPersonnel", acViewPreview
        DoCmd.Maximize
    End Sub
    
    Private Sub Refresh_Click()
    On Error GoTo Err_Refresh_Click
    
    
        DoCmd.DoMenuItem acFormBar, acRecordsMenu, 5, , acMenuVer70
    
    Exit_Refresh_Click:
        Exit Sub
    
    Err_Refresh_Click:
        MsgBox Err.Description
        Resume Exit_Refresh_Click
        
    End Sub
    Not sure what I'm doing wrong, but I would appreciate some help. Also attached is a picture of what my sort box looks like.
    Attached Files
  • joshapalooza
    New Member
    • Feb 2008
    • 11

    #2
    Originally posted by joshapalooza
    I am using a form to sort a report, but I can't seem to get the "clear form" button to work. I used the wizard when I installed the button, using the clear form option under the form option list. What I want to be able to do is clear the information on the drop down combo box in case I want to change the sort order. This is what the code looks like now:

    Option Compare Database

    Code:
    Private Sub close_Click()
    On Error GoTo Err_close_Click
    
    
        DoCmd.close
    
    Exit_close_Click:
        Exit Sub
    
    Err_close_Click:
        MsgBox Err.Description
        Resume Exit_close_Click
        
    End Sub
    
    Private Sub cmdSetSort_Click()
    
        Dim strSQL As String, intCounter As Integer
        'Build strSQL String
        For intCounter = 1 To 4
            If Me("cboSort" & intCounter) <> "" Then
            strSQL = strSQL & "[" & Me("cboSort" & intCounter) & "]"
            If Me("Chk" & intCounter) = True Then
                strSQL = strSQL & " DESC"
            End If
            strSQL = strSQL & ", "
        End If
        Next
    
        If strSQL <> "" Then
            'Strip Last Comma & Space
            strSQL = Left(strSQL, (Len(strSQL) - 2))
            'Set the OrderBy property
            Reports![rptPersonnel].OrderBy = strSQL
            Reports![rptPersonnel].OrderByOn = True
        Else
            Reports![rptPersonnel].OrderByOn = False
            
        End If
    
    End Sub
    
    
    Private Sub Form_Open(Cancel As Integer)
    ' Open the report maximized, in Print Preview
        DoCmd.OpenReport "rptPersonnel", acViewPreview
        DoCmd.Maximize
    End Sub
    
    Private Sub Refresh_Click()
    On Error GoTo Err_Refresh_Click
    
    
        DoCmd.DoMenuItem acFormBar, acRecordsMenu, 5, , acMenuVer70
    
    Exit_Refresh_Click:
        Exit Sub
    
    Err_Refresh_Click:
        MsgBox Err.Description
        Resume Exit_Refresh_Click
        
    End Sub
    Not sure what I'm doing wrong, but I would appreciate some help. Also attached is a picture of what my sort box looks like.
    Never mind, I figured out the answer by myself. For those that are interested about the answer though, here is what I figured out:

    Code:
    Private Sub cmdClear_Click()
    On Error Resume Next
    Me.cboSort1 = ""
    Me.cbosort2 = ""
    Me.cboSort3 = ""
    Me.cboSort4 = ""
    cboSort1.SetFocus
    End Sub
    Hope this helps those of us out there that are still trying to puzzle their way through VBA and Access.

    Comment

    Working...