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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do Loop and If statement not working

Status
Not open for further replies.

NTesla

Programmer
Jul 19, 2002
146
US
Can someone please explain to me two things from the code below and how to fix the problems?

#1 Why would the If statement never be executed?
#2 Why no code past the Loop Statement is executed?

The Problem is that I open this file again later in the program for output, and I get an error that the file is already open. I have tried everything I can think of.

Private Sub Form_Load()
Dim BLNEOF As Boolean

On Error GoTo HandleErrors

BLNEOF = False

Open GSTRFilePath For Input As #1

Do

Input #1, GSTRInspectorName, GSTRInspectorNumber

CBOInspectorName.AddItem GSTRInspectorName
CBOInspectorName.ItemData(CBOInspectorName.NewIndex) = GSTRInspectorNumber
MsgBox "Not Closed", vbOKOnly, "NOT CLOSED" 'Trubleshooting Purposes
If EOF(1) = True Then
BLNEOF = True
MsgBox "Closed", vbOKOnly, "CLOSED" 'Trubleshooting Purposes
End If

Loop While BLNEOF = False

MsgBox "Closed", vbOKOnly, "CLOSED" 'Trubleshooting Purposes
Close

Form_Load_Exit:
Exit Sub

HandleErrors:

Select Case Err.Number

Case 53, 76 'File or path not found

Resume Form_Load_Exit 'Exit the procedure

Case 71 'Disk not ready

GINTResponse = MsgBox("Disk not ready. Retry?", _
vbRetryCancel + vbQuestion, "Disk Error")

If GINTResponse = vbRetry Then
Resume 'Try again
Else
End 'Exit Program
End If

'Case Else 'All other errors should cancel execution
' Err.Raise Err

End Select
End Sub
 


Hi NTesla:

Delete from where? From the CBO, it is a simple code loop. From the data file, it is a little more involved.

Untested code:
Code:
Private Function DeleteFromFile(ByVal STRDeletion As String) As Boolean
    Dim INTInFile As Integer, INTOutFile As Integer
    Dim STRTemp As String
    
    DeleteFromFile = False
    
    On Error GoTo DeleteFromFile_Error
    '
    '   Set up the original data file from reading.
    '
    INTInFile = FreeFile
    Open "c:\test1.txt" For Input As #INTInFile
    '
    '   Set up a new file for writing to.
    '
    INTOutFile = FreeFile
    Open "c:\test2.txt" For Output As #INTOutFile
    '
    '   Loop to scan the input file.
    '
    Do
        '   Get a line from the input file.
        Line Input #INTInFile, STRTemp
        '   Trim the whitespace and if there is anything left, process it, . . .
        If Len(Trim(STRTemp)) > 0 Then
            '   Check to see whether the deletion string exists in the current line.
            If InStr(1, STRTemp, STRDeletion) = 0 Then
                '   Deletion string was not found.
                '   Write the current line to the output file.
                Print #INTOutFile, STRTemp
                
                DeleteFromFile = True   ' Let the calling routine know that we found
                                        ' at least one line with the deletion string
                                        ' in it.
            End If
            '   Have we reached the end of the file?  If so, flag it.
            If EOF(intFileNumber) = True Then BLNEOF = True
        Else
            '   . . . otherwise, flag the end of the input file.
            BLNEOF = True
    Loop While Not BLNEOF
    '
    '   Close the two files.
    '
    Close #INTInFile
    Close #INTOutFile
    '
    '   Delete the original data file and rename the new file as the data file.
    '
    Kill "c:\test1.txt"
    Name "c:\test2.txt" As "test1.txt"
    '
    '   Tidy up and leave.
    '
    On Error GoTo 0
    Exit Function

DeleteFromFile_Error:
    '   Show an error report in the "Immediate" window.
    Debug.Print "DeleteFromFile : " & Err.Number & " : "; Err.Description
    '   Clear the error.
    Err.Clear
    '
    '   Tidy up and leave.
    '
    On Error GoTo 0
    DeleteFromFile = False
End Function

Cassie
 
From the file! Unfortunetly. Actually fom the CBO to be written to the file. I want the user to be able to add an Inspector to the file which I have working fine, or Remove an Inspector from the file. On the remove form I have a combo box where the user can select an inspector to be removed or clear the list. The code that we have been working on is for the remove form, this code is executed when the form is loaded to read all the Inspectors in to the combo box for the user to select. When the user clicks the remove command then the code is executed to remove the inspector.
 
Hey Cassie,

Just got home from work. Copied and pasted your code into my program and guess what IT WORKED!!!! IT WORKED!!!!![2thumbsup]

Thanks a million!!!! Know All I have to do is get the delete portion to work!!![3eyes]

this is my code:

Open GSTRFilePath For Output As #1
INTMaximum = CBOInspectorName.ListCount - 1

For INTIndex = 0 To INTMaximum
Write #1, CBOInspectorName.List(INTIndex)
Write #1, CBOInspectorName.ItemData(CBOInspectorName.NewIndex) = GSTRInspectorNumber


Next INTIndex

Close #1

Am I again going about it all wrong.

Man I tell you its been too long since I worked with fils like this!!!

Only the name gets written to the file I get a Run Time Error 381, Invalid Property Array Index at the following line.
Write #1, CBOInspectorName.ItemData(CBOInspectorName.NewIndex) = GSTRInspectorNumber
 


Hi NTesla:

Yes, you are on the right track. However, a bit of change to the code that you have posted:
Code:
    Dim INTInFile as Integer

    INTInFile = FreeFile
    Open GSTRFilePath For Output As #1

Just using the
Code:
Open GSTRFilePath For Output As #1
runs the risk of conflicting with a file that the system already has open. FreeFile provides a "safe" filenumber to use.

Your second Write line appears to be in error. The
Code:
 = GSTInspectorNumber
doesn't make sense to me.

Another "error" in your code is that you forgot to put the comma into the file. So, alter the code to
Code:
    Write #INTInFile, CBOInspectorName.List(INTIndex)
    Write #INTInFile, ","
    Write #INTInFile, CBOInspectorName.ItemData(CBOInspectorName.NewIndex)

Personally, I prefer to do the following:
Code:
    With CBOInspectorName
        Print #INTInFile, .List(INTIndex) & "," & .ItemData(.NewIndex)
    End With

Cassie
 


Hi NTesla:

It's too early in the morning. I missed the error that is being generated, as you reported.

In place of the
Code:
    .ItemData(CBOInspectorName.NewIndex)
use
Code:
    .ItemData(INTIndex)

NewIndex only works when one has just added something to the combobox or listbox.

Cassie
 
Hi Cassie

IT WORKS!!! IT WORK!!!! IT WORKS!!!

I really apreciate all your help!!

I still have a question however. Although I have broken down and have started using the FreeFile. I still do not under stand how it works. If you have three open files how do you know the right data is getting into the right file, or if you want to close say the third file opened but leave the first ope how do you go about doing that.
 

Hi NTesla:

FreeFile works with the Windows operating system. When you invoke it, it queries Windows to obtain an unused filenumber. One uses an assignment statement (such as
Code:
INTInFile = FreeFile
to keep track of the filenumber that Windows gave us.

FreeFile only obtains a filenumber for you. It doesn't care whether you have other files open. To keep the data flowing to or from the right file, you just keep track of the variables you used to open the files.

Use appropriately-named integer variables for your file references, such as INTInFile, INTOutFile, INTBackupFile.

When you write to a file using one of the integer variable, it writes the file that you assigned to that integer variable.

Code:
    INTInFile = FreeFile  ' Assign a filenumber for In File.
    Open FILE1 for Input as #INTInFile  ' Now you have assigned the FILE1 file to INTInFile.  Whenever you read from INTInFile, it will come from FILE1, not FILE2, not FILE3 or any other file.

    INTOutFile = FreeFile  ' Assign a filenumber for Out File.
    Open FILE2 for Output as #INTOutFile  ' Now you have assigned the FILE2 file to INTOutFile.  Whenever you write or print to INTOutFile, it will go to FILE2, not FILE1, not FILE3 or any other file.

It doesn't matter which order I had opened the files, I can close any of the files using its assigned integer variable at anytime.

So, I could do the following:
Code:
    INTFile1 = FreeFile
    Open data1.txt For Input As #INTFile1
    INTFile2 = FreeFile
    Open data2.txt For Input As #INTFile2
    INTFile3 = FreeFile
    Open data3.txt For Output As #INTFile3
  
   . . . (some processing) . . . 

    Close #INTFile2     ' say that we finished reading data2.txt

   . . . (some more processing) . . . 

    Close #INTFile1
    Close #INTFile3

Just remember the sequence for each file: FreeFile, Open, Read or Write, and finally Close. It does not matter if you work on another file in the mean time. You reference each by its integer variable.

Cassie
 


Hi NTesla:

Remember: FreeFile is function, not a variable. Each time you invoke FreeFile, it goes to Windows to get an unused filenumber. Unless you close a previously opened file, FreeFile will not return the filenumber that was previously assigned.

Cassie
 
Thanks for the explaination. I understand it now. You would happen to teach do you. You explained that very well.[smile]

Thanks again for the help[2thumbsup]
 


Hi NTesla:

I have taught and love to teach, although I do not teach at the present.

Thanks for the compliment! You're very welcome!

Cassie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top