Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Whats wrong with this If-Then-ElseIf??

Status
Not open for further replies.

londonkiwi

Programmer
Sep 25, 2000
83
NZ
I'm reading a string from a text file - whats wrong with the if-then-else staement?? Thanks in anticipation


Do Until EOF(FileNumRead) ' get first line of data
Line Input #FileNumRead, sLine
Do While Left(sLine, 2) >= 12 And Left(sLine, 2) <= 18 'time btw 12-18 hrs
If Mid(sLine, 19, 1) = 1 Then ' AtoB
If Trim(Mid(sLine, 21, 2)) = 1 Then 'Class = 1
ab1_Count = ab1_Count + 1
ElseIf Trim(Mid(sLine, 21, 2)) = 2 Then 'Class = 2
ab2_Count = ab2_Count + 1
End If
ElseIf Trim(Mid(sLine, 21, 2)) = 3 Then 'Class = 3
ab3_Count = ab3_Count + 1
End If
ElseIf Trim(Mid(sLine, 21, 2)) = 4 Then 'Class = 4
ab4_Count = ab4_Count + 1
End If
ElseIf Trim(Mid(sLine, 21, 2)) = 5 Then 'Class = 5
ab5_Count = ab5_Count + 1
End If
Else: Trim(Mid(sLine, 21, 2)) = 6 'Class = 6
ab6_Count = ab6_Count + 1
End If
End If
End If
Loop
Loop

Print #FileNumWrite, ab1_Count & &quot; &quot; & ab2_Count & &quot; &quot; & ab3_Count & &quot; &quot; & ab4_Count & ab5_Count & &quot;12-18&quot; [sig][/sig]
 
You have 2 &quot;If&quot; statements and 7 &quot;End If&quot; statements. Each &quot;If&quot; must have one and only one &quot;End If&quot;. You cannot exit an &quot;If - End If&quot; the same way as a &quot;Do - Loop&quot; or a &quot;For - Next&quot;, by using Exit Do or Exit For.
Perhaps a Select Case would work better for you.
I think this is correct, I hope it helps.
Rowland [sig][/sig]
 
It seems to me you are assuming that Elseif is a new IF statement and requires an endif, this is incorrect. It is treated as an ELSE within the main IF nest, and does not require endif. [sig][/sig]
 
Thanks for all the help. I have made ammendment as below.
Can you masters tell me a) what I'm doing wrong b) how to fix it and c) how to start a new line when a new day is found:

Sample of Raw Data
22:31:23 04/10/00 AB 1
23:03:56 04/10/00 BA 2
23:04:38 04/10/00 AB 5
23:08:49 04/10/00 AB 1
05:31:38 05/10/00 BA 8
06:34:46 05/10/00 AB 10
06:43:53 05/10/00 AB 1
07:11:52 05/10/00 AB 1

Private Sub proc_finMC_Click()

Dim ab1_Count, ab2_Count, ab3_Count, ab4_Count, ab5_Count, ab6_Count As Long
Dim lLine As Long
Dim FileNumRead As Integer
Dim FileNumWrite As Integer
Dim sLine As String

Dim sFileName As String
sFileName = App.Path & &quot;\MCexport.txt&quot;
Dim sNewFile As String
sNewFile = App.Path & &quot;\MCFin_Proc.txt&quot;
FileNumRead = FreeFile

Open sFileName For Input As #FileNumRead
FileNumWrite = FreeFile
Open sNewFile For Output As #FileNumWrite

Do Until EOF(FileNumRead)
Line Input #FileNumRead, sLine
Loop

ab1_Count = 0: ab2_Count = 0: ab3_Count = 0: ab4_Count = 0: ab5_Count = 0: ab6_Count = 0

Do While Trim(Left(sLine, 2)) >= &quot;0&quot; And Trim(Left(sLine, 2)) >= &quot;6&quot; 'time btw xx-xx hrs
If Mid(sLine, 19, 2) = &quot;AB&quot; Then ' AtoB
If Trim(Mid(sLine, 22, 2)) = &quot;1&quot; Then 'Class = 1
ab1_Count = ab1_Count + 1
ElseIf CStr(Trim(Mid(sLine, 22, 2))) = &quot;2&quot; Then 'Class = 2
ab2_Count = ab2_Count + 1
ElseIf Trim(Mid(sLine, 22, 2)) = &quot;3&quot; Then 'Class = 3
ab3_Count = ab3_Count + 1
ElseIf Trim(Mid(sLine, 22, 2)) = &quot;4&quot; Then 'Class = 4
ab4_Count = ab4_Count + 1
ElseIf Trim(Mid(sLine, 22, 2)) = &quot;5&quot; Then 'Class = 5
ab5_Count = ab5_Count + 1
Else: 'Class = 6 or something else
ab6_Count = ab6_Count + 1
End If
End If
Loop
Print #FileNumWrite, &quot;AB&quot; & &quot;,&quot; & ab1_Count & &quot;,&quot; & ab2_Count & &quot;,&quot; & ab3_Count & &quot;,&quot; & ab4_Count & &quot;,&quot; & ab5_Count & &quot;,&quot; & ab6_Count
Close #FileNumRead
Close #FileNumWrite
MsgBox &quot;Completed&quot; [sig][/sig]
 
Hello,

Try this...Not exactly sure what you are trying to do
but I think this is what you want. Here are the results
I get using your test data and your code with some
changes I made:

AB,2,0,0,0,1,0
AB,1,0,0,0,0,0


Here's the code I used, it may be wrong, but I think it
will get you closer than you were:

Private Sub proc_finMC_Click()

Dim ab1_Count As Long, ab2_Count As Long, ab3_Count As Long
Dim ab4_Count As Long, ab5_Count, ab6_Count As Long

Dim lLine As Long
Dim FileNumRead As Integer
Dim FileNumWrite As Integer
Dim sLine As String
Dim sFileName As String
Dim sNewFile As String
Dim intDay As Integer

sFileName = App.Path & &quot;\MCexport.txt&quot;
sNewFile = App.Path & &quot;\MCFin_Proc.txt&quot;
FileNumRead = FreeFile

Open sFileName For Input As #FileNumRead
FileNumWrite = FreeFile
Open sNewFile For Output As #FileNumWrite

intDay = 0

ab1_Count = 0: ab2_Count = 0: ab3_Count = 0: ab4_Count = 0
ab5_Count = 0: ab6_Count = 0

Do While Not EOF(FileNumRead)
Line Input #FileNumRead, sLine

If Trim(Left(sLine, 2)) >= &quot;07&quot; Then
'check if we need a new line
If intDay <> Mid$(sLine, 10, 2) Then
If intDay > 0 Then
Print #FileNumWrite, &quot;AB&quot; & &quot;,&quot; & ab1_Count & &quot;,&quot; & ab2_Count & &quot;,&quot; & ab3_Count & &quot;,&quot; & ab4_Count & &quot;,&quot; & ab5_Count & &quot;,&quot; & ab6_Count
End If
ab1_Count = 0: ab2_Count = 0: ab3_Count = 0
ab4_Count = 0: ab5_Count = 0: ab6_Count = 0
intDay = Mid$(sLine, 10, 2)
End If

If Mid(sLine, 19, 2) = &quot;AB&quot; Then ' AtoB
Select Case Trim(Mid(sLine, 22, 2))
Case &quot;1&quot; 'Class = 1
ab1_Count = ab1_Count + 1
Case &quot;2&quot; 'Class = 2
ab2_Count = ab2_Count + 1
Case &quot;3&quot; 'Class = 3
ab3_Count = ab3_Count + 1
Case &quot;4&quot; 'Class = 4
ab4_Count = ab4_Count + 1
Case &quot;5&quot; 'Class = 5
ab5_Count = ab5_Count + 1
Case Else 'Class = 6 or something else
ab6_Count = ab6_Count + 1
End Select
End If 'Mid(sLine, 19, 2) = &quot;AB&quot;
End If 'Trim(Left(sLine, 2)) >= &quot;07&quot;
Loop 'EOF(FileNumRead)

'print the last one
Print #FileNumWrite, &quot;AB&quot; & &quot;,&quot; & ab1_Count & &quot;,&quot; & ab2_Count & &quot;,&quot; & ab3_Count & &quot;,&quot; & ab4_Count & &quot;,&quot; & ab5_Count & &quot;,&quot; & ab6_Count

Close #FileNumRead
Close #FileNumWrite
MsgBox &quot;Completed&quot;

End Sub

 
Thanks for the help

I am trying to read through a text file, which is of the floowing format:
hh:mm:ss dd/mm/yy AB(orBA) 1(to13)
FOR each new day, GROUP the times into the following bands (0-6, (7-12), (13-18),(19-24). FOR each time band and each AB(orBA), determine how many times the values 1-13 are found, then write that data to a new line in a file

eg
&quot;filename&quot;,&quot;date&quot;,AB&quot;,&quot;time eg6,12,18 or 24&quot;,0010,0040,0050 etc (13 of these) [sig][/sig]
 
The Data you are working with fits perfect in a User Defined Type
In a standard module;
Type myType
myTime as Date
myDate as Date
myBln as Boolean 'AB/BA, only two choices True =AB
'False =BA
myInt as Integer
End Type
Public mType as myType

and, since it fits into a User Defined Type (UDT) the binary file access works better IF you can rewrite the original data into the binary format

This is an example of how to open a binary file;
Open sFileName for Random as #FileNumRead len = len(mType)
You then can loop through the file useing a loop like such
Dim sType as myType
Do Until EOF(#FileNumRead)
Get #FreeFile,,sType

'Here you would break it into your bands
but what makes this easier is that you could for example get the hour from the Time portion like this instead of all those inefficiant string manipulations
intHour = Hour(sType.myTime)
Loop

Hope this helps alittle
Collin
[sig][/sig]
 
Again, thanks for the input. Learning heaps

I have completed this wee task!. If anyone wants to see the finished code, leave a msg here + email, and I'll send it to you.
lk [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top