从代码执行时,INSERT 和 UPDATE 参数查询不起作用
INSERT and UPDATE Parameter queries not working when executed from code
在代码中执行时,我无法获得 return 任何结果的三个参数查询。
如果我手动执行查询,查询工作正常,当我将相关的 tables、查询和 VBA 代码复制到新数据库中以尝试创建 Minimal, Complete, and Verifiable example它也很好用....
我的三个 table 是:
Table: List_LunchPattern_Names
PatternID
(自动编号和主键)
Pattern_Name
(文字)
Table: List_LunchPatterns
PatternID
(数字和主键 - 长整型)
Pattern_Step
(数字和主键 - 长整型)
LunchStart1
、LunchEnd1
、LunchStart2
和 LunchEnd2
(Date/Time - 短时间)
Table: tmp_LunchPatterns
Pattern_Step
(数字和主键 - 长整型)
预填充数字 1 - 7 表示一周中的几天。
LunchStart1
、LunchEnd1
、LunchStart2
和 LunchEnd2
(Date/Time - 短时间)
三个查询是:
查询: 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 部分和所有 tables/queries 复制到一个新数据库并且 运行 它工作正常,如果我手动 运行 每个查询它工作正常,如果我 运行 它使用上面的代码块然后 DML_Add_NewLunchPattern_Name
工作正常,但第二个查询仅部分工作而最后一个根本不工作。
第二个查询使用正确的 PatternID
添加了 7 条记录,但是开始和结束时间是空白的,即使这 7 条记录一定来自包含预期的 start/end 时间的临时 table .
有什么明显的我遗漏的吗?
编辑:
为了尽量减少我的问题,我似乎错过了一个非常重要的部分。
List_LunchPattern_Names
table 用于填充表单上的组合框,该组合框将显示的记录从 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
更改为临时 table,因此可以输入新模式并在最终确定之前对照现有模式进行检查。
代码如下,我 认为 问题是表单需要刷新或重新查询才能完成记录源的更改。
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
编辑 2:
这更像是一种练习,以确保我首先正确地测试了所有内容。
这只是我添加到临时 table 的最后一条记录在更新查询期间被忽略了 - 我在之前的测试中没有看到这一点,因为我只添加了一条记录。例如,将时间添加到星期一和星期二将创建一个仅使用星期一的模式,而星期二仍保留在临时 table - 即使在我执行查询以清除 table.
之后
这让我相信,当我在一段时间内退出控件以单击确定模式的按钮时,记录不会添加到 table 直到单击事件完成之后,所以在单击事件我应该 DoEvents
或保存记录.....
事实证明答案很简单,有点明显,而且在我第一次迭代问题时不太可能得到答案,因为我遗漏了关键点(请参阅问题中的编辑)。
场景是一个 Continuous Form
链接到一个 table,其中包含七个空记录,主键除外,列出了 1 到 7 的数字范围。
用户将数据输入到这些记录中的一个或多个,然后单击 保存 按钮,将输入的所有记录传输到最终 table。
这里的问题是你输入数据的控件和按钮在同一个窗体上,所以当你点击按钮时最后输入的记录不会被保存。
在查找事件顺序来解释这一点时,我发现了这一点:
Q1. If data is entered in one or more fields on the main form and the
user then clicks the button that opens the message box, is the record
saved?
No, if the button is on the main form. However, you sometimes
want to save a record (perhaps to print the values) so in the OnClick
event you'd have something like If Me.Dirty then Me.Dirty = False in
order to force a save.
所以答案是添加
If Me.Dirty Then Me.Dirty = False
到 btnCreateLunchPattern_Click()
过程的开始。
鉴于此 - 这个问题现在不适合回答。我应该删除问题吗?改写它以使其符合答案?在 meta 上提问?
在代码中执行时,我无法获得 return 任何结果的三个参数查询。
如果我手动执行查询,查询工作正常,当我将相关的 tables、查询和 VBA 代码复制到新数据库中以尝试创建 Minimal, Complete, and Verifiable example它也很好用....
我的三个 table 是:
Table: List_LunchPattern_Names
PatternID
(自动编号和主键)Pattern_Name
(文字)
Table: List_LunchPatterns
PatternID
(数字和主键 - 长整型)Pattern_Step
(数字和主键 - 长整型)LunchStart1
、LunchEnd1
、LunchStart2
和LunchEnd2
(Date/Time - 短时间)
Table: tmp_LunchPatterns
Pattern_Step
(数字和主键 - 长整型)
预填充数字 1 - 7 表示一周中的几天。LunchStart1
、LunchEnd1
、LunchStart2
和LunchEnd2
(Date/Time - 短时间)
三个查询是:
查询: 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 部分和所有 tables/queries 复制到一个新数据库并且 运行 它工作正常,如果我手动 运行 每个查询它工作正常,如果我 运行 它使用上面的代码块然后 DML_Add_NewLunchPattern_Name
工作正常,但第二个查询仅部分工作而最后一个根本不工作。
第二个查询使用正确的 PatternID
添加了 7 条记录,但是开始和结束时间是空白的,即使这 7 条记录一定来自包含预期的 start/end 时间的临时 table .
有什么明显的我遗漏的吗?
编辑:
为了尽量减少我的问题,我似乎错过了一个非常重要的部分。
List_LunchPattern_Names
table 用于填充表单上的组合框,该组合框将显示的记录从 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
更改为临时 table,因此可以输入新模式并在最终确定之前对照现有模式进行检查。
代码如下,我 认为 问题是表单需要刷新或重新查询才能完成记录源的更改。
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
编辑 2:
这更像是一种练习,以确保我首先正确地测试了所有内容。
这只是我添加到临时 table 的最后一条记录在更新查询期间被忽略了 - 我在之前的测试中没有看到这一点,因为我只添加了一条记录。例如,将时间添加到星期一和星期二将创建一个仅使用星期一的模式,而星期二仍保留在临时 table - 即使在我执行查询以清除 table.
之后
这让我相信,当我在一段时间内退出控件以单击确定模式的按钮时,记录不会添加到 table 直到单击事件完成之后,所以在单击事件我应该 DoEvents
或保存记录.....
事实证明答案很简单,有点明显,而且在我第一次迭代问题时不太可能得到答案,因为我遗漏了关键点(请参阅问题中的编辑)。
场景是一个 Continuous Form
链接到一个 table,其中包含七个空记录,主键除外,列出了 1 到 7 的数字范围。
用户将数据输入到这些记录中的一个或多个,然后单击 保存 按钮,将输入的所有记录传输到最终 table。
这里的问题是你输入数据的控件和按钮在同一个窗体上,所以当你点击按钮时最后输入的记录不会被保存。
在查找事件顺序来解释这一点时,我发现了这一点:
Q1. If data is entered in one or more fields on the main form and the user then clicks the button that opens the message box, is the record saved?
No, if the button is on the main form. However, you sometimes want to save a record (perhaps to print the values) so in the OnClick event you'd have something like If Me.Dirty then Me.Dirty = False in order to force a save.
所以答案是添加
If Me.Dirty Then Me.Dirty = False
到 btnCreateLunchPattern_Click()
过程的开始。
鉴于此 - 这个问题现在不适合回答。我应该删除问题吗?改写它以使其符合答案?在 meta 上提问?