“Save as…” dialog box in MSAccess vba: how?

余生颓废 提交于 2019-12-18 07:25:09

问题


In MSAccess I've a mask with a button. When the user clicks on the button, the data in a table are exported on a .txt file:

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

It works, but I would display a "Save as..." dialog box by allowing the user to choose the file on which export the data.

Is it possible?


回答1:


You can set a reference to the Microsoft Office x.x Object Library and use FileDialog.

FileDialog Properties

Sub ShowFileDialog()
    Dim dlgOpen As FileDialog
    Set dlgOpen = Application.FileDialog(msoFileDialogSaveAs)
    With dlgOpen
        .InitialFileName = "Z:\docs\"
        .Show
    End With
End Sub

Also: How do I get a single file name out of a File Dialog object in VBA (for MS Access 2007)?




回答2:


For an API solution see here : http://access.mvps.org/access/api/api0001.htm

Works well for me. Simply copy and paste in a new module.



来源:https://stackoverflow.com/questions/11743224/save-as-dialog-box-in-msaccess-vba-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!