繁体   English   中英

如何使用以下VBA代码在表单上创建一个命令按钮,该命令按钮生成“另存为”对话框并导出Access查询?

[英]How do I use the following VBA code to create a command button on my form that produces “Save As” Dialog Box and exports Access queries?

这个问题与以下两个帖子有关:

按下按钮时如何运行查询并将结果导出到Excel文件

MSAccess vba中的“另存为...”对话框:如何?

我目前有一个参数化查询(可称为“ qryExport”),该查询从表单上的某些控件(称为“ Form A”)中获取值。 从上述链接中,我了解到我可以对“ Form A”命令按钮的“ on click”事件使用以下代码行,该事件可以将“ qryExport”导出为Excel文件,位于固定文件路径位置。

DoCmd.TransferSpreadsheet acExport, , "qryExport", "C:\yourPath\exportedReport.xlsm", True

问题

但是,我需要为“表单A”产生一个命令按钮,当用户单击它时,它将执行以下操作:

1)提示用户命名即将导出的文件。

2)允许用户指定要在其计算机上保存导出的“ qryExport”对象的位置。

3)允许用户选择他们想要导出“ qryExport”的文件类型(例如Excel,XML,Txt等)。

4)一旦用户选择了文件路径名称,文件名和所需的文件类型,便执行导出操作。

我将把表单分发给多个用户(他们在不同的工作站上工作),这需要我的命令按钮满足上述要求。

我认为一种可能的解决方案是在用户单击命令按钮时添加“另存为”提示。 它将要求用户指定文件路径,选择他们希望将文件保存为工作站中的文件格式(Excel,XML,Txt等),并允许他们为新文件命名。

我发现了VBA代码,该代码允许命令按钮生成“另存为”窗口(值得注意的是,它仅向用户提供将文件另存为Excel文件的选项)(请参见链接下方的代码):

VBA另存为原点

Option Compare Database
Private mstrFileName As String
Private mblnStatus As Boolean
'Declare needed functions
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" _
(pOpenfilename As OPENFILENAME) As Long

Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" _
(pOpenfilename As OPENFILENAME) As Long

'Declare OPENFILENAME custom Type
Private Type OPENFILENAME
  lStructSize As Long
  hwndOwner As Long
  hInstance As Long
  lpstrFilter As String
  lpstrCustomFilter As String
  nMaxCustFilter As Long
  nFilterIndex As Long
  lpstrFile As String
  nMaxFile As Long
  lpstrFileTitle As String
  nMaxFileTitle As Long
  lpstrInitialDir As String
  lpstrTitle As String
  Flags As Long
  nFileOffset As Integer
  nFileExtension As Integer
  lpstrDefExt As String
  lCustData As Long
  lpfnHook As Long
  lpTemplateName As String
End Type

'Function needed to call the "Save As" dialog
Public Function SaveFileDialog(lngFormHwnd As Long, _
lngAppInstance As Long, strInitDir As String, _
strFileFilter As String) As Long

Dim SaveFile As OPENFILENAME
Dim X As Long

If IsMissing(strFileName) Then strFileName = ""

With SaveFile
 .lStructSize = Len(SaveFile)
 .hwndOwner = lngFormHwnd
 .hInstance = lngAppInstance
 .lpstrFilter = strFileFilter
 .nFilterIndex = 1
 .lpstrFile = String(257, 0)
 'Use for a Default File SaveAs Name - [UD]
 '.lpstrFile = "testfile.txt" & String(257 - Len("testfile.txt"), 0)
 .nMaxFile = Len(SaveFile.lpstrFile) - 1
 .lpstrFileTitle = SaveFile.lpstrFile
 .nMaxFileTitle = SaveFile.nMaxFile
 .lpstrInitialDir = strInitDir
 .lpstrTitle = "Enter a Filename to Save As"        '[UD]
 .Flags = 0
 .lpstrDefExt = ".xls"   'Sets default file extension to Excel,
                         'in case user does not type it - [UD]
End With

X = GetSaveFileName(SaveFile)

If X = 0 Then
  mstrFileName = "none"
  mblnStatus = False
Else
  mstrFileName = Trim(SaveFile.lpstrFile)
  mblnStatus = True
End If
End Function
Public Property Let GetName(strName As String)
  mstrFileName = strName
End Property
Public Property Get GetName() As String
  GetName = mstrFileName
End Property
Public Property Let GetStatus(blnStatus As Boolean)
  mblnStatus = blnStatus
End Property
Public Property Get GetStatus() As Boolean
  GetStatus = mblnStatus
End Property

但是,该代码无法满足我的所有需求。 我不知道如何使用所有这些代码来在表单上生成所需的命令按钮。

和往常一样,谢谢您的时间。

请注意:我正在使用Access 2010

您是否看过FileDialog选项的功能? 您可以使用此功能完成您提到的所有操作; 不需要WinAPI的东西。 真正的问题是DoCmd.TransferSpreadsheet命令只会生成一个Excel文件。

如果要创建其他文件类型,则需要:

  • 创建FileDialog对象set fd = Application.FileDialog(msoFileDialogFilePicker)
  • 设置FileDialog对象
    • 从对象fd.Filters.Clear清除以前的扩展
    • 提供可用的扩展名fd.Filters.Add "Query Output", "*.xls; *.txt; *.xml"
    • 在这种情况下关闭多fd.AllowMultiSelect = False
    • 提供默认文件名fd.InitialFilename = "<desired start path>\\<desired filename>.<desired extension>"
  • .Show其显示给用户并返回fd.Show的结果
    • If fd.Show Then (它们没有按If fd.Show Then或关闭对话框)(如果按保存则Show = true)
    • 查看他们为下一步选择的文件名的扩展名
      • If Instr(fd.SelectedItems(0), ".xls") > 0 Then 'Do the xls stuff
      • If Instr(fd.SelectedItems(0), ".xml") > 0 Then 'Do the text stuff
      • Else 'Do the xml stuff
    • 将查询的输出解析为用户选择的格式
      • 通过解析查询结果的内容来创建文本文件或XML。
      • 使用TransferSpreadsheet命令创建XLS。

该命令不仅会创建所需的文件类型。 您需要控制它。
阅读此链接可获取2013年和2010年的更多信息。
阅读此链接以获得对FileDialog对象属性的更好描述。

要使其成为命令按钮单击事件的一部分,只需在命令按钮的单击事件过程中为表单编写代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM