By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,940 Members | 1,300 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,940 IT Pros & Developers. It's quick & easy.

Select Case Assistance Requested

P: 4
Please see my pseudocode below. I'm attempting to read a text file and find specific text in each string of text that is read. When a string of text is read that has the specific text I'm looking for the Case Statement will not evaluate to True. Is what I am doing possible with the Case Statement? Any Help will be appreciated.

dim strL as String

While not tso.eof

strL = tso.readline

Select Case strL

Case Left(strL, 8) = "someText"
do someting
Case Left(strL, 13) = "someOtherText"
do something

End Select

Apr 6 '10 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 1,134
Case is meant for deciding an action based on the value of a single expreression
Expand|Select|Wrap|Line Numbers
  1. select case Left(strL, 8)
  2.    case "someText"
  3.       dosomething
  4.    case "someOthe"
  5.       dosomethingelse
  6. End select
Since you need to select different length strings then that won't work

You will need to use if elseif which allows different expressions for each test
Expand|Select|Wrap|Line Numbers
  1. If Left(strL, 8) = "someText" Then
  2.     MsgBox "someText"
  3. ElseIf Left(strL, 13) = "someOtherText" Then
  4.     MsgBox "someOtherText"
  5. End If
Apr 6 '10 #2

Expert 5K+
P: 8,705
Select Case is not being used in the proper context since you are not specifically analyzing the entire Line, namely strL. The following compact code segment will do the trick for you:
Expand|Select|Wrap|Line Numbers
  1. 'Must first set a Refernce to the Microsoft Scripting Runtime Library
  2. Dim fso As FileSystemObject
  3. Dim fil As File
  4. Dim strL As String
  5. Dim tso As TextStream
  7. Set fso = New FileSystemObject
  9. 'Open the File for Reading
  10. Set fil = fso.GetFile("C:\Windows\Setuplog.txt")
  11. Set tso = fil.OpenAsTextStream(ForReading)
  13. Do While Not tso.AtEndOfStream
  14.   strL = tso.ReadLine
  15.     If Left$(strL, 8) = "sometext" Then
  16.       'YaDa, YaDa
  17.     ElseIf Left(strL, 13) = "someOtherText" Then
  18.       'More YaDa, YaDa
  19.     Else
  20.     End If
  21. Loop
Apr 6 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.