Option Compare Database
Public Function ExportCaseManagement()
Dim MyDB As DAO.Database
Dim rst As DAO.Recordset
Dim intFldCtr As Integer
Set MyDB = CurrentDb
MyDB.Execute "Delete * FROM [Case Management File]" ' delete contents so an append can populate tables'
MyDB.Execute "7 Case Management Query" 'append query for the table'
Set rst = MyDB.OpenRecordset("Case Management File", dbOpenSnapshot)
Open "C:\Test\Case Management.csv" For Output As #1 'location for csv file from the table'
rst.MoveFirst
With rst
'Write Field names to Output File, delimiting by '|'
For intFldCtr = 0 To .Fields.Count - 1
strBuild = strBuild & .Fields(intFldCtr).Name & "|"
Next
Print #1, Left$(strBuild, Len(strBuild) - 1) 'Field Names, remove ending ' | '
strBuild = "" 'Must RESET
Do While Not .EOF 'Values in Fields delimited by '|'
For intFldCtr = 0 To .Fields.Count - 1
strBuild = strBuild & .Fields(intFldCtr).Value & "|"
Next
Print #1, Left$(strBuild, Len(strBuild) - 1) 'Each Record, remove ending ' | '
strBuild = "" 'Must RESET for Next Record
.MoveNext
Loop
End With
rst.Close
Set rst = Nothing
Set MyDB = Nothing
Close #1
Application.Quit
End Function
If not, then you have a hanging reference somewhere. Make sure you are closing any objects you open and are setting object variables to nothing. i.e.
Dim rst As DAO.Recordset
set rst = ....
'Clean up.
rst.Close
Set rst = Nothing
Jim.