【问题标题】:Access and VBA Coding访问和 VBA 编码
【发布时间】:2020-07-13 21:56:40
【问题描述】:

我正在尝试在我的表单中添加我的组合框,以允许我添加不在列表中的数据。我的组合框名为 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.撤消 MsgBox "操作失败"

End Sub

End Sub

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

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

    第 1 列 ID

    第 2 栏标题

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

    您的插入语句需要是

    插入 tblDVDS(标题字段)值

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

    【讨论】:

    • 非常感谢,是的,ID 是自动编号,列宽的代码是什么,我还没有这样做
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 2020-12-13
    相关资源
    最近更新 更多