views:

1832

answers:

4

I have an Access database which I would like to export to a text file. I have a schema defined within Access, and currently use a macro to export it. I would like to use VBScript to always append the result of a query to the same file. If it is not possible to use my defined schema, I only need the fields to be comma separated and enclosed by the ", and the text file must be in UTF-8 format.

I found the following code snippet, but I am unsure how to adopt it for my needs.

db = "C:\Docs\LTD.mdb"
TextExportFile = "C:\Docs\Exp.txt"

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open _
   "Provider = Microsoft.Jet.OLEDB.4.0; " & _
   "Data Source =" & db

strSQL = "SELECT * FROM tblMembers"

rs.Open strSQL, cn, 3, 3

Set fs = CreateObject("Scripting.FileSystemObject")

Set f = fs.CreateTextFile(TextExportFile, True)

a = rs.GetString

f.WriteLine a

f.Close
+1  A: 

Perhaps the easiest way is to use [text...].filename approach:-

Dim db: db = "C:\Docs\LTD.mdb"
Dim exportDir: exportDir = "C:\Docs\" '" SO prettify does not do VB well
Dim exportFile: exportFile = "Exp.txt"

Dim cn: Set cn = CreateObject("ADODB.Connection")

cn.Open _
    "Provider = Microsoft.Jet.OLEDB.4.0; " & _
    "Data Source =" & db

cn.Execute "SELECT * INTO [text;HDR=Yes;Database=" & exportDir & _
   ";CharacterSet=65001]." & exportFile & " FROM tblMembers"

FileSystemObject won't help you since it doesn't do UTF-8. UTF-8 is acheived by specifying CharacterSet=65001 (65001 is the UTF-8 codepage). Note the file generated does not contain a UTF-8 BOM but the schema.ini file created will note that the CharacterSet is UTF-8.

Note this doesn't achieve your append requirements are you sure that makes sense anyway, won't you end up with lots of duplicates?

Edit:

The above is adjusted to include the UTF-8 requirement. You can simply append something like the date to create multiple snapshot files for the table.

AnthonyWJones
I will be chopping the appended data into a new file, unless I can write to a unique filename(date?) each time? Can you explaint the last line a bit further? Is there no way to force utf-8 with vbs? Thanks for your help so far :)
Jason Stanthorp
I've tweaked the answer solving the UTF-8 requirement. You can simply write to a unique file name each time. Note that if you subsquently delete a file you need to update the shema.ini. Alternatively you might be able to use a Text provider to DROP the file, which will update the schema for you.
AnthonyWJones
Please excuse me adding your code to my post, it is to illustrate the new file name to the OP. I have acknowledged the source.
Remou
Thank you very much Anthony.
Jason Stanthorp
A: 

I've numbered the lines for reference.

1. db = "C:\Docs\LTD.mdb"
2. TextExportFile = "C:\Docs\Exp.txt"
3. strSQL = "SELECT * FROM tblMembers"
4. Set f = fs.CreateTextFile(TextExportFile, True)

Line 1 - is the current access database file you are working with. this case it's LTD.mdb
Line 2 - is the name of the file that you are going to write/append. It's Exp.txt
Line 3 - is the sql statement that will be used to collect the data.
Line 4 - is the command to open the file to write to.

Change line 2 to the name of file you want.
Change line 3 to the table you want to use. Select * will use all the columns if you want only a couple identify them by name. select col1, col2 ... from mytable. You will want to look into using where clauses also.

Change line 4 from CreateTextFile to OpenTextFile and use ForAppending to append. MSDN VBA

I'm drawing a blank on formatting the line. One of the ways I use is modify the select statement to include commas. example select col1 & "," & col2 from mytable.

For UTF-8 (I don't have a working example) Try:

utf = new String(a, 0, a.length, UTF-8);
f.WriteLine utf;

UTF-8 VBA

jim
+1  A: 

DIRECTION (2) This is some VBA, run from the Access database:

Sub InsertRecs()
Set db = CurrentDb

'DSN=Suitable system DSN for MySQL
'Then, depending on your set up, you can incude:
'Database=DBName;
'Trusted_Connection=Yes;

'NameOfMySQLTable
strSQL = "INSERT INTO [ODBC;DSN=baywotch;].tblAuction Select * FROM tblAuction;"

db.Execute strSQL, dbFailOnError
End Sub

This is the same thing, but in VBScript, using DAO:

Dim objEngine
Dim objWS
Dim objDB
Dim db: db = "C:\Docs\baywotch.db5"

Set objEngine = wscript.CreateObject("DAO.DBEngine.36")

Set objDB = objEngine.OpenDatabase(db)

objDB.Execute "INSERT INTO [ODBC;DSN=baywotch].[tblAuction] SELECT * FROM tblAuction;"

DIRECTION (1)

I suggest a completely different direction, and that is to let MySQL do the work:

MySQL Migration Toolkit

I tested this against your database, and it appears to import correctly, only takes a few minutes, and will generate all sorts of reusable scripts and so on.

If you are having problems with the set-up of MySQL, you may wish to read: 9.1.4. Connection Character Sets and Collations

DiRECTION (0)

REWRITE (2)

'========================================================================'
'
'                   FROM: AnthonyWJones, see post ' 
'
'========================================================================'
Dim db: db = "C:\Docs\baywotch.db5"
Dim exportDir: exportDir = "C:\Docs\" '" SO prettify does not do VB well
Dim exportFile: exportFile=NewFileName(exportDir)


Dim cn: Set cn = CreateObject("ADODB.Connection")

cn.Open _
    "Provider = Microsoft.Jet.OLEDB.4.0; " & _
    "Data Source =" & db

cn.Execute "SELECT * INTO [text;HDR=Yes;Database=" & exportDir & _
   ";CharacterSet=65001]." & exportFile & " FROM tblAuction"

'Export file

'========================================================================'

'Support functions

Function NewFileName(ExportPath)
Dim fs 
Dim NewFileTemp

Set fs = CreateObject("Scripting.FileSystemObject")

NewFileTemp = "CSV" & Year(Date) _
    & Right("00" & Month(Date),2) & Right("00" & Day(Date) ,2) & ".csv"

a = fs.FileExists(ExportPath & NewFileTemp)

i = 1
Do While a
    NewFileTemp = "CSV" & Year(Date) _
        & Right("00" & Month(Date),2) & Right("00" & Day(Date) ,2) & "_" & i & ".csv"

    a = fs.FileExists(ExportPath & NewFileTemp)
    i = i + 1
    If i > 9 Then
        'Nine seems enough times per day to be 
        'exporting a table
        a = True
        MsgBox "Too many attempts"
        WScript.Quit
    End If
Loop

NewFileName = NewFileTemp
End Function
Remou
I found it on this site :)ahh, forget schema, i meant specification, I am sorry for the confusion. I only meant I wanted text transferred as per my specificaition. Is there a way to generate the filename from the date?
Jason Stanthorp
Yes, see my edit.
Remou
I am unsure how to ty the two examples together.., how do you specify the query and where it should be written to?
Jason Stanthorp
I have rewritten my example.
Remou
Very nice :) However it is throwing an error that the csv file is not found. I am unsure why this should be, as it can not find something that is not yet created?
Jason Stanthorp
This is tested code. What changes did you make to suit your system?
Remou
Hi Remou, I only changed the path of my database and the path to write to. I now get an error that the insert into statement has an unknown fieldname. I then tried removing the scheme stuff as I did not think it was relevant.
Jason Stanthorp
I have uploaded the error message and resultant file in a zip at http://www.yousendit.com/download/U0d4R0lUMGN0TW5IRGc9PQ
Jason Stanthorp
Remou
Even using the original code as above with only the path and db changed, I still get the same error.
Jason Stanthorp
Can you post a sample database?
Remou
yup, http://www.sendspace.com/file/34q0eo although the extension is db5 it is definitely an ms access database.
Jason Stanthorp
That file is password protected. I am surprised you got anywhere without adding the password to the connection string.
Remou
how odd. I have no idea why that file is password protected or what the password is. I have uploaded the actual database I am currently using, which is definitely not password protected, but is 27mb. http://www.sendspace.com/file/9lqcp2
Jason Stanthorp
The change belongs to AnthonyWJones, please acknowledge him.
Remou
Hi Remou, This seems just to output all the column names, without any of the actual data.
Jason Stanthorp
I have tested this with the database you sent, and it outputs a full file for me, thought it looks a little odd because of the encoding. I have changed the extension back to db5, and it still works.
Remou
Here is a resultant csv file: http://www.sendspace.com/file/lxmomk the data does not seem to be exported as utf-8, umlauts and such do not display correctly, no matter what I open the csv file with.
Jason Stanthorp
I think certain characters must be escaped with / as some data is html and contains quotes and commas and as such will not import correctly into mysql. Is it possible to remove the column names at the start of the file? Perhaps this is causing part of the problem.
Jason Stanthorp
Is it possible to have a 0 before the month or day in the filename?
Jason Stanthorp
I have suggested a new approach and added a change that will include zeros in the file name.
Remou
I considered the migration toolkit, but it is not useful for the situation I the db5 file is genereated nightly and cannot be directly changed, automating all of this via the migration toolkit is not the easiest way. Did umlauts and such get exported in the csv file for you?
Jason Stanthorp
Umlats got exported without any special attention to character sets. I am suggestion another possible approach.
Remou
Hi Remou, sorry for the very long reply. If the file is exported as utf8, then umlauts should appear. This is not a mysql problem, but an exporting problem. No matter what I check with, the resulting csv file does not contain any umlauts, are you saying your tests do?
Jason Stanthorp
Yes, I am. My text files contain umlauts.
Remou
hmmm, as do mine apparently. Must have been a windows problem. This is a perfect solution, due to the unique constrictions of what I need to do.
Jason Stanthorp
How would you remove the column names from the start of the file?
Jason Stanthorp
Which 'direction' heading? It may just be HDR=No.
Remou
I just mean in the csv file, the first lines are the names of the columns and not data, which hampers importing automatically. I can not see which part of the code is doing this.
Jason Stanthorp
nevermind. Thankyou for your assistance with this.
Jason Stanthorp
the column names are still exported before the data in the csv file even if I set HDR=No.
Jason Stanthorp
If you choose hdr=no do column names not get exported at the beginning of the resultant csv file?
Jason Stanthorp
A: 
Tester101
What about text lines with commas? CSV normally quotes text.
Remou
In that case he would have to loop through each field of each record and create the csv file the long way. Or don't put commas in the database.
Tester101