我希望你能帮忙,我是VBA的新手,但渴望学习。我创建了一个表单,它将文档编号添加到我的数据库(字段名称DocNum)。我为该字段创建了一个“afterupdate”事件,以在每个表上创建相应的记录,该记录将保存该文档ID的附加信息。请参阅此处的代码:
Private Sub DocNum_AfterUpdate()
Dim TBL_3_ManuscriptPrimaryReviewer As DAO.Recordset
Set TBL_3_ManuscriptPrimaryReviewer = CurrentDb.OpenRecordset("Select * FROM [TBL_3_ManuscriptPrimaryReviewer]")
TBL_3_ManuscriptPrimaryReviewer.AddNew
TBL_3_ManuscriptPrimaryReviewer![Manuscript_Number] = Me.DocNum.Value
TBL_3_ManuscriptPrimaryReviewer.Update
TBL_3_ManuscriptPrimaryReviewer.Close
Set TBL_3_ManuscriptPrimaryReviewer = Nothing
End Sub
Private Sub DocNum_AfterUpdate()
Dim TBL_4_ManuscriptSTATReviewer As DAO.Recordset
Set TBL_4_ManuscriptSTATReviewer = CurrentDb.OpenRecordset("Select * FROM [TBL_4_ManuscriptSTATReviewer]")
TBL_4_ManuscriptSTATReviewer.AddNew
TBL_4_ManuscriptSTATReviewer![Manuscript_Number] = Me.DocNum.Value
TBL_4_ManuscriptSTATReviewer.Update
TBL_4_ManuscriptSTATReviewer.Close
Set TBL_4_ManuscriptSTATReviewer = Nothing
End Sub
Private Sub DocNum_AfterUpdate()
Dim TBL_5_ManuscriptSCReview As DAO.Recordset
Set TBL_5_ManuscriptSCReview = CurrentDb.OpenRecordset("Select * FROM [TBL_5_ManuscriptSCReview]")
TBL_5_ManuscriptSCReview.AddNew
TBL_5_ManuscriptSCReview![Manuscript_Number] = Me.DocNum.Value
TBL_5_ManuscriptSCReview.Update
TBL_5_ManuscriptSCReview.Close
Set TBL_5_ManuscriptSCReview = Nothing
End Sub
Private Sub DocNum_AfterUpdate()
Dim TBL_6_ManuscriptPublications As DAO.Recordset
Set TBL_6_ManuscriptPublications = CurrentDb.OpenRecordset("Select * FROM [TBL_6_ManuscriptPublications]")
TBL_6_ManuscriptPublications.AddNew
TBL_6_ManuscriptPublications![Manuscript_Number] = Me.DocNum.Value
TBL_6_ManuscriptPublications.Update
TBL_6_ManuscriptPublications.Close
Set TBL_6_ManuscriptPublications = Nothing
End Sub
但是,当我尝试使用表单时出现以下错误:
“您在事件属性设置中输入的更新后表达式产生了以下错误:检测到不明确的名称:DocNum_AfterUpdate。”
在做了一些研究之后,我尝试将代码重写为一个Private Sub而不是四个,如下所示:
Private Sub DocNum_AfterUpdate()
Dim TBL_3_ManuscriptPrimaryReviewer As DAO.Recordset
Set TBL_3_ManuscriptPrimaryReviewer = CurrentDb.OpenRecordset("Select * FROM [TBL_3_ManuscriptPrimaryReviewer]")
TBL_3_ManuscriptPrimaryReviewer.AddNew
TBL_3_ManuscriptPrimaryReviewer![Manuscript_Number] = Me.DocNum.Value
TBL_3_ManuscriptPrimaryReviewer.Update
TBL_3_ManuscriptPrimaryReviewer.Close
Set TBL_3_ManuscriptPrimaryReviewer = Nothing
Dim TBL_4_ManuscriptSTATReviewer As DAO.Recordset
Set TBL_4_ManuscriptSTATReviewer = CurrentDb.OpenRecordset("Select * FROM [TBL_4_ManuscriptSTATReviewer]")
TBL_4_ManuscriptSTATReviewer.AddNew
TBL_4_ManuscriptSTATReviewer![Manuscript_Number] = Me.DocNum.Value
TBL_4_ManuscriptSTATReviewer.Update
TBL_4_ManuscriptSTATReviewer.Close
Set TBL_4_ManuscriptSTATReviewer = Nothing
Dim TBL_5_ManuscriptSCReview As DAO.Recordset
Set TBL_5_ManuscriptSCReview = CurrentDb.OpenRecordset("Select * FROM [TBL_5_ManuscriptSCReview]")
TBL_5_ManuscriptSCReview.AddNew
TBL_5_ManuscriptSCReview![Manuscript_Number] = Me.DocNum.Value
TBL_5_ManuscriptSCReview.Update
TBL_5_ManuscriptSCReview.Close
Set TBL_5_ManuscriptSCReview = Nothing
Dim TBL_6_ManuscriptPublications As DAO.Recordset
Set TBL_6_ManuscriptPublications = CurrentDb.OpenRecordset("Select * FROM [TBL_6_ManuscriptPublications]")
TBL_6_ManuscriptPublications.AddNew
TBL_6_ManuscriptPublications![Manuscript_Number] = Me.DocNum.Value
TBL_6_ManuscriptPublications.Update
TBL_6_ManuscriptPublications.Close
Set TBL_6_ManuscriptPublications = Nothing
End Sub
然而,这也不起作用。它只更新TBL_6_ManuscriptPublications,而不是表3,4或5。
我一直在寻找关于如何进行多次更新后事件的大约2个小时,但似乎没有任何帮助。 If> Then似乎不适用,For> Next也不适用。
我很乐意帮助,谢谢! -Deb
答案 0 :(得分:0)
只需运行操作追加查询,不需要任何记录集。由于您要从用户输入表单传递值,请考虑使用QueryDefs进行参数化。为了使它成为一个干燥的解决方案( D on R epeat Y 我们自己),可以使用一个函数查询电话。
Private Sub DocNum_AfterUpdate()
Dim strSQL
' TBL_3_ManuscriptPrimaryReviewer APPEND
strSQL = "PARAMETERS DocNumParam TEXT(255);" _
& " INSERT INTO [TBL_3_ManuscriptPrimaryReviewer] ([Manuscript_Number]) " _
& " VALUES (DocNumParam)"
Call RunQuery(strSQL)
' TBL_4_ManuscriptSTATReviewer APPEND
strSQL = "PARAMETERS DocNumParam TEXT(255);" _
& "INSERT INTO [TBL_4_ManuscriptSTATReviewer] ([Manuscript_Number]) " _
& " VALUES (DocNumParam)"
Call RunQuery(strSQL)
' TBL_5_ManuscriptSCReview APPEND
strSQL = "PARAMETERS DocNumParam TEXT(255);" _
& "INSERT INTO [TBL_5_ManuscriptSCReview] ([Manuscript_Number]) " _
& " VALUES (DocNumParam)"
Call RunQuery(strSQL)
' TBL_6_ManuscriptPublications APPEND
strSQL = "PARAMETERS DocNumParam TEXT(255);" _
& "INSERT INTO [TBL_6_ManuscriptPublications] ([Manuscript_Number]) " _
& " VALUES (DocNumParam)"
Call RunQuery(strSQL)
End Sub
Public Function RunQuery(stmt As String)
Dim qdef As QueryDef
Set qdef = CurrentDb.CreateQueryDef("", stmt)
' BIND PARAM VALUE
qdef!DocNumParam = Me.DocNumValue
' EXECUTE ACTION
qdef.Execute, dbFailOnError
Set qdef = Nothing
End Function
但是,您的数据库设计可以通过规范化进行优化。不要使用需要更新和维护的多个类似结构的表,而是使用带有 Type 等指示符的一个表(即TBL_REVIEWERS
)。此外,查询数据将更容易。
ID Type Manuscript_Number ...
1 PrimaryReviewer 12345
2 ManuscriptSTATReviewer 12345
3 ManuscriptSCReview 12345
4 ManuscriptPublications 12345
然后,您的追加查询将针对同一查询调用(即使是DRY-er)修改两个值。
Private Sub DocNum_AfterUpdate()
Dim var As Variant
Dim strSQL As String
strSQL = "PARAMETERS TypeParam TEXT(255), DocNumParam TEXT(255);" _
& " INSERT INTO [TBL_Reviewers] ([Type], [Manuscript_Number]) " _
& " VALUES (TypeParam, DocNumParam)"
' RUN APPEND QUERIES
For Each var In Array("PrimaryReviewer", "ManuscriptSTATReviewer" _
"ManuscriptSCReview", "ManuscriptPublications")
Call RunQuery(strSQL, var)
Next var
End Sub
Public Function RunQuery(strSQL As String, strType As String)
Dim qdef As QueryDef
Set qdef = CurrentDb.CreateQueryDef("", strSQL)
' BIND PARAM VALUES
qdef!TypeParam = strType
qdef!DocNumParam = Me.DocNumValue
' EXECUTE ACTION
qdef.Execute, dbFailOnError
Set qdef = Nothing
End Function