What am I doing wrong with freefile


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: What am I doing wrong with freefile

Hybrid View

  1. #1
    Join Date
    Dec 2004
    Posts
    110

    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

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    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!

  3. #3
    Join Date
    Dec 2004
    Posts
    110
    Thanks, I gave that a go - still no satisfaction. I still can't see what is wrong with it...

  4. #4
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    post the cmdstop_click code (and people do continue to ask me why I hate DoEvents...)
    Marco

  5. #5
    Join Date
    Dec 2004
    Posts
    110
    cmdstop_click contains only "blnAbort = True"

    Does it matter that the first lot of code (cmdcount_click) shown above is within a "select case"....?

  6. #6
    Join Date
    Feb 2004
    Location
    Sydney, Australia
    Posts
    498
    Quote 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?

  7. #7
    Join Date
    Dec 2004
    Posts
    110

    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

  8. #8
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    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

  9. #9
    Join Date
    Dec 2004
    Posts
    110

    Unhappy ?

    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.....

  10. #10
    Join Date
    Feb 2004
    Location
    Sydney, Australia
    Posts
    498
    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)

  11. #11
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    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

  12. #12
    Join Date
    Dec 2004
    Posts
    110

    Smile

    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.

  13. #13
    Join Date
    Dec 2004
    Posts
    110

    Red face 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center