-
What am I doing wrong with freefile
Hi,
What's wrong with my code - which is designed to count the number of lines in long ascii files and gives the user a chance to abort the process:
HTML Code:
int_current_file_number = FreeFile
cmdCount.Visible = False
cmdStop.Visible = True
Open lstrFileName$ For Input As int_current_file_number
Do While EOF(int_current_file_number) = False
Line Input #int_current_file_number, str_input ' input file in str_input to count lines
lngNumberLines = lngNumberLines + 1 ' increment lines
If lngNumberLines Mod 1000 = 0 Then ' return focus to system every 1000 lines, update count
DoEvents
lblLines = "(total: " & lngNumberLines & ")"
End If
If blnAbort = True Then ' if user clicked stop then clear count and show count command button
lblLines.Caption = "(total: Unknown)"
cmdCount.Visible = True
cmdStop.Visible = False
Exit Do
End If
Loop
Close int_current_file_number
If I click the 'cmdstop' button somehow the int_current_file_number is incremented by one and the do...loop continues and produces a 'bad file number error'. The sub isnt revisiting the first line of code again (I used breakpoints) so how is it incrementing....??
No doubt it is some simple oversight but I just can't figure it out. Thanks for your time.
Cheers,
Vbnewbie
-
Try making your "If blnAbort Then..." as simple as possible:
If blnAbort Then Exit Do
Update the label and the buttons after you've exited the loop. Let us know if that makes any difference.
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
Thanks, I gave that a go - still no satisfaction. I still can't see what is wrong with it...
-
post the cmdstop_click code (and people do continue to ask me why I hate DoEvents...)
Marco
-
cmdstop_click contains only "blnAbort = True"
Does it matter that the first lot of code (cmdcount_click) shown above is within a "select case"....?
-
when you use a DoEvents, everything is possible.
Put a break point in the cmdstop_click event, and then use F8 to debug your code step by step. That will find 'easy' problems.
Marco
-
?
Marco,
I followed your advice and put a breakpoint in the cmdstop_click sub - but it doesnt stop. Obviously, the code runs because otherwise blnabort would not be true, but the breakpoint isnt activated and it goes straight to 'bad file' etc in the first line of the do loop.
If I manually exit the loop there is not problem (ie. put in an exit do when the number of lines equals an arbritary number), but if I rely on a cmdstop_click event.....
-
 Originally Posted by Lithic
Does it matter that the first lot of code (cmdcount_click) shown above is within a "select case"....?
Since the assembled multitude hasn't solved your prob yet, and you imply that you haven't posted all the code for the cmdcount_click, then why not show us the rest?
-
Ok
Ok, here's the lot. Thanks for your time.
HTML Code:
Private Sub cmdCount_Click()
Dim intGoAhead As Integer
intGoAhead = MsgBox("This will count the total number of lines in the file and may take a long time....continue anyway?", 36, "Count lines...")
Select Case intGoAhead:
Case 6: 'yes
' count
int_current_file_number = FreeFile
cmdCount.Visible = False
cmdStop.Visible = True
Open lstrFileName$ For Input As int_current_file_number
Do While EOF(int_current_file_number) = False
Line Input #int_current_file_number, str_input ' input file in str_input to count lines
lngNumberLines = lngNumberLines + 1 ' increment lines
If lngNumberLines Mod 1000 = 0 Then ' return focus to system every 1000 lines, update count
DoEvents
lblLines = "(total: " & lngNumberLines & ")"
End If
If blnAbort Then ' = True Then ' if user clicked stop then clear count and show count command button
lblLines.Caption = "(total: aborted)"
cmdCount.Visible = True
cmdStop.Visible = False
Exit Do
End If
Loop
Close int_current_file_number
Case 7: 'no
Exit Sub
End Select
End Sub
-
Code:
intGoAhead = MsgBox("This will count the total number of lines in the file and may take a long time....continue anyway?", 36, "Count lines...")
Select Case intGoAhead:
Case 6: 'yes
Personally cannot see answer.
However, FYI would suggest easier to code (and subseq. read) if you use VB values ... (all available from msgbox 'dropdown')
Code:
intGoAhead = MsgBox("whatever", vbYes + vbNo + vbQuestion,"Heading")
Select Case intGoAhead
case vbYes
(etc)
-
there is nothing wrong in the code you POSTED. The following works perfectly fine with me...
Marco
Code:
Option Explicit
Private blnAbort As Boolean
Private Sub cmdCount_Click()
Dim intGoAhead As Integer
Dim int_current_file_number As Integer
Dim lstrFileName As String
Dim str_input As String
Dim lngNumberLines As Long
lstrFileName = "<- Insert your file name here"
intGoAhead = MsgBox("This will count the total number of lines in the file and may take a long time....continue anyway?", 36, "Count lines...")
Select Case intGoAhead:
Case 6: 'yes
' count
int_current_file_number = FreeFile
cmdCount.Visible = False
cmdStop.Visible = True
blnAbort = False
Open lstrFileName$ For Input As int_current_file_number
Do While EOF(int_current_file_number) = False
Line Input #int_current_file_number, str_input ' input file in str_input to count lines
lngNumberLines = lngNumberLines + 1 ' increment lines
If lngNumberLines Mod 1000 = 0 Then ' return focus to system every 1000 lines, update count
DoEvents
lblLines = "(total: " & lngNumberLines & ")"
End If
If blnAbort Then ' = True Then ' if user clicked stop then clear count and show count command button
lblLines.Caption = "(total: aborted)"
cmdCount.Visible = True
cmdStop.Visible = False
Exit Do
End If
Loop
Close int_current_file_number
Case 7: 'no
Exit Sub
End Select
End Sub
Private Sub cmdStop_Click()
blnAbort = True
End Sub
-
Yep, you are right Marco - if I run this code by itself it works perfectly. Thanks for your trouble... If I manage to find out what is causing the problem I'll post is here for the benefit of others.
Thanks too gupex for your time and advice, I'm looking forward to learning a lot more from this forum.
Cheers.
-
embarrasment...
They don't have any red-faced icons here....
I found the problem, very embarrasing. I had declared freefile as a global variable (why, I dont know).....a good advert for sticking to proper naming conventions.
Anyhow, thanks all for your time. If it is any consolation, I learnt a lot from your advice and suggestions.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks