Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with Select Case

Status
Not open for further replies.

Ausburgh

Programmer
Jul 21, 2004
62
US
Here I go again bugging you nice people with my problems. In any case, on a form I have:

a.) 3 frames - user Info, Travel, and Training (with multiple labels and text fields on each).

b.) EntryType comboBox (with Travel, Training, and Both)

My goal is to allow the user to enter information based on what they choose on on the cboEntryType. Example, if user picks Travel on the cboEntryType then Travel frame will be enable and Training frame will be disabled -- Travel & Training frames are disabled by default.

But my code does not work. With a new record, I have to pick the EntryType and save it first and then navigate back and forth before it enables the correct frame.

-------------------
Private Sub cboEntryType_Change()
Dim a, b, c, d As String
Dim EntryType As String
a = "Travel"
b = "Training"
c = "Travel & Training"
d = ""

EntryType = cboEntryType.Text

Select Case EntryType
Case a
Account.Text = "21xx - Travel"
Frame2.Enabled = True
'Frame3.Enabled = False
Case b
Account.Text = ""
Frame2.Enabled = False
Frame3.Enabled = True
Case c
Account.Text = "21xx - Travel"
Frame2.Enabled = True
Frame3.Enabled = True
Case d
Account.Text = ""
Frame2.Enabled = False
Frame3.Enabled = False
End Select
End Sub

-----------

What I'm doing wrong? I've also tried using If... then...Else; If...Elseif statements and I'm having the same problems. I even put the above code in the Private Sub Form_Load() but I got the same result.

Thanks in advance
 
How about:
Private Sub cboEntryType_Change()
Dim a, b, c, d As String
Dim EntryType As String
a = "Travel"
b = "Training"
c = "Travel & Training"
d = ""

EntryType = cboEntryType.Text

Select Case EntryType
Case "a"
Account.Text = "21xx - Travel"
Frame2.Enabled = True
'Frame3.Enabled = False
Case "b"
Account.Text = ""
Frame2.Enabled = False
Frame3.Enabled = True
Case "c"
Account.Text = "21xx - Travel"
Frame2.Enabled = True
Frame3.Enabled = True
Case "d"
Account.Text = ""
Frame2.Enabled = False
Frame3.Enabled = False
End Select
End Sub

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Try using cboEntryType_Click() instead of cboEntryType_Change().
Also use cboTest.ListIndex to determine what the user selected: -1 is nothing selected, 0 is the first item, 2 is the second and so on.
 
I don't beleive it's your Case statement that is at fault, as I was able to test that each case gets chosen correctly from a combobox, it must be your enabling/disabling Frames that is incorrect.

A,
 
In the first example Case a will be looking for the string a which is "Travel"
and in the second example Case "a" will be looking for "a".
 
Oops...my bad...didn't see where a, b, c, and d were set as variables. Of course with names like a, b, c, and d I'm not suprised that I did not realize they were variables.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
You do realize -
when you declare:

Dim a, b, c, d As String

ONLY d is a String - a, b, and c are VARIANTs

Just to let you know....

---- Andy

 
Thanks guys for responding.

ettienne,

I Updated as follows:

------------------
Private Sub Form_Load()

cboEntryType.AddItem ""
cboEntryType.ListIndex = -1
cboEntryType.AddItem "Travel"
cboEntryType.ListIndex = 0
cboEntryType.AddItem "Training"
cboEntryType.ListIndex = 1
cboEntryType.AddItem "Travel & Training"
cboEntryType.ListIndex = 2
End
-------------------
and
-----------------------------
Private Sub cboEntryType_Click()
Dim A as String
Dim b as String
Dim c as String
Dim d as String
Dim EntryType As String

a = "Travel"
b = "Training"
c = "Travel & Training"
d = ""

EntryType = cboEntryType.Text

Select Case EntryType
Case a
Account.Text = "21xx - Travel"
Frame2.Enabled = True
'Frame3.Enabled = False
Case b
Account.Text = ""
Frame2.Enabled = False
Frame3.Enabled = True
Case c
Account.Text = "21xx - Travel"
Frame2.Enabled = True
Frame3.Enabled = True
Case d
Account.Text = ""
Frame2.Enabled = False
Frame3.Enabled = False
End Select
End Sub
------------------------------

Now all the frames are disabled. I'm sure I messed it up somewhere............
 
I guess cboEntryType is in Frame1 so include;

Frame1.enabled = true

into Sub Form_Load or set that in design time properties for Frame1

Hugh,

 
Yes, frame1 is still enabled but the other frames are disabled.


 

Your End in Form_Load should've been an End Sub was that a typo when you posted; however

The following code appears to run ok when pasted into a new Form1 on which are placed Frame1 containing cboEntryType, Frame2, and Frame3.

Private Sub Form_Load()

cboEntryType.AddItem ""
cboEntryType.ListIndex = -1
cboEntryType.AddItem "Travel"
cboEntryType.ListIndex = 0
cboEntryType.AddItem "Training"
cboEntryType.ListIndex = 1
cboEntryType.AddItem "Travel & Training"

'the lines above including cboEntryType.ListIndex are redundant; rem em!
cboEntryType.ListIndex = 2

End Sub

Private Sub cboEntryType_Click()
Dim A As String
Dim b As String
Dim c As String
Dim d As String
Dim EntryType As String

A = "Travel"
b = "Training"
c = "Travel & Training"
d = ""

EntryType = cboEntryType.Text

Select Case EntryType
Case A
'Account.Text = "21xx - Travel"
Frame2.Enabled = True
'Frame3.Enabled = False
Case b
'Account.Text = ""
Frame2.Enabled = False
Frame3.Enabled = True
Case c
'Account.Text = "21xx - Travel"
Frame2.Enabled = True
Frame3.Enabled = True
Case d
'Account.Text = ""
Frame2.Enabled = False
Frame3.Enabled = False
End Select
End Sub

Lines containing Account.Text remmed out for the purposes of my test.

Hugh

 

Now it's working partially.

If I pick Travel nothing happens until I save it and then navigate to next or previous record and then I see that action on whatever record regardless of what's in cboEntryType for that record.

Do I need to refresh once cboEntryType has been populated? How do you refresh a form anyway without saving it.

Thanks
 
The "Click" event is raised only by the mouse. Specifically navigating to another record does not raise the combo box's click event.

You should also note that 'Frame3.Enabled = False in the "Case A" code is commented out so nothing is happening to Frame3 when you select "Travel".

You refresh a form with Me.Refresh
 
Most of the controls have a .Refresh method. The form has a .Refresh method as well.

Code:
  'For the form
  Form1.Refresh

  'For the control
  cboEntryType.Refresh

HyperEngineer
If it ain't broke, it probably needs improvement.
 
When you retrieve the record you will of course have to set cboEntryType.ListIndex to reflect the setting previously saved in the record.

When you save the record you will have to set its EntryType field to reflect that in the combo.

The EntryType field in the data record may be a string containing "Travel" etc. in which case you will write it and read it to the combo's text property or an integer reflecting the combo's ListIndex property.

Hugh,

 
<<The "Click" event is raised only by the mouse>>

Not true. It is also raised when selecting an item in the drop down and pressing Return, and from within code when the ListIndex property is changed.

Hugh
 
correction:

It is also raised when selecting a new item in the drop down, and from within code when the ListIndex property is changed.
Basically anytime ListIndex is changed.

Hugh,
 
Yes Hugh's right, don't forget that event raised through code, that can bite you if your not careful.

A,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top