Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2005
    Posts
    220

    Unanswered: How do I un-select an OptionButton?

    I have a form with a few option buttons. I also have a button the user can press to clear all the fields on the form, some of which are text boxes. I can clear everything but the OptionButton. I keep getting an error: You can't assign a value to this object

    This is the line it complains about "ctl.Value = False"

    Code:
    Private Sub ClearCat1_Form()
    
    Dim ctl As Control
     
    For Each ctl In Me.Controls
        Select Case ctl.ControlType
            Case acTextBox, acComboBox, acListBox, acCheckBox
                If ctl.ControlSource = "" And Val(ctl.Tag) = 1 Then
                    ctl.Value = Null
                End If
            Case acOptionButton
                If Val(ctl.Tag) = 1 Then
                    ctl.Value = False
                End If
            Case Else
        End Select
    Next ctl

  2. #2
    Join Date
    Apr 2006
    Posts
    157
    try using ctl.value = 0 to clear the optionbutton
    ctl.value = -1 to select it.
    it actually reads -1 for true and 0 for false
    Only quitters quit!

  3. #3
    Join Date
    Nov 2002
    Posts
    154

    Smile Use Null

    I set radio button option groups to null all the time and it works fine. I even use the double click event of the group to set optWhatever = Null. This clears any selection from the entire group so that all of the radio buttons look blank. I hope that this helps, because usually radio buttons have values like 0, 1 ,2 ,3 and not TRUE or FALSE like check boxes. If you wanted a default option chosen you would just set the value of the option group to the value of the radio button. In any case, you set the value to the option group and not to the individual radio buttons. You can find these values by clicking on the appropriate radio button in design view and looking at the Data tab of the Properties dialog box for 'Option Value'. Good luck to you!

  4. #4
    Join Date
    Apr 2006
    Posts
    157
    The same works for checkboxes
    Only quitters quit!

  5. #5
    Join Date
    Sep 2005
    Posts
    220
    I tried setting it to 0 and to Null. Neither worked.

    Code:
    For Each ctl In Me.Controls
        Select Case ctl.ControlType
            Case acTextBox, acComboBox, acListBox, acCheckBox
                If ctl.ControlSource = "" And Val(ctl.Tag) = 1 Then
                    ctl.Value = Null
                End If
            Case acOptionButton
                If Val(ctl.Tag) = 1 Then
                    ctl.Value = 0
                End If
            Case Else
        End Select
    Next ctl

  6. #6
    Join Date
    Apr 2006
    Posts
    157
    try placing this in your if then instead,

    Code:
    case acoptionbutton
    
    if ctl.value = -1 then
          ctl.value = 0
    end if
    Only quitters quit!

  7. #7
    Join Date
    Sep 2005
    Posts
    220
    No luck. I still get an error:

    "You entered an expression that has no value"

  8. #8
    Join Date
    Apr 2004
    Location
    outside the rim
    Posts
    1,011
    Is your OptionButton part of an OptionGroup?

    If so, the value of the OptionGroup determines which OptionButton is active and you'll want to set the OptionGroup = Null.

    tc

  9. #9
    Join Date
    Sep 2005
    Posts
    220
    It's a single option button.

  10. #10
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    assuming you have handled .enabled and .locked

    if you want to allow an undefined state for the option, try:
    me.myoptionbutton.triplestate = true
    me.myoptionbutton = null
    (.triplestate can also be set in design view: allows true/false and null)

    if you only want to force unselected, try
    me.myoptionbutton = false
    it should do the trick UNLESS the option is bound to an uneditable (recordsource from a) query. to check, open the source query and attempt to edit (or watch the status line as the thing loads).

    izy
    currently using SS 2008R2

  11. #11
    Join Date
    Apr 2006
    Posts
    157
    try using the typeof keyword to identify the control that you are pertaining to
    Only quitters quit!

  12. #12
    Join Date
    Dec 2003
    Location
    Dallas, TX
    Posts
    1,004

    Talking

    Hi ssmith001,

    As usual I took the simplistic way around this. I just created an OptionGroup and named it Department, unbound and has Two options. Then I created a CommandButton (without the wizard) and just did this:

    Code:
    Private Sub ClearOptions_Click()
    
        If Me.Department <> "" Then
        Me.Department = 0
        End If
        
    End Sub
    Put that somewhere in your code along with the others. Didn't need to Declare it as a Control (Ctl). Try it and see if you like.

    have a nice one,
    BUD

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •