[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

VB6 contro structure, If

Posted on 2008-11-12
Medium Priority
356 Views
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++++++++++++++++++++++
0
Question by:Allen Pitts
• 2

LVL 76

Expert Comment

ID: 22940691
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

LVL 11

Accepted Solution

Antagony1960 earned 2000 total points
ID: 22941687
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

LVL 11

Expert Comment

ID: 22941724
^^ Gremlins :-D That should be Case 9, Case 10 & Case 11 of course.

0

LVL 46

Expert Comment

ID: 22943054
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

Featured Post

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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 Month18 days, 17 hours left to enroll