2009-02-12 41 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