Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clear a from

Status
Not open for further replies.

DebbieCoates

Programmer
Oct 2, 2007
23
0
0
GB
i have various forms in my database that as I select a project, will fill all the field on the form.

Rather than writing

FieldA= ""
FieldB= ""
FieldC= ""
FieldD= ""

etc etc

everytime I have to clear the form, I thought I would write a function, where I passed in the form name, and then clear the fields, sort of like this

Public Sub ClearForm(strFormName As String)
Select Case "strFormName"

Case "frm_Main":
frm_Main.fieldA= ""
frm_Main.fieldB= ""
frm_Main.FfieldC= ""

Case "AnotherForm":
AnotherForm.fieldA= ""
AnotherForm.fieldA= ""
AnotherForm.fieldA= ""
End Select

End Sub

and then from the calling form

Call ClearForm(me.name)

for some reason the me.name, although is the name of the form, doesn't pass it to the clearform sub because it doesn't recognise it in the select case statement

can anyone give me a clue as to why it is doing this

Many Thanks
 
I've had a look on the net, and found a sub that someone had written that does what I want

Shuld anyone else want it, it is


Public Sub MyResetControls(frmMyForm As Form)
On Error GoTo Err
Dim Control As Object
For Each Control In frmMyForm.Controls
Control.Text = ""
Control.Value = ""
Next
Exit Sub
Err:
If Err.Number = 438 Then
Resume Next
ElseIf Err.Number = 13 Then
Control.Value = False
Resume Next
ElseIf Err.Number = 383 Then
Control.Clear
Resume Next
Else
Resume Next
End If

End Sub
 
>because it doesn't recognise it in the select case

To begin with, remove the quotes around "strFormName"

Select Case strFormName 'no quotes

Also, ould be a case sensitive comparison which were were doing, so becareful when doing this and possibly use:

strFormName=UCase$(strFormName)
Select Case strFormName

Case "FRM_MAIN" 'use also upper case


Lastly, you don't need the semi-colons at the end of each "Case" line


 
Similar to what is posted, but using select case instead of error trapping.
Code:
Sub Clear_Form(frm As Form)
Dim ctl         As Control

With ctl
    For Each ctl In frm.Controls
    
'     Debug.Print TypeName(ctl)
         Select Case TypeName(ctl)
         
             Case "TextBox"
                 ctl = ""
                 
             Case "Label"
             
             Case "OptionButton"
                g_blnCancelThis = True
                ctl.Value = 0
                g_blnCancelThis = False
                
             Case "CheckBox"
                 ctl = 0
                 
             Case "ComboBox"
             
                 'this will be used to cancel the click events of some of the combo
                 'boxes.  If we don't cancel the action errors will result
'                 g_blnCancelThis = True
                 ctl.ListIndex = -1
                 
                 'resets the variable to false. used throughout the app
'                 g_blnCancelThis = False
                 
             Case "DTPicker"
                 ctl.Value = Date
                 
    
         End Select
        
    
    Next
End With
            
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top