【发布时间】:2018-02-14 03:35:43
【问题描述】:
我在别处找不到这个问题的答案。我是 VBA 新用户。
我有一个非常简单的表格用于输入植物标本的数据。只有 3 个字段具有下拉框。 “物种”和“代码”两个字段引用另一个名为“MasterVegList”的表中的信息,以方便字段数据输入。
为了让最终用户可以将新物种添加到表单和“MasterVegList”表中,我编写了以下 VBA 代码:
Private Sub Code_NotInList(NewData As String, Response As Integer)
Dim strTmp As String
'Get confirmation that this is not just a spelling error.
strTmp = "Add '" & NewData & "' as a new category?"
If MsgBox(strTmp, vbYesNo + vbDefaultButton2 + vbQuestion, "Not in list") = vbYes Then
'Append the NewData as a record in the Categories table.
strTmp = "INSERT INTO MasterVegList ( Code ) " & _
"SELECT """ & NewData & """ AS Code;"
DBEngine(0)(0).Execute strTmp, dbFailOnError
'Notify Access about the new record, so it requeries the combo.
Response = acDataErrAdded
End If
End Sub
Private Sub Species_NotInList(NewData As String, Response As Integer)
Dim strTmp As String
'Get confirmation that this is not just a spelling error.
strTmp = "Add '" & NewData & "' as a new category?"
If MsgBox(strTmp, vbYesNo + vbDefaultButton2 + vbQuestion, "Not in list") = vbYes Then
'Append the NewData as a record in the Categories table.
strTmp = "INSERT INTO MasterVegList ( Species ) " & _
"SELECT """ & NewData & """ AS Species;"
DBEngine(0)(0).Execute strTmp, dbFailOnError
'Notify Access about the new record, so it requeries the combo.
Response = acDataErrAdded
End If
End Sub
问题在于,当输入一个新物种时,会为 MasterVegList 表中的物种和代码创建一个新记录。我需要将物种和代码输入到同一记录中。请注意,我指的是 MasterVegList 表,而不是打算使用表单填充的表。
【问题讨论】: