VBA DoCmd.TransferText-使用用户定义的文件路径将查询导出为.csv
目前我的代码是:VBA DoCmd.TransferText-使用用户定义的文件路径将查询导出为.csv,vba,Vba,目前我的代码是: Dim testSQL As String Dim qd As DAO.QueryDef testSQL = "SELECT * FROM qryExample WHERE exampleID IN (" & strExampleIDList & ")" Set qd = db.CreateQueryDef("tmpExport", testSQL) DoCmd.TransferText acExportDelim, , "tmpExport", "C:\ex
Dim testSQL As String
Dim qd As DAO.QueryDef
testSQL = "SELECT * FROM qryExample WHERE exampleID IN (" & strExampleIDList & ")"
Set qd = db.CreateQueryDef("tmpExport", testSQL)
DoCmd.TransferText acExportDelim, , "tmpExport", "C:\export.csv"
db.QueryDefs.Delete "tmpExport"
如何更改“C:\export.csv”部分,以便用户能够定义文件路径和文件名
谢谢。如果您希望提示用户输入,然后在TransferText调用中使用该输入,请尝试以下操作:
Dim UserInput As String
UserInput = InputBox("Please enter the file path.", "I WANT A VALUE!")
DoCmd.TransferText acExportDelim, , "tmpExport", UserInput
还有其他方法,但这可能是最容易实现的方法
祝您好运。此示例将允许您使用filedialog另存为对象: 若要使用此函数,必须添加对“Microsoft Office XX.0对象库”的引用。添加新模块并粘贴以下功能:
Public Sub exportQuery(exportSQL As String)
Dim db As DAO.Database, qd As DAO.QueryDef
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogSaveAs)
Set db = CurrentDb
'Check to see if querydef exists
For i = 0 To (db.QueryDefs.Count - 1)
If db.QueryDefs(i).Name = "tmpExport" Then
db.QueryDefs.Delete ("tmpExport")
Exit For
End If
Next i
Set qd = db.CreateQueryDef("tmpExport", exportSQL)
'Set intial filename
fd.InitialFileName = "export_" & Format(Date, "mmddyyy") & ".csv"
If fd.show = True Then
If Format(fd.SelectedItems(1)) <> vbNullString Then
DoCmd.TransferText acExportDelim, , "tmpExport", fd.SelectedItems(1), False
End If
End If
'Cleanup
db.QueryDefs.Delete "tmpExport"
db.Close
Set db = Nothing
Set qd = Nothing
Set fd = Nothing
End Sub
谢谢,这比我之前的要好-但是,我正在寻找用户通常用于执行此类操作的windows资源管理器框。啊,然后你想使用FileDialog--看这里:找到了!非常感谢你!没问题,很高兴我能帮忙!
Public Sub someButton_Click()
Dim queryStr as String
'Store Query Here:
queryStr = "SELECT * FROM..."
Call exportQuery(queryStr)
End Sub