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

Array question for VBA

Status
Not open for further replies.

BeginnerProgrammer

Technical User
Aug 2, 2002
33
0
0
US
I am using the following code to read a text file into an array and need to write certain elements to a separate text file. Please help.

zoscarray = Split(mytextfilestring, "--------------------------------------------------------------------------------")

I know how to identify the array elements I just don't know how to write them to a text file.

Thanks in advance for your help.
 
Code:
Open "C:\Text.txt" For Writing As #1
Print #1, zoscarray(1)
Close #1

As far as I can remember.
 
... which is not very far.
[tt]Open "C:\Text.txt" For Output As #1[/tt]
 
This is only writing the first occurence. I need for it to continue to write to the text file for each occurence that it encounter within the array. I know that it is looping through the array because I had it writing individual files for each element of the array. Now I want it to write one file for each unique element. I guess the easiest way to say it is, say I have an employee ID of 130, I want it to find employee ID of 130 in the array and if it finds it then write/append it to the text file. Here is my code.

Public Function zoscfiletostring()
Dim intcount As Integer
Dim zoscarray() As String
Dim rst
Dim PATH
Dim MYLEN
Dim WRKDRV
Dim myemp
Dim mycall

PATH = CurrentDb.Name
MYLEN = Len(PATH) - (Len(CurrentProject.Name) + 1)
WRKDRV = Left(PATH, MYLEN)
mytextfilestring = ReadFile(WRKDRV & "\zosc.txt")

Set rst = CurrentDb().OpenRecordset("zosc", DB_OPEN_DYNASET)

'MsgBox "Made it to here"
zoscarray = Split(mytextfilestring, "--------------------------------------------------------------------------------")

For intcount = 0 To UBound(zoscarray())
rst.AddNew
rst.call_data = "zosc"
rst.Update
Next

Set rst = CurrentDb().OpenRecordset("zosc")
MYCOUNT = rst.RecordCount

Set rst = CurrentDb().OpenRecordset("zosc", DB_OPEN_DYNASET)
rst.MoveFirst
While MYCOUNT > 0

For intcount = 0 To UBound(zoscarray())
MYCOUNT = MYCOUNT - 1
If MYCOUNT >= 0 Then
myint = InStr(zoscarray(MYCOUNT), "empl:")
myemp = Mid(zoscarray(MYCOUNT), myint + 6, 6)
myemp = RTrim(myemp)

If myemp = 64501 Then
Open WRKDRV & "\TECH FILES\" & myemp & ".TXT" For Output As #1
Print #1, zoscarray(MYCOUNT)
Close #1
End If

End If
Next

Wend
DoCmd.SetWarnings False
DoCmd.RunSQL "delete * from zosc"
DoCmd.SetWarnings True

End Function

Thanks
 
If you move this:

Open WRKDRV & "\TECH FILES\" & myemp & ".TXT" For Output As #1

outside of the loop and only open the file once then either For Output or For Append would work.

I can't see any reason to repeatedly open and close the file.

Ed Metcalfe.

Please do not feed the trolls.....
 
That should not matter. Try:
Code:
[b]Open WRKDRV & "\TECH FILES\" & myemp & ".TXT" For Output As #1[/b]
While MYCOUNT > 0

    For intcount = 0 To UBound(zoscarray())
        MYCOUNT = MYCOUNT - 1
            If MYCOUNT >= 0 Then
            myint = InStr(zoscarray(MYCOUNT), "empl:")
            myemp = Mid(zoscarray(MYCOUNT), myint + 6, 6)
            myemp = RTrim(myemp)
                        
                If myemp = 64501 Then
                Print #1, zoscarray(MYCOUNT)
                End If
            
            End If
    Next
            
Wend
[b]Close #1[/b]

As metioned by Ed2020. Don't forget to Close #1 in your error handler, too.
 
Beg pardon, I wss not paying sufficient attention, I should have said:
[tt]Open WRKDRV & "\TECH FILES\64501.TXT" For Output As #1[/tt]

Because that is the only file that is opened from myemp, according to your code:

Code:
If [b]myemp = 64501[/b] Then
      Open WRKDRV & "\TECH FILES\" & myemp & ".TXT" For Output As #1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top