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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA problem with Select Case

Status
Not open for further replies.

dprayner

Programmer
Oct 14, 2002
140
US
Good morning people.

I am having some trouble with my Select Case structures.

I have a half a dozen Select Case structures in my project and the ListIndex keeps changing auotmatically.

When the user (me) clicks on a comboBox it run the corresponding code to the ListIndex for example 1.

Then it loops up to the top and the ListIndex becomes -1 and/or the program switches to anothe Select Case structure and the ListIndex is -1.

When the ListIndex becomes -1 it executes the Else code in each Select case structure.

Below is an example of my syntax.

Private Sub cmbgetinfo_Change()
Select Case cmbgetinfo.ListIndex
Case 1 'Report
Case 2 'Print
Case 3 'Do something
End Select

I was wondering if I should be using some other sub procedure besides _Change().

Has anyone run into this situation? DAVE
 


Hi,

"...and/or the program switches to anothe Select Case structure ..."

Then, you did not post all the relevent code.

What are you using this control structure to accomplish?

Skip,

[glasses] [red]Be Advised![/red]
The band of elderly oriental musicians, known as Ground Cover, is, in reality...
Asian Jasmine![tongue]
 
Hi Skip. This is the actual code:

Private Sub cmbFrameSRA_Change()
MsgBox "Case= " & cmbFrameSRA.ListIndex
cmbFrameMorph.ListIndex = Null
cmbTransFromCashWalker.ListIndex = Null
Select Case cmbFrameSRA.ListIndex
Case 13 'Special Risk America
FrameFromCashWalker.Visible = False
FrameMorph.Visible = False
cmbFrameSRA.Text = "Special Risk America CL101 XFR"
FrameSRA.Visible = True
FrameSRA.Top = 15
FrameSRA.Left = 6
FrameSRA.Width = 582
FrameSRA.Height = 95
Case 14 'Special Risk NY
FrameFromCashWalker.Visible = False
FrameMorph.Visible = False
cmbFrameSRA.Text = "Special Risk America NY"
FrameSRA.Visible = True
FrameSRA.Top = 15
FrameSRA.Left = 6
FrameSRA.Width = 582
FrameSRA.Height = 95
Case Else
cmbFrameMorph.Text = cmbFrameSRA.Text
FrameMorph.Visible = True
FrameFromCashWalker.Visible = False
FrameMorph.Top = 50
FrameMorph.Left = 6
FrameMorph.Width = 582
FrameMorph.Height = 60
End Select
End Sub

Private Sub cmbFrameMorph_Change()
MsgBox "FrameMorph= " & cmbFrameMorph.ListIndex
cmbTransFromCashWalker.ListIndex = Null
cmbFrameSRA.ListIndex = Null
Select Case cmbFrameMorph.ListIndex
Case 18 'Merlin
cmbFrameMorph.Text = "Merlin"
lblFrameMorph_Name.Caption = "Name:"
lblFrameMorph_Name.Enabled = False
txtFrameMorph_Name.Enabled = False
lblFrameMorph_Member.Enabled = True
txtFrameMorph_Member.Enabled = True
lblFrameMorph_DueDate.Enabled = True
txtFrameMorph_DueDate.Enabled = True
FrameMorph.Top = 50
FrameMorph.Left = 6
FrameMorph.Width = 582
FrameMorph.Height = 60
Case 20 'Merlin (General Suspense)
lblFrameMorph_Name.Enabled = False
txtFrameMorph_Name.Enabled = False
lblFrameMorph_DueDate.Enabled = False
txtFrameMorph_DueDate.Enabled = False
cmbFrameMorph.Text = "Merlin (General Suspense)"
FrameMorph.Visible = True
FrameMorph.Top = 50
FrameMorph.Left = 6
FrameMorph.Width = 582
FrameMorph.Height = 60
Case Else
cmbFrameMorph.Text = cmbFrameMorph.Value
FrameMorph.Visible = True
FrameMorph.Top = 50
FrameMorph.Left = 6
FrameMorph.Width = 582
FrameMorph.Height = 60
End Select
End Sub

When I run the form the FrameMorph is the default. Then when the user selects Case 13 'Special Risk America or Case 14 'Special Risk NY, the ListIndex for FrameSRA becomes -1. I guess it's not looping after all, but I'm not sure why the ListIndex is -1. DAVE
 


That has noting to do with the Select Case statement.

Its your ListBox Selection that's the issue.

Is there a MULTI selection being made?

Skip,

[glasses] [red]Be Advised![/red]
The band of elderly oriental musicians, known as Ground Cover, is, in reality...
Asian Jasmine![tongue]
 
Hi Skip.

What do you mean by MULTI selection? I am using a VB Form in MS Word 2003. DAVE
 


There is a MultiSelect property in VB Forms for Listbox.

Skip,

[glasses] [red]Be Advised![/red]
The band of elderly oriental musicians, known as Ground Cover, is, in reality...
Asian Jasmine![tongue]
 
Hi Skip. I am using a combobox not a listbox. DAVE
 


oops....

Are you sure that the 13th and 14th entry are the VALUES that you are looking for?

Why aren't you using the VALUE property instead of the ListIndex property?

Skip,

[glasses] [red]Be Advised![/red]
The band of elderly oriental musicians, known as Ground Cover, is, in reality...
Asian Jasmine![tongue]
 
Hi Skip. Do you think that it would be wiser to use the .Value instead of ListIndex? I can't remeber why I switched from Value to ListIndex. For example .Value "Merlin" instead of .ListIndex 18 DAVE
 


In this case, yes.

Skip,

[glasses] [red]Be Advised![/red]
The band of elderly oriental musicians, known as Ground Cover, is, in reality...
Asian Jasmine![tongue]
 
Hi Skip. Now that I think of it, I think I remember having trouble with whitespaces. That is why I went to ListIndex. DAVE
 


Can you explain?

Skip,

[glasses] [red]Be Advised![/red]
The band of elderly oriental musicians, known as Ground Cover, is, in reality...
Asian Jasmine![tongue]
 
I guess it was alway defaulting to Case Else because a White space character (ie. "Whitespace" vs "Whitespace ") was messing up the Select where they didn't equal. DAVE
 

I'd use the UCase function
Code:
Select case UCase(MyValue)

end Select
Same for If..Then -- any kind of a string test.


Skip,

[glasses] [red]Be Advised![/red]
The band of elderly oriental musicians, known as Ground Cover, is, in reality...
Asian Jasmine![tongue]
 
Have a look at the Trim function.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 


Where was my head? Of course, PHV, the Trim function! [blush]

Skip,

[glasses] [red]Be Advised![/red]
The band of elderly oriental musicians, known as Ground Cover, is, in reality...
Asian Jasmine![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top