当用户点击取消MS Access VBA中的参数请求input框时如何获取返回值?

我的程序将我数据库中的所有对象导出为ex​​cel文件。 这很好。 其中一些查询具有参数请求,它提供了一个input框(不是我的代码的一部分)。 当我input值时,它工作正常,但是当我点击取消,我希望能够跳过该查询,而不是出口,但相反,我得到的错误:

运行时错误“3021”:

保留错误

当我按Debug时,它会将我带到我的代码中的这一行:

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, obj.Name, path, True, obj.Name 

我明白为什么这是出口到Excel的行。

我见过类似的其他问题,但这是用户创build的input框。 我想知道是否有类似的方法,以某种方式从该参数请求input框中获取返回值,并使用该值告诉我的程序做一些事情。

此外,如果我为这些框input值,它将进入我的代码的下一部分,在那里它将30页的报告导出为单词。 有一个盒子来计算所说的页面

现在将“examplefile”输出到“example path”

有一个button是“取消”。 如果我按取消,我得到的错误:

运行时错误'2501;:

OutputTo操作已被取消

并带我到我的代码中的这一行:

 DoCmd.OutputTo acOutputReport, obj.Name, acFormatRTF, pathLabel4.Caption & "\" & obj.Name & " " & Format(Date, "yyyy-mm-dd") & ".rtf" 

我再次明白为什么,因为这是我的出口我的报告单词文件,但我怎么能检查用户是否取消或不?

这可能不是最好的答案,但错误陷印会让您select跳过查询和导出:

 On Error GoTo PROC_ERR ...'Your code PROC_ERR: IF Err.Number = 2501 Exit Sub 'assuming the rest of the code should be aborted. End If IF Err.Number = 3021 Exit Sub End If End Sub 

根据你对许多参数的评论和你最初的问题,我想到了另一种可能的解决scheme。

有一种方法可以构build一个popup的自定义参数对话框,而不是默认的。 这也应该给你更多的执行代码的灵活性,因为你可以创build一个按你想要的方式工作的取消button。 这更像是“build立一种新的forms”,我知道你不想这样做,但系统地可以像现在一样工作。 有关演练,请参阅此链接。

理想情况下,您不使用popup窗口,参数请求对话框。 他们遇到许多问题,比如遇到什么问题,如果查询没有得到它的值,查询会抛出一个错误,而且根本没有validation。

我喜欢使用Range.CopyFromRecordset将数据移动到Excel中,因为对数据的格式和数据的位置有更多的控制。

假设你的窗体上有一些combobox,其中有效的input值为combo1 。 然后当你想导出到Excel时,你可以通过点击button来调用它

 If PassesValidation(Combo1) Then Dim e As New Exporter Dim views As ADOX.views Dim cat As New ADOX.Catalog Set cat.ActiveConnection = CurrentProject.Connection Dim proc As ADOX.Procedure 'because it has an input parameter Dim cmd As adodb.Command Set proc = cat.Procedures("ParameterTestQuery") Set cmd = proc.Command cmd.Parameters("pChangeType") = Combo1 Dim rs As adodb.Recordset Set rs = cmd.Execute e.CreateExcelReport rs End If 

您的PassesValidation可以采取input参数或直接查看表单。 无论哪种方式,这一步基本上是你在做什么,而不是当用户点击input参数对话框时捕捉错误。

这个支持代码看起来像

 Public Function CreateExcelReport(ExportRecordSet As adodb.Recordset) As Excel.Workbook Dim wbExp As Excel.Workbook, wsExport As Excel.Worksheet Dim ApXL As Excel.Application Dim rsReportableErrors As adodb.Recordset On Error Resume Next Set ApXL = GetExcelApplication On Error GoTo DoStuff_Error Set wbExp = ApXL.Workbooks.Add Set wsExport = wbExp.Sheets(1) Set wsExport = MoveRecordsetToSheet(wsExport, ExportRecordSet, options) wsExport.Cells.WrapText = False Dim c As Integer For c = 0 To ExportRecordSet.Fields.Count wsExport.Columns(c).AutoFit If wsExport.Columns(c).ColumnWidth > (35) Then wsExport.Columns(c).ColumnWidth = 35 End If Next c wsExport.Cells.EntireRow.AutoFit Set CreateExcelReport = wbExp Exit_DoStuff: ApXL.Visible = True On Error GoTo 0 Exit Function End Function Private Function MoveRecordsetToSheet(sht As Excel.Worksheet, rst As adodb.Recordset) As Excel.Worksheet Set sht = MakeFirstRowFieldNames(sht, rst) sht.Range("A2").CopyFromRecordset rst sht.Columns.AutoFit Set MoveRecordsetToSheet = sht End Function Private Function MakeFirstRowFieldNames(sht As Excel.Worksheet, rst As adodb.Recordset) As Excel.Worksheet Dim fld As adodb.Field Dim col As Integer col = 1 With sht For Each fld In rst.Fields With .Cells(1, col) .Value = fld.Name .Font.Bold = True .Interior.Color = RGB(192, 192, 192) .Borders(xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous .Borders(xlEdgeBottom).Weight = XlBorderWeight.xlMedium .Borders(xlEdgeBottom).Color = RGB(0, 0, 0) End With With .Cells.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Color = RGB(255, 0, 0) .TintAndShade = 0 .Weight = xlThin End With col = col + 1 Next fld .UsedRange.AutoFilter End With Set MakeFirstRowFieldNames = sht End Function Function GetExcelApplication(Optional ByRef WasANewInstanceReturned As Boolean) As Excel.Application If ExcelInstanceCount > 0 Then Set GetExcelApplication = GetObject(, "Excel.Application") WasANewInstanceReturned = False Else Set GetExcelApplication = New Excel.Application WasANewInstanceReturned = True End If End Function Function ExcelInstanceCount() As Integer Dim objList As Object, objType As Object, strObj As String strObj = "Excel.exe" Set objType = GetObject("winmgmts:").ExecQuery("select * from win32_process where name='" & strObj & "'") ExcelInstanceCount = objType.Count End Function 

你会得到一个很好的格式化的Excel文件。 在我看来,最大的好处是,该文件被打开, 但没有保存 。 这意味着最终用户可以将其保存到任何他们想要的地方,或者如果他们不喜欢他们看到的东西,他们可以closures它,而不会留下任何杂乱的文件。

请注意,这一切都需要引用Microsoft ActiveX Data Objects Library (ADODB), Microsoft ADO Ext. for DDL and Security Microsoft ADO Ext. for DDL and Security (ADOX)和Microsoft Excel Object Library (Excel)。