Option Group with Case statement

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is the code below correct?

I have two radio buttons within an option group (ImportOption). I have a
button, to which this code is executed when clicked.

I always receive the error message no matter which button is selected. On
debugging 'ImportOption' is empty.

Can anyone help me please?
Thanks.



Function importMethod_Click()

Select Case ImportOption
Case 1
MsgBox "Option 1."
Case 2
MsgBox "Option 2."
Case Else
MsgBox "Please select an option."
GoTo exit_importMethod_Click
End Select

exit_importMethod_Click:
Exit Function

End Function
 
Simon,

The problem is your reference to the option group is not correct. It should
be:

Select Case Me.ImportOption

The Me. keyword tells Access that ImportOption is a control in the object
(form, in this case) that the module belongs to, so it can reference it and
read its value. Ommitting the Me. keyword makes Access assume that
ImportOption is a local, implicitly declared variable, to which you have not
assigned a value; as a result, the variable returns Null, so the Case Else
branch is executed, and you get the error message.

HTH,
Nikos
 
Nikos,

I have tried to use the 'Me.' as suggested.
However, when I compile, I receive an error: "Invalid use of Me keyword"
 
It does work and I was being silly.

Nikos Yannacopoulos said:
Simon,

The problem is your reference to the option group is not correct. It should
be:

Select Case Me.ImportOption

The Me. keyword tells Access that ImportOption is a control in the object
(form, in this case) that the module belongs to, so it can reference it and
read its value. Ommitting the Me. keyword makes Access assume that
ImportOption is a local, implicitly declared variable, to which you have not
assigned a value; as a result, the variable returns Null, so the Case Else
branch is executed, and you get the error message.

HTH,
Nikos
 
Is this code in the form's module, or in a separate module?

If it is in a separate module, you will need to pass the function a
reference to the control ...

Function importMethod_Click(ImportOption As Control)

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
Back
Top