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 Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programing a Function 1st time 1

Status
Not open for further replies.

timhans

Programmer
Jun 24, 2009
75
Hello, my first attempt at coding a function, I have this

Function ClearAll()

Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
ctl.Value = Null
End If
Case Else
End Select
Next ctl

End Function

Would like to dynamicly exclude controls from being cleared

Could hard code it like

Function ClearAll()
Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If ctl.ControlSource = "" And ctl.Name <> "CtlName" Then 'And ctl.Name <> "txtNewField1" And ctl.Name <> "txtNewField2" And ctl.Name <> "txtNewField3" Then
ctl.Value = Null
End If
Case Else
End Select
Next ctl
End Function

but again would like it to be dynamic I tried but no luck.
Thanks

 
Both functions you've posted have syntax error...
The real question is: what do you want to do ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Sorry about that, that was me not cleaning up thoughly after my atemps at coding my function

I wanted to create a Clear "function", which I did, then I want to generalize the function so I can exclude control's from being cleared but dynamically, here's the clean code

Function ClearAll()

Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If ctl.ControlSource = "" Then
ctl.Value = Null
End If
Case Else
End Select
Next ctl

End Function
 
timhans,
Important to remember about functions, don't name the "Module" the same as the "Function."
If your "Function" is Clearall() then the "Module," for example, should be named Allclear, or Clearall1.
jim
 
So basically you want to pass the types of controls NOT to clear to the function and have it ignore them?

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
I can exclude control's from being cleared
You may play with the Tag property of the controls.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
So basically you want to pass the types of controls NOT to clear to the function and have it ignore them? "

Yes, that's exactly what I am looking for
 
KHP, I will play with the Tag property. HarleyQuinn, thank you for your comment
 
Know I can do it as well, Thanks: PHV, 67peshawar294

Function ClearAll(c As String)
Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If ctl.ControlSource = "" And ctl.Tag <> c Then
ctl.Value = Null
End If
Case Else
End Select
Next ctl

End Function
 
Placing this in a module(AllClear) I finaly have my general clearing function.
For controls you do not want cleared place a c in the tag property
Call it like so

Call ClearAll("c", Me.Form)

Function ClearAll(c As String, f As Form)
Dim ctl As Control
For Each ctl In f.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If ctl.ControlSource = "" And ctl.Tag <> c Then
ctl.Value = Null
End If
End Select
Next ctl
End If
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top