Select Case Picking up number

  • Thread starter Thread starter Dean Knox
  • Start date Start date
D

Dean Knox

I have this code

Private Sub cmb1_Change()

Dim sVar As String
sVar = cmb1.Text

With cmb1
Select Case Left(.Text, 1)
Case D:
txtBAID.Text = sVar
Case 7:
txtPLID.Text = sVar
Case 2:
txtSAVID.Text = sVar
Case A:
txtMORID.Text = sVar
Case Else:
txtCCID.Text = sVar
End Select
End With

End Sub

If I select one of these variables:

D12345
712345
212345
A12345
112345

The ones beginning with 7, 2, and 1 work. But the D and A
aren't picked up by the select case and just go to case
else.

Can anyone explain why, and how I get around this.

Thanks

Dean
 
to use string values in your select case, you need to
enclose the value in quotes e.g. Case "D", otherwise VBA
will look for a variable D, rather than the value.

Steve
 
Hi Dean,

You need to add quotes to D otherwise it expects D to be a variable.
So modify code to
Case "D"

Dean said:
I have this code

Private Sub cmb1_Change()

Dim sVar As String
sVar = cmb1.Text

With cmb1
Select Case Left(.Text, 1)
Case D:
txtBAID.Text = sVar
Case 7:
txtPLID.Text = sVar
Case 2:
txtSAVID.Text = sVar
Case A:
txtMORID.Text = sVar
Case Else:
txtCCID.Text = sVar
End Select
End With

End Sub

If I select one of these variables:

D12345
712345
212345
A12345
112345

The ones beginning with 7, 2, and 1 work. But the D and A
aren't picked up by the select case and just go to case
else.

Can anyone explain why, and how I get around this.

Thanks

Dean

--

Cheers
Andy

http://www.andypope.info
 
Back
Top