我正在为父亲(应他的要求)建立Access数据库。如果他输入的内容不在列表中,他希望组合框提示他。很简单。我遇到了一个我认为是错误的错误,因为当我创建多个combox框时,在一个combox框上使用的完全相同的代码将永远无法识别NotInList事件中的acDataErrAdded
响应。我在DAO记录集和DoCmd.SQL中都有工作代码,这些代码来自其他尝试以相同形式出现问题的尝试。作为记录,所有版本的代码都可以识别acDataErrContinue
这是当前的“有效” SQL版本。
Private Sub Rep2_ID_NotInList(NewData As String, Response As Integer)
On Error GoTo Error_Handler
Dim intAnswer As Integer
Dim strSQL As String
intAnswer = MsgBox(NewData & " is not an approved category. " & vbCrLf _
& "Do you want to add it now?", _
vbYesNo + vbQuestion, "Invalid Category")
Select Case intAnswer
Case vbYes
DoCmd.SetWarnings False
strSQL = "INSERT INTO tu_Rep2(Rep2) " & _
"Select """ & NewData & """;"
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
Response = acDataErrAdded
Case vbNo
MsgBox "Please select an item from the list.", _
vbExclamation + vbOKOnly, "Invalid Entry"
Response = acDataErrContinue
End Select
Exit_Procedure:
DoCmd.SetWarnings True
Exit Sub
Error_Handler:
MsgBox Err.Number & ", " & Err.Description
Resume Exit_Procedure
Resume
End Sub
和(几乎)完全相同的代码用于不同的组合框,该组合框无法识别acDataErrAdded
,但会添加到列表的底部(因此您可以选择它并继续前进)。
Private Sub Rep1_ID_NotInList(NewData As String, Response As Integer)
On Error GoTo Error_Handler
Dim intAnswer As Integer
Dim strSQL As String
intAnswer = MsgBox(NewData & " is not an approved category. " & vbCrLf _
& "Do you want to add it now?", _
vbYesNo + vbQuestion, "Invalid Category")
Select Case intAnswer
Case vbYes
DoCmd.SetWarnings False
strSQL = "INSERT INTO tu_Rep1(Rep1) " & _
"Select """ & NewData & """;"
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
Response = acDataErrAdded
Case vbNo
MsgBox "Please select an item from the list.", _
vbExclamation + vbOKOnly, "Invalid Entry"
Response = acDataErrContinue
End Select
Exit_Procedure:
DoCmd.SetWarnings True
Exit Sub
Error_Handler:
MsgBox Err.Number & ", " & Err.Description
Resume Exit_Procedure
Resume
End Sub
我还尝试通过有效的DAO NotInList代码进行复制,并且这也停止了识别acDataErrAdded
我愿意分享玩具数据库。任何煽动将不胜感激。与在Access中创建表单相比,我更习惯于使用命令行数据库界面。