0

我试图在我的表单中添加我的组合框,以允许我添加不在列表中的数据。我的组合框名为 cmbTitle,我从中提取组合框信息的表名是 tblDVDs。我的组合框没有列出标题,尽管它列出了我不想要的 ID 号,并且我拥有的代码不允许我将它添加到组合框也不允许更新我的表格我不确定我做错了什么。对不起,我是 VBA 的业余爱好者,有人可以帮助我。以下是带有组合框的表单代码:

Private Sub cmbTitle_Change()
On Error GoTo myError
Dim rst As DAO.Recordset
Set rst = Me.RecordsetClone
rst.FindFirst "ID = " & Me!cmbTitle
Me.Bookmark = rst.Bookmark
Me!cmbTitle = Null

leave:
 If Not rst Is Nothing Then Set rst = Nothing
 Exit Sub

 myError:
MsgBox "Record Not Found"
Resume leave
End Sub

Private Sub cmbTitle_NotInList(NewData As String, Response As Integer)

Dim db As Database

Dim LSQL As String
Dim LResponse As Integer
Dim ctl As Control

On Error GoTo Err_Execute

'Category combo box control
Set ctl = Me!Task

LResponse = MsgBox(NewData & " is a new item.  Do you wish to add it to the combo          box?", vbYesNo, "Add Item")

'User responded "Yes" to adding the new item to the combo box
If LResponse = vbYes Then
    Set db = CurrentDb()

    'Insert new item into underlying table
    LSQL = "insert into tblDVDs (tblDVDs) values ('" & NewData & "')"

    db.Execute LSQL, dbFailOnError

    Set db = Nothing

    'Set Response argument to indicate that data is being added.
    Response = acDataErrAdded

Else
    'Set Response argument to suppress error message and undo changes
    Response = acDataErrContinue
    ctl.Undo
End If

On Error GoTo 0

Exit Sub

` Err_Execute: ctl.Undo MsgBox "操作失败"

End Sub

End Sub
4

1 回答 1

0

您可以将组合框设置为 2 列

第 1 列 ID

第 2 栏标题

并将列宽设置为:0",1" 以隐藏组合框上的 ID

您的插入语句需要

插入 tblDVDS(标题字段)值

我假设 ID 是自动编号,其余代码看起来是正确的。

于 2013-07-24T13:53:51.250 回答