Here is a quick function that will dump any Access table or query to a Tab delimited file with headers. If you don't want the header, comment out the lines 38 to 47.
I built this function because, historically, Access databases will get corrupted and then have to be imported into a blank new database. Many times the drop down would be missed to check the box to import the existing Export and Import specifications. So then at the end-of-month, quarter, year the export would be tried and it will fail. The reason is that the specification is lost and would have to be rebuilt by hand.
It takes longer to build these custom import and export functions. However, once they are built built the first time, it becomes easier to adapt for the next situation.
I did this with a tab delimited (Chr(9)) file in mind. But you can easily change if it for a comma, pipe "|", or any other character you would like to use. Just substitute the Chr(9) code with either another Chr() code or a quoted value. To find other special characters do a search for "ASCII" in the Access help. It will give you a table of characters to use. If you wanted a double or single quoted export you can change the " & Chr(9) & " to "& Chr(xx) & Chr(9) & Chr(xx) &". Chr(39) is a single quote mark; Chr(34) is a double quote mark.
Just put in the TableOrQueryName as the Table or Query you want to export. Then put in the full file name and path (i.e "C:\Temp\MyFile.txt") as the FileNameAndPath value. That should allow you to dump the data out quickly and easily.
Note that this is built on the DAO reference. If it is missing, go to <CTRL>+<G> --> Tools --> References and select it.
Option Compare DatabaseOption ExplicitPublic Function Export_Tab_Delimited(TableOrQueryName As String, FileNameAndPath As String)'This uses the DAO database reference. IN the VB window (Ctrl+G) go to _ Tools --> References and select the Microsoft DAO 3.6 LibraryDim DB As DAO.DatabaseDim RS As DAO.RecordsetDim I As IntegerDim FileNum As Integer'Dim FileNameAndPath As StringDim OutputLine As StringFileNum = FreeFile()'FileNameAndPath = "C:\MyDirectory\MyFile.txt"Set DB = CurrentDb() 'Use the current databaseSet RS = DB.OpenRecordset(TableOrQueryName ) 'actually open the recordsetIf RS.EOF = False Then RS.MoveFirstElse MsgBox "No Data", vbExclamation, "Exiting Fuction" Set RS = Nothing Set DB = Nothing Exit FunctionEnd If'Open the file for outputOpen FileNameAndPath For Output Access Write Lock Write As FileNumI = 0OutputLine = ""'Output the field names as a headerFor I = 0 To RS.Fields.count - 1 If I > 0 Then OutputLine = OutputLine & Chr(9) & RS.Fields(I).NAME Else OutputLine = RS.Fields(I).NAME End IfNext IPrint #FileNum, OutputLine'Debug.Print OutputLineI = 0OutputLine = ""'start outputting the dataDo Until RS.EOF For I = 0 To RS.Fields.count - 1 If I > 0 Then OutputLine = OutputLine & Chr(9) & RS.Fields(I).Value Else OutputLine = RS.Fields(I).Value End If Next I Print #FileNum, OutputLine Debug.Print OutputLine OutputLine = "" RS.MoveNextLoopClose #FileNumSet RS = NothingSet DB = NothingEnd Function
'FileNameAndPath = "C:\MyDirectory\MyFile.txt
That is rem code for testing. I assume a valid from the end-user. I could probably build the FSO system, but that is not relevant for the code I'm posting.
1) When I download the db and run the form, I get an error "MS database engine could not find the input table or query 'MyQueryName'. Make sure that it exists and that it's name is spelled correctly." So I can't get it to work in it's initial db. How do I fix this?
2) If I want use this in another db, can I just export the Form and the function to the other database? If not, what is needed to do that? This way I don't have to code anything (I hope).