访问和VBA编码

时间:2013-07-24 02:18:01

标签: access-vba

我正在尝试在我的表单中放置我的组合框,以允许我添加数据(如果它不在列表中)。我的组合框名为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

1 个答案:

答案 0 :(得分:0)

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

第1列ID

Colum 2 Title

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

您的Insert语句必须

插入tblDVDS(titlefield)值

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