Help with resetting form - optionbuttons

RosemaryJB

Registered User.
Local time
Today, 15:39
Joined
Nov 6, 2006
Messages
24
My apologies if you have already seen this - I added it to another, related thread but as no-one answered thought it might be overlooked.

I reset all the fields on a search form - Do I = 0 to Count etc.

This works for fine all (textbox, combobox, listbox, toggle buttton) except optionbutton which VB won't let me, saying I can't set a value to this field.

I've tried setting the optionbuttons to 0, defaultvalue & false.

I have to list each optionbutton individually & set them to 0.

As this is a form which changes frequently I'd prefer to Do I = 0 to Count rather than hard-code the field:

Any suggestions please - see code below

Private Sub ClrFilter_Click()

'Clear filter entries from screen

Dim I As Integer
Dim Index As Integer

For I = 0 To Forms![Frm Contacts Search].Count - 1
If TypeOf Forms![Frm Contacts Search](I) Is TextBox Then
Forms![Frm Contacts Search](I) = ""
ElseIf TypeOf Forms![Frm Contacts Search](I) Is ComboBox Then
Forms![Frm Contacts Search](I) = ""
ElseIf TypeOf Forms![Frm Contacts Search](I) Is ListBox Then
For Index = 0 To Forms![Frm Contacts Search](I).ListCount - 1
Forms![Frm Contacts Search](I).Selected(Index) = False
Next Index
'next bit doesn't work
ElseIf TypeOf Forms![Frm Contacts Search](I) Is OptionButton Then
Forms![Frm Contacts Search](I) = 0
ElseIf TypeOf Forms![Frm Contacts Search](I) Is ToggleButton Then
Forms![Frm Contacts Search](I).Value = Forms![Frm Contacts Search](I).DefaultValue
End If
Next I

'Option buttons - this works

Me!Broxbourne = 0
Me!Dacorum = 0
'etc - unfortunately there are lots of these!

End Sub
Reply With Quote
 
how about something like

Code:
[FONT=monospace]Dim ctl As Control
For Each ctl In Me.Controls
    If (ctl.ControlType = acOptionbutton) Then
        ctl.Value = 0
    End If
Next ctl[/FONT]
 
Thank-you for your suggestion which I tried.

I still got the message 'you can't assign a value to this field'. However, I noticed the fields were in fact cleared so I inserted 'On Error Resume Next' before the statement which setting the optionbutton fields to False. The code now works perfectly.
BIZARRE!

I have now inserted this in the original code for consistency but many thanks - you gave me a clue.

I didn't know of 'control' as a type so I have learnt something new as well.
 

Users who are viewing this thread

Back
Top Bottom