【问题标题】:MS Access - ComboBox not allowing me to select an itemMS Access - 组合框不允许我选择一个项目
【发布时间】:2012-11-19 05:47:51
【问题描述】:

这里是 Access 新手,已经花了很多时间试图解决这个问题,所以这里是背景:

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

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

客户 - 客户 ID (PK) - 名 - 姓氏

流程 - 进程 ID (PK) - 细节 - 零件使用 -

工作 - 工作ID(PK) - 客户 ID (FK) - 进程 ID (FK) - 机器详情 -

Customer 与 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) 上的表单将 AllowEdit 设置为是,并且表单没有绑定到它的 RecordSource

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

仅此而已。抱歉,如果这是一个常见/易于解决的问题,但它困扰了我好几天。

【问题讨论】:

  • 为什么要使用 VBA 填充它?这太矫枉过正了。在该表单的设计窗口中,单击您的组合框,然后按 F4 转到属性。在“数据”选项卡中,将您的查询输入到数据源属性中。
  • 另外,通常当您在表单上插入一个组合框时,它会自动询问您提供数据源的信息,您是否跳过了该步骤?最后一步只是在 AfterUpdate 事件上绑定一个事件并解锁所有之前锁定的控件。

标签: database vba ms-access-2007


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    当您通过 VBA 设置 rowsource 属性时,您应该将其保留为未绑定控件并报废:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-13
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多