Clear Form

klwu

Brainy!!
Local time
Today, 19:57
Joined
Sep 13, 2004
Messages
47
Hi!

I wrote the codes to reset the form, means that when the user click on 'Reset', all the fields will turn blank. But when I test the button, a message came out said "invalid use of null". Could someone please guide me on this? Thanks

Code:
Private Sub cmdClear_Click()
On Error GoTo Err_cmdClear_Click

cmbCompanyNo = Null
RefNo = Null
Name = Null
IP = Null
PortNo = Null
NetworkID = Null
Email = Null
AltEmail = Null

Exit_cmdClear_Click:
    Exit Sub

Err_cmdClear_Click:
    MsgBox Err.Description
    Resume Exit_cmdClear_Click
    
End Sub
 
Are you wanting to Undo a record or go to a new record?
 
This code is for a Search Form, those fields are not bound, sometimes user might type in wrong keywords and wanting to clear all the fields and re-enter again.
 
sonja_n00b said:
How about replacing NULL with ""?

The default value for a control - since their default data type is Variant - is Null.

Comment out the On Error Goto line and see which line the code actually stops at. Also, qualify your controls by putting an appropriate prefix so that they are not confused with any underlying fields in the recordset. And use Me. before each object.
 
SJ McAbney said:
The default value for a control - since their default data type is Variant - is Null.

Comment out the On Error Goto line and see which line the code actually stops at. Also, qualify your controls by putting an appropriate prefix so that they are not confused with any underlying fields in the recordset. And use Me. before each object.


I have a button that clears come fields. and me.field = "" is the only thing that works!
 
If you're clearing ALL your input fields on a form you can throw this in a Module

Code:
Sub ResetFormFields(strFormName As String)

    Dim ctl As Control
    
    For Each ctl In Forms(strFormName)
        Select Case ctl.ControlType
            Case Is = acTextBox
                ctl = ""
            Case Is = acCheckBox
                ctl = Null
            Case Is = acComboBox
                ctl = Null
            Case Is = acListBox
                ctl = Null
            Case Is = acOptionGroup
                ctl = Null
            Case Else
                'do nothing since you don't need to reset lines, labels, rectangles, etc...
        End Select
    Next ctl

End Sub
Then in your On Click event

Code:
Call ResetFormFields("YourFormName")

PS these aren't all the Control Types
 
A control's data type is Variant; their default value is Null.
Since a textbox is a control you should set that to Null and not vbNullString, Last4Supper.

Code:
Sub ResetFormFields(strFormName As String)

    Dim ctl As Control
    
    For Each ctl In Forms(strFormName)
        Select Case ctl.ControlType
            Case Is = acTextBox, acCheckBox, acComboBox, acListBox, acOptionGroup
                ctl = Null
            Case Else
                'do nothing since you don't need to reset lines, labels, rectangles, etc...
        End Select
    Next ctl

End Sub
 
SJ McAbney said:
A control's data type is Variant; their default value is Null.
Since a textbox is a control you should set that to Null and not vbNullString, Last4Supper.

Code:
Sub ResetFormFields(strFormName As String)

    Dim ctl As Control
    
    For Each ctl In Forms(strFormName)
        Select Case ctl.ControlType
            Case Is = acTextBox, acCheckBox, acComboBox, acListBox, acOptionGroup
                ctl = Null
            Case Else
                'do nothing since you don't need to reset lines, labels, rectangles, etc...
        End Select
    Next ctl

End Sub


Brilliant. I've been playing with something like this and couldn't get it to work right. This is great...Thanks SJ!!
 

Users who are viewing this thread

Back
Top Bottom