我整理了一些看起来像ti应该起作用的代码,以将表中的每个唯一记录导出为单独的CSV文件,但实际上是将所有记录导出为CSV文件。这必须很近,但是有些地方不对劲,我不知道是什么。
Private Sub Command0_Click()
Dim dbThis As Database
Dim rsThis As Recordset
Dim qdfNew As QueryDef
Set dbThis = CurrentDb()
Set rsThis = dbThis.OpenRecordset("Select Distinct OrderID as dValue from Orders where OrderID is not null", dbOpenSnapshot)
Do While Not rsThis.EOF
Debug.Print rsThis.Fields("dValue").Value
Set qdfNew = New QueryDef
qdfNew.SQL = "select * from Orders where OrderID = '" & rsThis.Fields("dValue").Value & "'"
qdfNew.Name = "qryTemp"
dbThis.QueryDefs.Append qdfNew
DoCmd.TransferText acExportDelim, , "Orders", "C:\Test\" & rsThis.Fields("dValue").Value & ".csv", True
dbThis.QueryDefs.Delete "qryTemp"
rsThis.MoveNext
Loop
rsThis.Close
End Sub
答案 0 :(得分:1)
啊,现在我明白了问题所在。我有一个数字字段周围的引号。是的,文字需要加引号,数字则不需要。如果您要过滤数字字段,这是可行的解决方案。
Set dbThis = CurrentDb()
Set rsThis = dbThis.OpenRecordset("Select Distinct OrderID as dValue from Orders where OrderID is not null", dbOpenSnapshot)
Do While Not rsThis.EOF
Debug.Print rsThis.Fields("dValue").Value
Set qdfNew = New QueryDef
qdfNew.SQL = "select * from Orders where OrderID = " & rsThis.Fields("dValue").Value & ""
qdfNew.Name = "qryTemp"
dbThis.QueryDefs.Delete "qryTemp"
dbThis.QueryDefs.Append qdfNew
qryTemp = qdfNew.SQL
DoCmd.TransferText acExportDelim, , "qryTemp", "C:\Test\" & rsThis.Fields("dValue").Value & ".csv", True
rsThis.MoveNext
Loop
rsThis.Close
End Sub