从代码

时间:2018-05-09 16:18:49

标签: access-vba ms-access-2010

在代码中执行时,我无法获得三个参数查询以返回任何结果。

如果我手动执行查询,那么查询工作正常,当我将相关表,查询和VBA代码复制到新数据库中以尝试创建Minimal, Complete, and Verifiable example时,它也可以正常运行....

我的三张桌子是:

表: List_LunchPattern_Names

  • PatternID(自动编号和主键)
  • Pattern_Name(文字)

表: List_LunchPatterns

  • PatternID(数字和主键 - 长整数)
  • Pattern_Step(数字和主键 - 长整数)
  • LunchStart1LunchEnd1LunchStart2LunchEnd2(日期/时间 - 短时间)

表: tmp_LunchPatterns

  • Pattern_Step(数字和主键 - 长整数)
    预先填充数字1 - 7表示一周中的几天。
  • LunchStart1LunchEnd1LunchStart2LunchEnd2(日期/时间 - 短时间)

三个查询是:

查询: DML_Add_NewLunchPattern_Name

PARAMETERS  New_Pattern_Name Text ( 255 );
INSERT INTO List_LunchPattern_Names ( Pattern_Name )
VALUES      (New_Pattern_Name);  

查询: DML_Add_NewLunchPattern

PARAMETERS  Pattern_Identifier Long;
INSERT INTO List_LunchPatterns ( 
                PatternID, Pattern_Step, LunchStart1, 
                LunchEnd1, LunchStart2, LunchEnd2 
                                )
SELECT      Pattern_Identifier, Pattern_Step, LunchStart1
            , LunchEnd1, LunchStart2, LunchEnd2
FROM        tmp_LunchPatterns;  

查询: DML_Clear_tmp_LunchPatterns

UPDATE  tmp_LunchPatterns 
SET     LunchStart1 = NULL, LunchEnd1 = NULL
        , LunchStart2 = NULL, LunchEnd2 = NULL;  

代码位于List_LunchPatterns表单上的按钮后面:

Private Sub btnCreateLunchPattern_Click()

    Dim sResult As String
    Dim sExisting As Variant
    Dim lNewID As Long
    Dim db As DAO.Database

    On Error GoTo ERR_HANDLE

    sResult = InputBox("Please provide a unique name for the new lunch pattern.", "Lunch Patterns")
    sResult = Trim(sResult)
    If Len(sResult) = 0 Then
        'No entry.
    Else
        sExisting = DLookup("Pattern_Name", "List_LunchPattern_Names", "Pattern_Name='" & sResult & "'")
        If sExisting <> "" Then
            'Existing entry.
            MsgBox "'" & sResult & "' already exists.  Please choose another name.", vbOKOnly + vbInformation
        Else
            'Valid answer.
            Set db = CurrentDb

            'Add the new name to the pattern name list.
            'Add the temporary times into the final table.
            'Clear the temporary table, requery the combo-box for the new name
            With db
                With .QueryDefs("DML_Add_NewLunchPattern_Name")
                    .Parameters("New_Pattern_Name") = sResult
                    .Execute
                End With
                lNewID = DLookup("PatternID", "List_LunchPattern_Names", "Pattern_Name='" & sResult & "'")
                With .QueryDefs("DML_Add_NewLunchPattern")
                    .Parameters("Pattern_Identifier") = lNewID
                    .Execute
                End With
                .QueryDefs("DML_Clear_tmp_LunchPatterns").Execute
            End With

            With Me
                .cmbPattern_Selector.Requery
                .cmbPattern_Selector = lNewID
                .RecordSource = "List_LunchPatterns"
                FilterForm Me, "PatternID=" & .cmbPattern_Selector
            End With

        End If
    End If

EXIT_PROC:

        On Error GoTo 0
        Exit Sub

ERR_HANDLE:
        DisplayError Err.Number, Err.Description, "Form_List_LunchPatterns.btnCreateLunchPattern_Click()"
        Resume EXIT_PROC

End Sub  

代码底部使用的FilterForm过程是:

Public Sub FilterForm(frm As Object, FilterString As String)

    On Error GoTo ERR_HANDLE

    With frm
        .Filter = FilterString
        .FilterOn = True
    End With

EXIT_PROC:
        On Error GoTo 0
        Exit Sub

ERR_HANDLE:
    Select Case Err.Number

    Case Else
        DisplayError Err.Number, Err.Description, "mdl_FormFilter.FilterForm()"
        Resume EXIT_PROC
    End Select

End Sub

问题:

如果我将代码的QueryDefs部分和所有表/查询复制到新数据库并运行它,它工作正常,如果我手动运行每个查询它工作正常,如果我使用上面的代码块运行它{ {1}}工作正常,但第二个查询仅部分有效,而最后一个查询根本没有 第二个查询使用正确的DML_Add_NewLunchPattern_Name添加七个记录,但是开始和结束时间都是空白,即使七个记录必须来自包含预期的开始/结束时间的临时表。

我有什么明显的遗失吗?

编辑:

在努力保持我的问题最小化时,看起来我错过了一个非常重要的部分 PatternID表用于填充表单上的组合框,该组合框将List_LunchPattern_Names显示的记录过滤到相关模式。
组合框的List_LunchPatterns为:

Row Source

组合框的第一个选项是创建一个新模式。选择此选项会将表单SELECT DISTINCT 0 AS PatternID , '<New Pattern>' AS Pattern_Name FROM SingleRecord UNION SELECT PatternID, Pattern_Name FROM List_LunchPattern_Names ORDER BY PatternID 更改为临时表,以便在最终确定之前输入新模式并对照现有模式进行检查。
这个代码在下面,我认为问题是表单需要刷新或重新查询来完成记录源的更改。

RecordSource

编辑2:

这是为了确保我首先正确测试所有内容。

它只是我添加到临时表中的最后一条记录在更新查询期间被忽略 - 我没有在早期测试中看到这一点,因为我只添加了一条记录。例如,将时间添加到星期一和星期二将仅使用星期一创建模式,而星期二则保留在临时表中 - 即使在我执行查询以清除表格之后。 这让我相信,当我退出控件一段时间后,点击完成模式的按钮,直到点击事件结束后才将记录添加到表中,所以在点击事件开始时我应该Private Sub cmbPattern_Selector_AfterUpdate() On Error GoTo ERR_HANDLE With Me 'If the selected pattern is different from the currently 'active pattern unhide the Assign Pattern button. If Not IsNull(OpenArgs) Then If .cmbPattern_Selector <> Split(OpenArgs, "|")(2) Then .btnAssignLunchPattern.Visible = True End If End If If .cmbPattern_Selector <> 0 Then .cmbPattern_Selector.SetFocus .btnCreateLunchPattern.Visible = False .RecordSource = "List_LunchPatterns" FilterForm Me, "PatternID=" & .cmbPattern_Selector Else .btnCreateLunchPattern.Visible = True .RecordSource = "tmp_LunchPatterns" .btnAssignLunchPattern.Visible = False End If End With EXIT_PROC: On Error GoTo 0 Exit Sub ERR_HANDLE: DisplayError Err.Number, Err.Description, "Form_List_LunchPatterns.cmbPattern_Selector_AfterUpdate()" Resume EXIT_PROC End Sub 或保存记录.....

0 个答案:

没有答案