MS Access将项目添加到组合框(如果不在列表中)
我有一个访问表,我想知道如何在组合框中添加项目(如果那里没有). 我的组合框处于值模式.
I have an access form, i want to know how to add item in combo box if there is not in there. my combo box is in value mode.
不幸的是,如果不将表单更改为设计模式并添加新值,就无法永久更改行源.用代码可以做到这一点,但是当人们在工作时,这不是一个好主意.最简单的方法是创建一个小表并将值添加到该表中.关闭表单后,新值将被保存.
Unfortunately you cannot change the rowsource permanently without changing the form to design mode and adding the new value. It is possible to do this with code, but it is not a good idea when people are working. The easiest thing is to create a small table and add the values to that. New values will then be saved when you close the form.
艾伦·布朗(Allen Browne)对如何执行此操作进行了说明: http://allenbrowne.com/ser-27 .html
Allen Browne has a description of how to do this : http://allenbrowne.com/ser-27.html
这是他展示的想法之一:
This is one of the ideas he shows:
Private Sub CategoryID_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 product 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 Categories ( CategoryName ) " & _
"SELECT """ & NewData & """ AS CategoryName;"
DBEngine(0)(0).Execute strTmp, dbFailOnError
'Notify Access about the new record, so it requeries the combo.
Response = acDataErrAdded
End If
End Sub