looping through form to clear textboxes

G

Guest

I tried using the following two methods to code this and get error messages
as noted in comments.

Private Sub btnClearForm_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If (Me.ctl.ControlType = acTextBox) Then 'error method or data
member error cont. not found, highlights (.ctl)
If Me.ctl.DefaultValue <> "" Then 'if have default value
put it in textbox
Me.ctl.Value = Me.ctl.DefaultValue
Else
Me.ctl.Value = ""
End If
End If
Next ctl

'I also tried it this way

Dim i As Integer
For i = 0 To Me.Count - 1
If TypeOf Me(i) Is TextBox Then
Me(i).Value = "" ' error can't assign a value
End If
Next i

End Sub

I have been stuck on this for a while, help would be appreciated
 
D

Duane Hookom

You have to make sure your text box control source is not bound to an
expression like:
=[Qty] * [UnitPrice]
Also, I would use:
Me(i).Value = Null
 
R

RoyVidar

Arnold Klapheck said:
I tried using the following two methods to code this and get error
messages as noted in comments.

Private Sub btnClearForm_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If (Me.ctl.ControlType = acTextBox) Then 'error method
or data member error cont. not found, highlights (.ctl)
If Me.ctl.DefaultValue <> "" Then 'if have default
value put it in textbox
Me.ctl.Value = Me.ctl.DefaultValue
Else
Me.ctl.Value = ""
End If
End If
Next ctl

'I also tried it this way

Dim i As Integer
For i = 0 To Me.Count - 1
If TypeOf Me(i) Is TextBox Then
Me(i).Value = "" ' error can't assign a
value End If
Next i

End Sub

I have been stuck on this for a while, help would be appreciated

What is supposed to happen, is that in the for each next construct, the
variable ctl is supposed to be set to a control in the controls
collection (me.controls), so, just drop the "Me" reference in front of
the ctl variable.

Dim ctl As Control
For Each ctl In Me.Controls
If (ctl.ControlType = acTextBox) Then
If ctl.DefaultValue <> "" Then
ctl.Value = ctl.DefaultValue
Else
ctl.Value = ""
End If
End If
Next ctl
 
G

Guest

Duane Hookom said:
You have to make sure your text box control source is not bound to an
expression like:
=[Qty] * [UnitPrice]
Also, I would use:
Me(i).Value = Null
Yes that works, thank you what follows is the correct code

Private Sub btnClearForm_Click()
Dim i As Integer
For i = 0 To Me.Count - 1
If TypeOf Me(i) Is TextBox Then
If Me(i).DefaultValue <> "" Then
Me(i).Value = Me(i).DefaultValue
Else
Me(i).Value = Null
End If
End If
Next i
End Sub

when it puts in the default value however it also puts in the " around it,
"CA" instead of CA, any ideas on how to leave out the "?
 
G

Guest

If you are using AC2K or newer:

Me(i).Value = Replace(Me(i).DefaultValue, """", "")

Arnold Klapheck said:
Duane Hookom said:
You have to make sure your text box control source is not bound to an
expression like:
=[Qty] * [UnitPrice]
Also, I would use:
Me(i).Value = Null
Yes that works, thank you what follows is the correct code

Private Sub btnClearForm_Click()
Dim i As Integer
For i = 0 To Me.Count - 1
If TypeOf Me(i) Is TextBox Then
If Me(i).DefaultValue <> "" Then
Me(i).Value = Me(i).DefaultValue
Else
Me(i).Value = Null
End If
End If
Next i
End Sub

when it puts in the default value however it also puts in the " around it,
"CA" instead of CA, any ideas on how to leave out the "?
 
G

Guest

Yes that did it also, so know I know two ways to do it, thanks

Dim ctl As Control
For Each ctl In Me.Controls
If (ctl.ControlType = acTextBox) Then
If ctl.DefaultValue <> "" Then
ctl.Value = Replace(ctl.DefaultValue, """", "")
Else
ctl.Value = "" 'or NULL
End If
End If
Next ctl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top