locked
validating radio options RRS feed

  • Question

  • User1216627406 posted

    Greetings,

     

    Hopefully, this is the right forum for this question.


    We are using Access database as front end to connect to sql server db.

    We have a form and one of the controls is a radio button.

    There are 3 options with this control.

    We want to make sure that users can check one of the 3 radio buttons before proceeding.

    Everything I have tried so far has not worked.

    For instance I tried:

    if Me.radioButtonName.Value <> 1 or Me.radioButtonName.Value <> 2 OR Me.radioButtonName.Value <> 3

    Msg="must make a selection"

    end if

     

    No errors but it doesn't work.

    Any ideas?

    Thanks alot in advance

    Thursday, March 14, 2013 11:21 AM

Answers

  • User281315223 posted

    I updated my previous answer, you may also want to consider trying a case (switch) statement instead (especially if you are going to end up with any additional options or need some specific logic added) : 

    Select Case Me.radioButtonName.Value
        Case 1
            'Code here'
        Case 2
            'Code here'
        Case 3
            'Code here'
        Case Else
            'Your Message Here'
    End Select


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 14, 2013 12:13 PM

All replies

  • User281315223 posted

    Have you considered using a RadioButtonList Control, which will generate each of your options and you could use it to check if any of them had been checked?

    <form id="form1" runat="server">
          <!-- Your RadioButtonList with three options -->
          <asp:RadioButtonList ID="YourRadioButtonList" runat="server">
                <asp:ListItem Value="1">1</asp:ListItem>
                <asp:ListItem Value="2">2</asp:ListItem>
                <asp:ListItem Value="3">3</asp:ListItem>
          </asp:RadioButtonList>
          <!-- A Label to store any error messages -->
          <asp:Label ID="ErrorLabel" runat="server" />
          <!-- A Submit Button to Submit Your Form -->
          <input type="submit" value='Submit' />
    </form>

    along with the following server-side code : 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            'If a PostBack occured'
            If (IsPostBack) Then
                'Check if a value was selected'
                If (YourRadioButtonList.SelectedItem Is Nothing) Then
                    'If not output an error'
                    ErrorLabel.Text = "Please select a value!"
                Else
                    'Your Database call goes here'
                End If
            End If
    End Sub

    You'll need to make sure that you are also checking the SelectedItem property instead of the SelectedValue as well.

    Thursday, March 14, 2013 11:48 AM
  • User1216627406 posted

    Ron, that's very much for your prompt response.

    I know how to validate radiobutonList using .net but in this case, I am using Access Form as front end.

    So, your solution won't work for an Access Form.


    Sorry if I wasn't clear before.

    Thursday, March 14, 2013 11:52 AM
  • User281315223 posted

    Are you using an Option Group for all of these (so that only a single one can be selected at a time)? I'm trying to find the Documentation for Access Forms (as it has really been forever since I have even been around them).

    It's likely just a simple property that isn't being checked properly and probably should be something like Value or OptionValue, I'll keep looking : 

    'Attempts to check each of them'
    If(Me.radioButtonName.Value <> 1 OR Me.radioButtonName.Value <> 2 OR Me.radioButtonName.Value <> 3) Then
        'Set your message here'
    End If
    
    'Checks if there is a value at all'
    If(Not Me.radioButtonName.Value) Then
        'Set your message here'
    End If

    (Pardon any syntax errors as my experience in this area is rather limited)

    Thursday, March 14, 2013 11:59 AM
  • User1216627406 posted

    Thank you Rion.

     

    Yes, it should be an Option Group where only one single one can be seletected at a time.

    I will keep looking too as I haven't done this before.

    Thanks a lot

    Thursday, March 14, 2013 12:05 PM
  • User281315223 posted

    I updated my previous answer, you may also want to consider trying a case (switch) statement instead (especially if you are going to end up with any additional options or need some specific logic added) : 

    Select Case Me.radioButtonName.Value
        Case 1
            'Code here'
        Case 2
            'Code here'
        Case 3
            'Code here'
        Case Else
            'Your Message Here'
    End Select


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 14, 2013 12:13 PM
  • User1216627406 posted

    Rion,

    I know you said you haven't done this for a while but do I need to put the code in the BeforeUpdate procedure because I tried exactly the same code as the first part of your solution:

    If Me.RadioButtonname <> 1... and it wasn't doing anything.

    I haven't tried the select case yet. Same question applies here.

    Do I need to put in beforeUpdate procedure?

    Thursday, March 14, 2013 12:37 PM
  • User281315223 posted

    I can't really say with any kind of certainty. You'll likely need someone with a bit more experience using Access Forms that I have, but I'll be glad to try and help you figure it out.

    What does your BeforeUpdate procedure currently look like? According to this discussion it looks like that is typically where the validation is handled within Access Forms so that sounds like the correct place to put it.

    Thursday, March 14, 2013 12:46 PM
  • User1216627406 posted

    Testing now...

     I will be back in a sec.

    Thanks a million Rion.

    Thursday, March 14, 2013 12:47 PM
  • User281315223 posted

    No problem. I hope it works out.

    Thursday, March 14, 2013 12:57 PM