MS Access - ComboBox不允许我选择项目

问题描述 投票:0回答:2

在这里访问新的并且已经花费了大量时间来尝试解决这个问题,所以这里的背景:

注意:已经看过这个:ComboBox won't allow me to select an item,但没有提供我需要的答案。

我有一个数据库,允许一个人通过表单输入和存储客户详细信息,目前我有3个表具有以下关系:

客户 - CustomerID(PK) - FirstName - LastName

流程 - ProcessID(PK) - 详细信息 - PartsUse -

工作 - JobID(PK) - CustomerID(FK) - ProcessID(FK) - MachineDetail -

客户与Job有1-M的关系,但是通过扩展(不确定原因)Process也与Job有1-M的关系。因此,这里的目标是客户可以拥有许多工作,而工作应该只有一个流程(以后需要解决这个问题)。

现在,这是我的代码,用于在我的NewJob表单中绑定ComboBox - 其目的是在表单打开时使用客户的所有名称填充ComboBox,并且仅允许用户在选择客户后输入作业详细信息:

Private Sub Form_Open(Cancel As Integer)

    Dim db As Database
    Dim recordSet As DAO.recordSet
    Dim sql As String

    sql = "SELECT [Customer].[CustomerID], [Customer].[FirstName] & [Customer].[LastName] FROM Customer ORDER BY [CustomerID];"

    'clear all fields
    ClearJobFormFields

    'disable all controls until a customer is selected
    DisableJobFormControls

    With cmbCustomer
        .ControlSource = "Customer"
        .RowSource = sql
        .ColumnCount = 2
        .ColumnWidths = "1cm; 3cm"
        .BoundColumn = 0
    End With

    cmbCustomer.ControlSource = "Customer"
    cmbCustomer.RowSource = sql

End Sub

需要注意的是,每个表单都是独立的 - 我没有使用子表单。这个形式(NewJob)DOES将AllowEdit设置为yes,并且表单没有绑定它的RecordSource

ComboBox DOES填充正确,但每次我尝试选择一个项目时,我都会收到错误:“控件无法编辑,它绑定到未知字段Customer”。

这就是它的全部内容。对不起,如果这是一个常见/易于解决的问题,但它已经困扰了我好几天。

database vba ms-access-2007
2个回答
0
投票

如果您的表单没有Recordsource,则您的控件不应该(不能)拥有Controlsource。如果您在设计模式下转到表单的属性,您将在Controlsource属性中看到Customer不是有效选项。你为什么没有Recordsource。表格的目的不是输入工作数据吗?


0
投票

当您通过VBA设置rowsource属性时,您应将其保留为unbond控件并废弃:

cmbCustomer.ControlSource = "Customer"

你的开放式表格应该更像这样:

Private Sub Form_Open(Cancel As Integer)

Dim db As Database
Dim recordSet As DAO.recordSet
Dim sql As String

    sql = "SELECT [Customer].[CustomerID], [Customer].[FirstName] & [Customer].[LastName] FROM Customer ORDER BY [CustomerID];"

    'clear all fields
    ClearJobFormFields

    'disable all controls until a customer is selected
    DisableJobFormControls

    With cmbCustomer
        .RowSource = sql
        .ColumnCount = 2
        .ColumnWidths = "1cm; 3cm"
        .BoundColumn = 0
    End With

End Sub

然后,您可以使用此组合框的更新后事件来确定是否启用作业详细信息字段:

Private Sub cmbCustomer_AfterUpdate()

    'Check it's populated and set fields as necessary
    If cmbCustomer & "" = "" Then
        txtJobDetails.Enabled = 0 'Change this fieldname as required
    Else
        txtJobDetails.Enabled = -1
    End If

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.