VB6 contro structure, If

Good morning expert,
Currently in our payroll system, if the costr center number is 9 digits we display the first four digits of the cost center number as the plant number, 'current system' snippet below. We are adding Canadian plants that have 10 and 11 digit cost center numbers per 'new system' snippet below. But the way i wrote breaks the system. I also tried Else if for the new conditiosn but that didn't work either.

Thanks.

Allen in Dallas

Begin current system++++++++++++++++++++++
            
' Show plants with UN-CLOSED entries
         vntPlants = objTimeEntry.RetrievePlantsWithOutRecords (Session("intDB"),vntList(0, lngCoCount), Session("EndDate"))
      If isempty(vntPlants) = false Then
      strOldPlantNumber = ""      ' initialize to empty string
         For lngCount = lbound(vntPlants, 2) to ubound(vntPlants, 2)
'               strNewPlantNumber = Left(vntPlants(0, lngCount),3)
            strNewPlantNumber = vntPlants(0, lngCount)
            if len(strNewPlantNumber) = 9 then
                  strNewPlantNumber = Left(strNewPlantNumber,4)
            else
                  strNewplantNumber = Left(strNewPlantNumber,3)
            end if

               If strOldPlantNumber <> strNewPlantNumber then
                     %></TR><TR><TD></TD><TD>Plant&nbsp;#&nbsp;<%Response.write strNewPlantNumber%></td><TD></TD>
                  <TR><TD></TD><TD></TD><TD><%
                      Response.write "<font color=" & Chr(34) & "#FF0000" & Chr(34) & ">" & vntPlants(0, lngCount) & "</Font>, "
               Else
                      Response.write "<font color=" & Chr(34) & "#FF0000" & Chr(34) & ">" & vntPlants(0, lngCount) & "</Font>, "
               End If
               strOldPlantNumber = strNewPlantNumber
         Next            
         
         
 End new system++++++++++++++++++++++        
         
' Show plants with CLOSED entries
      If isempty(vntPlants) = false Then
      strOldPlantNumber = ""      ' initialize to empty string
         For lngCount = lbound(vntPlants, 2) to ubound(vntPlants, 2)
'               strNewPlantNumber = Left(vntPlants(0, lngCount),3)
            strNewPlantNumber = vntPlants(0, lngCount)
            If len(strNewPlantNumber) = 9 then
                  strNewPlantNumber = Left(strNewPlantNumber,4)
            If len(strNewPlantNumber) = 10 then
                  strNewPlantNumber = Left(strNewPlantNumber,5)
            If len(strNewPlantNumber) = 11 then
                  strNewPlantNumber = Left(strNewPlantNumber,8)
            else
                  strNewPlantNumber = Left(strNewPlantNumber,3)
            end if
            If strOldPlantNumber <> strNewPlantNumber then
                     %></TR><TR><TD></TD><TD>Plant&nbsp;#&nbsp;<%Response.write strNewPlantNumber%></td><TD></TD>
                  <TR><TD></TD><TD></TD><TD><%
                     Response.write "<font color=" & Chr(34) & "#FF0000" & Chr(34) & ">" & vntPlants(0, lngCount) & "</Font>, "
               Else
                      Response.write "<font color=" & Chr(34) & "#FF0000" & Chr(34) & ">" & vntPlants(0, lngCount) & "</Font>, "
               End If
               strOldPlantNumber = strNewPlantNumber
         Next         
         
End new system++++++++++++++++++++++         
Allen PittsBusiness analystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
Is this really VB6? If so, some of the code has not come out correctly. Try pasting it as a snippet.

Can you define 'breaks the system', please? Is there an error message?
0
Antagony1960Commented:
Hello Allen in Dallas,

It's not clear what you are asking for, but the code above has a definite error in the if block on the new system.
The line <<If len(strNewPlantNumber) = 10 then>> should be <<ElseIf len(strNewPlantNumber) = 10 then>>

Having said that, you might the "Case Select" program flow structure easier to use. Your new system program flow would look like this:

Select Case len(strNewPlantNumber)
    Case 10
        strNewPlantNumber = Left(strNewPlantNumber,4)
    Case 11
        strNewPlantNumber = Left(strNewPlantNumber,5)
    Case 11
        strNewPlantNumber = Left(strNewPlantNumber,8)
    Case Else
        strNewPlantNumber = Left(strNewPlantNumber,3)
End Select
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Antagony1960Commented:
^^ Gremlins :-D That should be Case 9, Case 10 & Case 11 of course.

0
aikimarkCommented:
Antagony has the best solution.  The Select Case is cleaner and faster.

Explanation of failure:
If len(strNewPlantNumber) = 11 then
  strNewPlantNumber = Left(strNewPlantNumber,8)
else

Since execution falls into this If statement after checking for lengths 9 and 10, the length=11 check is applied with no consideration for the immediately prior length checks.

Note: If implementing this with If statements, you would need ElseIf statements for the checks after 9.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.