2009-02-12 39 views
4

我正在使用宏将Microsoft Access数据库中的表导出到csv文件以导入到mysql数据库中。我最终使用了一个批处理文件,在导出之前在文本文件中放置一个标记,然后将最后一个标记之后的所有内容放到一个新文件中。这很好,除了访问不会追加,但每次都会重新创建文件,所以不可能使用任何类型的标记。将数据从不断追加的文件分离到新文件中

有什么办法,使用访问或批处理文件或其他方式:a)强制访问附加到文件,或放置自己的标记,或b)每次可能导出到不同的文件文件名是一个这样的可变的日期,或c)克服与外部操纵此行为

+0

是否有某些原因导致您无法使用MyODBC驱动程序,并直接从Access执行此操作?此外,从您的问题中可以看出,在您要导入到MySQL的CSV文件中,您经常需要的是旧数据的情况。 – 2009-02-13 01:21:00

回答

3

而不是使用宏来导出表,您可以简单地创建一些代码来打开文件,并将数据附加到它。

如何使用

只要将代码复制到VBA模块中的应用程序,并调用它像这样:

' Export the Table "Orders" to "orders.csv", appending the data to the  ' 
' existing file if there is one.            ' 
ExportQueryToCSV "Orders", "C:\orders.csv", AppendToFile:=True 

' Export the result of the query to "stock.csv" using tabs as delimiters  ' 
' and no header or quotes around strings          ' 
ExportQueryToCSV "SELECT * FROM Stock WHERE PartID=2", _ 
       "C:\stock.csv", _ 
       AppendToFile:=False, _ 
       IncludeHeader:=False, _ 
       Delimiter:=chr(9), _ 
       QuoteString:=false 

代码

'----------------------------------------------------------------------------' 
' Export the given query to the given CSV file.        ' 
'                   ' 
' Options are:                ' 
' - AppendToFile : to append the record to the file if it exists instead of ' 
'     overwriting it (default is false)       ' 
' - Delimiter : what separator to use (default is the coma)    ' 
' - QuoteString : Whether string and memo fields should be quoted   ' 
'     (default yes)            ' 
' - IncludeHeader: Whether a header with the field names should be the first ' 
'     line (default no)           ' 
' Some limitations and improvements:           ' 
' - Memo containing line returns will break the CSV       ' 
' - better formatting for numbers, dates, etc        ' 
'----------------------------------------------------------------------------' 
Public Sub ExportQueryToCSV(Query As String, _ 
          FilePath As String, _ 
          Optional AppendToFile As Boolean = False, _ 
          Optional Delimiter As String = ",", _ 
          Optional QuoteStrings As Boolean = True, _ 
          Optional IncludeHeader As Boolean = True) 
    Dim db As DAO.Database 
    Dim rs As DAO.RecordSet 

    Set db = CurrentDb 
    Set rs = db.OpenRecordset(Query, dbOpenSnapshot) 
    If Not (rs Is Nothing) Then 
     Dim intFile As Integer 

     ' Open the file, either as a new file or in append mode as required ' 
     intFile = FreeFile() 
     If AppendToFile And (Len(Dir(FilePath, vbNormal)) > 0) Then 
      Open FilePath For Append As #intFile 
     Else 
      Open FilePath For Output As #intFile 
     End If 

     With rs 
      Dim fieldbound As Long, i As Long 
      Dim record As String 
      Dim field As DAO.field 

      fieldbound = .Fields.count - 1 

      ' Print the header if required ' 
      If IncludeHeader Then 
       Dim header As String 
       For i = 0 To fieldbound 
        header = header & .Fields(i).Name 
        If i < fieldbound Then 
         header = header & Delimiter 
        End If 
       Next i 
       Print #intFile, header 
      End If 

      ' print each record' 
      Do While Not .EOF 
       record = "" 
       For i = 0 To fieldbound 
        Set field = .Fields(i) 
        If ((field.Type = dbText) Or (field.Type = dbMemo)) And QuoteStrings Then 
         record = record & """" & Nz(.Fields(i).value, "") & """" 
        Else 
         record = record & Nz(.Fields(i).value) 
        End If 
        If i < fieldbound Then 
         record = record & Delimiter 
        End If 
        Set field = Nothing 
       Next i 
       Print #intFile, record 
       .MoveNext 
      Loop 
      .Close 
     End With 
     Set rs = Nothing 
     Close #intFile 
    End If 
    Set rs = Nothing 
    Set db = Nothing 
End Sub 

请注意,这是不完美,您可能需要修改代码以反映您希望如何格式化数据,但在大多数情况下,默认值应该没问题。

1

一个)力访问追加到文件中,或将其自身

号的标志物的出口是期望每次都写一个全新的文件,并且如果该文件已经存在就会出现问题。每次

b)出口到不同的文件,可能的文件名是一个变量,如日期

是,建立的路径/文件名起来作为一个字符串,以及日期/时间追加到尾部

C)克服与外部操纵这种行为

嗯,你可以转储出来有什么新的虚拟文件,然后用一个批处理脚本或系统调用,它:

echo marker >> goodfile.csv 
type dummy >> goodfile.csv 
del dummy 

但是,如果您只是想添加新记录,则更好的方法是只处理虚拟文件,而不是尝试查找最后一个标记并处理低于此的所有内容。

0

您也可以使用VBScript来执行您用于导出的查询,并将这些记录附加到现有文件。

+0

或者用vbscript我甚至可以在每次我猜的时候输入一个新文件?问题是,我最后一次使用vbscript进行尝试时,输出与宏无法正确输出。 – 2009-02-13 12:23:43

+0

是的,你可以。 Access宏的一个问题是它们确实不那么灵活;你几乎局限于你可以用GUI做什么。VBA模块和/或VBScript脚本为您提供更多的功能,但复杂性更高。 – 2009-02-13 12:40:48