Option Explicit 'This script will convert a Microsoft Access query or table into plain-text. 'It only exports the data values (it doesn't try to get the field names). 'Pass it the mdb file, plain-text file name, query name, comma-delimited max 'desired lengths of each field and preferred separators on the command line: 'START /W "MdbToTxt.vbs" "Database.mdb" "QueryName" "Output.txt" "4,2,4,17" "---" 'There MUST be a max length number for every field that will be present 'in the query. There should also be one fewer separators than there are 'fields. Remember: It's your responsibility to be sure whatever separator 'you use isn't present in your input data! Main Sub Main Dim strMdbFile 'As String Dim strTextFile 'As String Dim strQuery 'As String Dim strLength 'As String Dim strSeparator 'As String Dim strOutput 'As String Dim strData 'As String Dim intField 'As Integer Dim fs 'As Scripting.FileSystemObject Dim dbeng 'As DAO.DBEngine Dim db 'As Database Dim rs 'As Recordset Dim ts 'As Scripting.TextStream Const dbOpenDynaset = 2 'DAO.RecordsetTypeEnum Const ForWriting = 2 'Check for arguments If Wscript.Arguments.Count <> 5 Then CreateObject("Wscript.Shell").Popup "This script will export an Access query to text. Pass it the mdb file name, query name, output text file name, field lengths, and separators. Read the comments in this script for more info.", 10 Wscript.Quit 1 End If 'Get database file name strMdbFile = Wscript.Arguments(0) If strMdbFile = "" Then strMdbFile = Dir("mdb") If instr(strMdbFile, ":\") = 0 Then strMdbFile = FileNameInThisDir(strMdbFile) 'Get query or table name strQuery = Wscript.Arguments(1) 'Get output text file name strTextFile = Wscript.Arguments(2) If strTextFile = "" Then strTextFile = strMdbFile & ".txt" If instr(strTextFile, ":\") = 0 Then strTextFile = FileNameInThisDir(strTextFile) 'Get output field lengths strLength = Wscript.Arguments(3) If strLength = "" Then strLength = "255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255" 'Get separator types strSeparator = Wscript.Arguments(4) If strSeparator = "" Then strSeparator = ",,,,,,,,,,,,,,," 'Open the database Set dbeng = CreateObject("DAO.DBEngine.35") Set db = dbeng.OpenDatabase(strMdbFile) Set rs = db.OpenRecordset(strQuery, dbOpenDynaset) If rs.EOF And rs.BOF Then Exit Sub End If rs.MoveLast rs.MoveFirst 'Open the output text file Set fs = Wscript.CreateObject("Scripting.FileSystemObject") Set ts = fs.OpenTextFile(strTextFile, ForWriting, True) 'Read the database and write the text file! Do until rs.EOF strData = "" For intField = 0 to rs.Fields.Count - 1 strData = strData & Left(rs.Fields(intField).Value, Split(strLength,",")(intField)) If (intField < (rs.Fields.Count - 1)) Then strData = strData & Mid(strSeparator, intField + 1,1) End If Next ts.WriteLine strData rs.MoveNext Loop ts.Close rs.Close db.Close End Sub Function FileNameInThisDir(strFileName) 'As String 'Returns the complete path and file name to a file in 'the script directory. For example, "trans.log" might 'return "C:\Program Files\Scripts\Database\trans.log" 'if the script was in the "C:\Program Files\Scripts\Database" 'directory. Dim fs 'As Object Set fs = CreateObject("Scripting.FileSystemObject") FileNameInThisDir = fs.GetAbsolutePathName(fs.BuildPath(Wscript.ScriptFullName, "..\" & strFileName)) End Function Function FileNameLikeMine(strFileExtension) 'As String 'Returns a file name the same as the script name except 'for the file extension. Dim fs 'As Object Dim strExtension 'As String Set fs = CreateObject("Scripting.FileSystemObject") strExtension = strFileExtension If Len(strExtension) < 1 Then strExtension = "txt" If strExtension = "." Then strExtension = "txt" If Left(strExtension,1) = "." Then strExtension = Mid(strExtension, 2) FileNameLikeMine = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & strExtension End Function Function Dir(strFileExtension) 'As String 'Returns the complete path and file name of a file 'in the script directory with the matching extension 'Dir("mdb") might return "C:\MYDOCU~1\TestFile.mdb" Dim fs 'As Scripting.FileSystemObject Dim strDir Dim fil 'As Scripting.Files Dim fils 'As Scripting.File Set fs = CreateObject("Scripting.FileSystemObject") Set fils = fs.GetFolder(fs.BuildPath(Wscript.ScriptFullName, "..\")).Files For each fil in fils If Lcase(Right(fil.name, Len(strFileExtension) + 1)) = "." & Lcase(strFileExtension) Then strDir = fil.Path End If Next Dir = strDir End Function