在MSAccess中,我有一个带按钮的面具。当用户单击该按钮时,表中的数据将导出到.txt文件中:
Private Sub Command_Click()
Dim Rst As DAO.Recordset
Dim AField As DAO.Field
Dim TempStr As String
Dim FileNumber
FileNumber = FreeFile
Open "c:\table.txt" For Output As #FileNumber
Set Rst = CurrentDb.OpenRecordset("Tabella1", dbOpenForwardOnly)
Do While Not Rst.EOF
For Each AField In Rst.Fields
If (AField.Name <> "ID") Then
TempStr = TempStr & AField.value & " "
End If
Next
Print #FileNumber, Left(TempStr, Len(TempStr) - 1)
TempStr = ""
Rst.MoveNext
Loop
Rst.Close
Set Rst = Nothing
Close #FileNumber
End Sub
它可以工作,但我会显示一个“另存为...”对话框,允许用户选择导出数据的文件。
有可能吗?
答案 0 :(得分:6)
您可以设置对Microsoft Office x.x对象库的引用,并使用FileDialog。
Sub ShowFileDialog()
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogSaveAs)
With dlgOpen
.InitialFileName = "Z:\docs\"
.Show
End With
End Sub
另外:How do I get a single file name out of a File Dialog object in VBA (for MS Access 2007)?
答案 1 :(得分:1)
有关API解决方案,请参阅此处: http://access.mvps.org/access/api/api0001.htm
适合我。 只需复制并粘贴到新模块中即可。