通过不在组合框的列表事件中添加记录

Adding record via not in list event of combo box

希望有人能帮助我。

在 frmMain 上,我有一个列表框 (lstAuthor) 和一个组合框 (cboAuthor)。两者的 RowSource 都是查询 qryListAuthor。两者都有 authorID 和 AuthorName 两列,绑定列为 col 1.

当我开始在组合框中输入作者姓名时,我希望它更新,当我要查找的作者填写我正在输入的当前行时,我想按回车键并让 lstAuthor 更新为该特定记录,然后 selected。

加上另一个名为 lstBook 的列表框,用于更新和显示作者 select 在 lstAuthor 中编写的书籍。如果我向下滚动 lstAuthor 和 select 中的作者列表,就会发生这种情况。

不在列表中:

如果我在 cboAuthor 中输入作者姓名,而作者不存在,我需要按回车键并打开一个名为 frmAddAuthorFly 的表单。添加作者并关闭表单后,我需要用新作者更新 cboAuthor 和 lstAuthor,还需要将刚刚添加的作者 selected in lstAuthor。还有 lstBook 也要更新。 frmAddAuthorFly 只有三个字段; authorID、authorName 和 authorCategory。

我在这上面浪费了很多时间,所以也许有人可以给出解决方案。非常感谢...

今天花了一点时间,这就是我想出的。似乎工作正常,但如果有人发现任何明显的问题,请随时提供替代方案。

在cboAuthor的NotInList事件中

Private Sub cboAuthor_NotInList(NewData As String, Response As Integer)
On Error GoTo errline

Dim MsgBoxAnswer As Variant

Response = acDataErrContinue

MsgBoxAnswer = MsgBox("Do you want to add a new author?", vbYesNo, "Add New Author")

If msboxanswer = vbNo Then
Me.cboAuthor = Null
DoCmd.GoToControl "cboAuthor"
GoTo exitline

Else
txtHidden = "addOk"
DoCmd.OpenForm ("frmAddAuthorFly")
DoCmd.GoToRecord , , acNewRec
Forms![frmaddauthorfly]![AuthorName] = NewData
Me.cboAuthor = Null
DoCmd.GoToControl "authorcategory"

End If
exitline:
Exit Sub
errline:
Exit Sub
'Select Case Err.Number

End Sub

我在 frmAddAuthorFly 中添加了一个 cmdButton 并包含了以下代码:

Private Sub cmdClose_Click()
On Error GoTo errline

If Forms![frmmain]![txtHidden] = "addok" Then
Forms![frmmain]![cboAuthor].Requery
Forms![frmmain]![LstAuthor].Requery
Forms![frmmain]![LstAuthor] = Me.AuthorID
Forms![frmmain]![txtHidden] = "AddDone"
DoCmd.Close acForm, "frmAddAuthorFly"
Forms![frmmain]![cboAuthor].Requery
Forms![frmmain]![LstAuthor].Requery
Forms![frmmain].[LstAuthor].SetFocus

Else
MsgBox "txt hidden is not addok"
DoCmd.Close acForm, "frmaddauthorfly"
End If

errline:
Exit Sub

End Sub

我想做的一件事是,在我将焦点设置在刚刚添加的新作者的记录上之后,在列表框中单击它。不只是突出显示,但我想我可以点击它...