Option Box and Default Value not changing

  • Thread starter Thread starter Peder Aas
  • Start date Start date
P

Peder Aas

I have this code for updating an Option Box' values:

Dim M as Integer
M = Month(Now())

If M = 1 Or 2 Or 3 Or 4 Then
OptionBoxName.DefaultValue = 1
ElseIf M = 5 Or 6 Or 7 Or 8 Then
OptionBoxName.DefaultValue = 2
Else
OptionBoxName.DefaultValue = 3
End If

I have put the code in the Forms Form_Open part.
But I have problems. The Option Box do not update accordingly when I open
the form. It always show DefaultValue = 1. Any suggestions? What I want to
do is to let the month decide which default option value the Option Box
shows. The OptionBoxName.DefaultValue = 1 works OK when I check it in the
Immediate Window.

Thanks
Peder Aas
 
Peder,

I think the main problem is in the syntax of your code.
If M = 1 Or 2 Or 3 Or 4 Then
.... will always evaluate to True, so the code never proceeds past this
point. I know what your intended meaning is, but Access doesn't! Try
one of these approaches...
-----------
If M = 1 Or M = 2 Or M = 3 Or M = 4 Then
Me.OptionBoxName.DefaultValue = 1
ElseIf M = 5 Or M = 6 Or M = 7 Or M = 8 Then
Me.OptionBoxName.DefaultValue = 2
Else
Me.OptionBoxName.DefaultValue = 3
End If
------------
If M < 5 Then
Me.OptionBoxName.DefaultValue = 1
ElseIf M < 9 Then
Me.OptionBoxName.DefaultValue = 2
Else
Me.OptionBoxName.DefaultValue = 3
End If
-----------
Select Case M
Case 1, 2, 3, 4
Me.OptionBoxName.DefaultValue = 1
Case 5, 6, 7, 8
Me.OptionBoxName.DefaultValue = 2
Case Else
Me.OptionBoxName.DefaultValue = 3
End Select
 
As well, I believe that the DefaultValue needs to be specfiied as a string,
even for numeric values:

Select Case M
Case 1, 2, 3, 4
Me.OptionBoxName.DefaultValue = "1"
Case 5, 6, 7, 8
Me.OptionBoxName.DefaultValue = "2"
Case Else
Me.OptionBoxName.DefaultValue = "3"
End Select

If you were trying to default to a text value, you need to include quotes
around the value:

Select Case M
Case 1, 2, 3, 4
Me.OptionBoxName.DefaultValue = """A"""
Case 5, 6, 7, 8
Me.OptionBoxName.DefaultValue = """B"""
Case Else
Me.OptionBoxName.DefaultValue = """C"""
End Select
 
Doug,

I have never done it like this, and I wasn't aware that you should. I
have just tried out your method, and it seems to work :-) But it also
works fine with...
Me.OptionBoxName.DefaultValue = 1
 
Access must coerce to a string. Maybe it's only a issue with text values,
where you must include the quotes as I showed.
 
Back
Top