简体   繁体   English

MS Access - 使用子表单过滤时禁止保存提示

[英]MS Access - Suppress Save Prompt When Using Subform Filtering

I have a form set-up that has a subform window and a few command buttons.我有一个表单设置,它有一个子表单窗口和几个命令按钮。 The subform loads in a crosstab query that allows user to manipulate the data to a desired dataset before exporting (exporting done with a command button).子表单加载在交叉表查询中,允许用户在导出之前将数据操作到所需的数据集(使用命令按钮完成导出)。 This manipulation is only to be single use and discarded when the form is closed.此操作只能一次性使用,并在表单关闭时丢弃。 When the form closes, I am being prompted to save the crosstab query.当表单关闭时,系统会提示我保存交叉表查询。 I would like to find a way to shut off this prompt and discard the changes.我想找到一种方法来关闭此提示并放弃更改。

To clarify: Form is opened, query loads in, user filters data to the data they want to view/export, when form is closed a prompt to save the query is produced.澄清:表单打开,查询加载,用户过滤数据到他们想要查看/导出的数据,当表单关闭时,会生成保存查询的提示。 This prompt appears only if a filter had been applied.仅当应用了过滤器时才会出现此提示。

I attempted to set-up a form to use as a subform initially but quickly realized that new controls will not be created when new columns are added (the whole reason why I used a crosstab).我最初尝试设置一个表单以用作子表单,但很快意识到添加新列时不会创建新控件(这就是我使用交叉表的全部原因)。 When the form is closed with the "Close Form" command button, I have the line of code当使用“关闭表单”命令按钮关闭表单时,我有一行代码

DoCmd.Close , , acSaveNo

which works perfectly.这完美地工作。 When the form is closed any other way, the prompt appears (closing the form with the "x", exiting the database, etc.).当表单以任何其他方式关闭时,会出现提示(用“x”关闭表单、退出数据库等)。 I do not want to shut off warnings as this will save the changes to the crosstab query.我不想关闭警告,因为这将保存对交叉表查询的更改。 I have been researching everywhere for a workaround but the fact that I am using and must use a crosstab query becomes a significant factor.我一直在到处研究解决方法,但我正在使用并且必须使用交叉表查询这一事实成为一个重要因素。

The code I am using to apply the crosstab query filtering to the export command:我用来将交叉表查询过滤应用于导出命令的代码:

Private Sub ExportCmd_Click()

Dim NewSQL As String
Dim strPart1 As String
Dim strPart2 As String
Dim strPart3 As String
Dim OrigSQL As String

On Error GoTo Cancelled_Export

OrigSQL = CurrentDb.QueryDefs("basicrecordextractcrosstab").SQL
If Forms("exportlogdataform").Controls("Child290").Form.Filter & vbNullString = vbNullString Then
    NewSQL = OrigSQL
Else
    strPart1 = Left(OrigSQL, InStr(1, OrigSQL, "GROUP BY", vbTextCompare) - 1)
    strPart2 = "WHERE " & Replace(Forms("exportlogdataform").Controls("Child290").Form.Filter, "[BasicRecordExtractCrosstab].", "", , , vbTextCompare)
    strPart3 = Right(OrigSQL, Len(OrigSQL) - Len(strPart1))
    NewSQL = strPart1 & strPart2 & Chr(13) & strPart3
End If

CurrentDb.QueryDefs("basicrecordextractcrosstab").SQL = NewSQL

DoCmd.OutputTo acOutputQuery, "BasicRecordExtractCrosstab", "ExcelWorkbook(*.xlsx)", "", True, "", , acExportQualityPrint

Restore_SQL_Def:

CurrentDb.QueryDefs("basicrecordextractcrosstab").SQL = OrigSQL

Exit Sub

Cancelled_Export:
    If Err = 2501 Then
        MsgBox "Export to Excel Action Cancelled", vbInformation + vbOKOnly, "Export Cancelled"
    ElseIf Err = 2302 Then
        MsgBox "Unable to save export file.  Make sure the file is not currently open.", vbExclamation + vbOKOnly, "Export Failed"
    Else
        MsgBox Err & ": " & Error$
    End If

Resume Restore_SQL_Def

End Sub

I didn't want to go this route, but I hard coded the default SQL definition and turned off warnings when the form loads.我不想走这条路,但我硬编码了默认的 SQL 定义并在表单加载时关闭了警告。 I have the warnings turning back on in the form's close event.我在表单的关闭事件中重新打开了警告。

Private Sub Form_Load()

Dim OrigSQL as String

OrigSQL = "Default SQL Def string here"

CurrentDb.QueryDefs("basicrecordextractcrosstab").SQL = OrigSQL

DoCmd.SetWarnings False

End Sub
Private Sub Form_Close()

DoCmd.SetWarnings True

End Sub

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

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