?
Solved

Select..Case statement.Help.

Posted on 2003-02-26
10
Medium Priority
?
369 Views
Last Modified: 2010-04-07
I want to do this but it does not work:

Select Case i

Case i=1
  Call f1()
Case i=2
  Call f2()
.
.
End Select

 What this code should be doing is for i=1,call suubroutine f1 and i=2,call f2 and so on.But it does not work,as i step thru the code,the Call f1()does not jump to subroutine f1.How should I do??
0
Comment
Question by:VivienW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 

Expert Comment

by:RMatzka
ID: 8024031
Try this:

Select Case i

Case 1
 Call f1()
Case 2
 Call f2()
.
.
End Select

When you use "Case i=1", the expression "i=1" evaluates either to true or false, which is -1 or 0, which does not match your values of i.
0
 
LVL 1

Expert Comment

by:sazhagianambi
ID: 8024114
This is The Syntax For Select

Select Case <Variable>

Case "Value":
        ------
Case "Value":
        -----
Case "Value":
        -----
Else

End Select

Regards,
Nambi
0
 
LVL 3

Expert Comment

by:Gunsen
ID: 8025516
This is beacuse (as RMatzka and sazha says):

Case i=1
'This returns True (True equeals -1 numeric, not 1), thus F1 will never be fired
Case i=2
' This returns False (0) and will never ever call F2

Select Case i
Case 1: F1
Case 2: F2
End Select
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:VivienW
ID: 8029946
Actually this is how my code looks like:

Private Sub FileERT_Click()
Dim i%, textline$, k1$, d1$, z1%, j%

d1 = DirERT.Path & "\" & FileERT.FileName

i = FreeFile

Open d1 For Input Shared As #i


Do While Not EOF(i)
 Line Input #i, textline    'reading each line
k1 = Mid(textline, 2, 2)
first = Right(textline, 2)
z1 = CInt("&h" & first) + 3
last = Right("0" & Hex(z1), 2)

Select Case i

Case 1
    Call cmdK1_Click
Case 2
    Call cmdK2_Click
Case 3
   Call cmdK3_Click

End Select
Loop

Close #i
End Sub

Here,whenever I read the first line of the file, I'd want to call cmdK1 and the second line cmdK2 and third line cmdK3 and so on. Using this code,whenever I read the second line(i=2),still cmdK1 is fired.
Any solution to this??
0
 
LVL 1

Expert Comment

by:sazhagianambi
ID: 8030952
Hi,

Try Out This. Just Declare One Variable to count The Line Number.

According to that You can call Functions As You Like

Private Sub FileERT_Click()

Dim i%, textline$, k1$, d1$, z1%, j%
Dim First, Last

d1 = DirERT.Path & "\" & FileERT.FileName

i = FreeFile

Open d1 For Input Shared As #i

Dim LineCount
LineCount = 0
Do While Not EOF(i)
  LineCount = LineCount + 1
  Line Input #i, textline    'reading each line
  k1 = Mid(textline, 2, 2)
  First = Right(textline, 2)
  'z1 = CInt("&h" & First) + 3
  Last = Right("0" & Hex(z1), 2)
  Select Case LineCount

    Case 1
        'Call cmdK1_Click

    Case 2
        'Call cmdK2_Click
    Case 3
        'Call cmdK3_Click
    End Select
Loop

Close #i
End Sub

I Hope this Will Help You Better Manner.

Regards,
Nambi
0
 

Author Comment

by:VivienW
ID: 8031441
Thanks Nambi, that looks really good. What if it comes to EOF?? Will LineCount still read the last line???
0
 
LVL 1

Accepted Solution

by:
sazhagianambi earned 160 total points
ID: 8031500
Hi Friend,

Defintely this will Work Until Eof File. If You Add One Else Part in Select Statement, ie

Select
Case 1
case 2
case 3
Else
  Call Function()
End select

Then It Will Work If Your File LineCount Exceeds the 3 lines. After 3rd line Every Time Else part will be called.

Regards,
Nambi



0
 

Expert Comment

by:CleanupPing
ID: 8901098
VivienW:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8972932
Moderator, my recommended disposition is:

    Accept sazhagianambi's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question