首页 > 解决方案 > 禁用与 Ms Access 中的表单连接的所有控件

问题描述

目前我使用一个列表框来选择表单提供的要编辑的记录。如果列表框中没有选择记录怎么办?如何禁用和清空连接到表单数据源的所有控件?

标签: ms-access

解决方案


您必须从表格中取消绑定表单并使用编码将值放入未绑定的表单控件中。在编程之前需要设置一些东西。

  1. 确保所有表单控件名称与表字段名称匹配。
  2. 确保表单属性中的记录源为空白。
  3. 在选择列表框时使用循环“插入”来自查询的值。

这需要更多的编程,因为您必须在每次控件更新时添加更新查询。但我发现这是最好的选择,因为它有助于防止损坏的记录,因为它在查看时没有绑定到记录。

当表单打开时,所有内容都应该是空白的,包括列表框。在更新事件后的列表框上,放入您的编码以查询数据集并将值放入表单控件中。

我没有您的表格字段的名称,也没有您的表单名称,所​​以我将使用随机名称。

编码示例:

Private sub Listbox1_AfterUpdate()
dim rst as dao.recordset
dim strSQL as string
dim ctr as control

for each ctr in me.controls
    'Identifies if the control is a text box or combo box before trying to enter value in the incorrect control type
    if ctr.controltype = actextbox or ctr.controltype = accombobox
        strsql = "SELECT " & ctr.name & " as [FieldName] " & _
                 "FROM Table1 " & _
                 "WHERE (((Field1) ='" & me.listbox1 & "'));"
        set rst = currentdb.openrecordset(strsql)
        ctr.value = rst![FieldName]
        rst.close
        set rst = nothing
     end if
next ctr

EndCode:
If not rst is nothing then
    rst.close
    set rst = nothing
end if
end sub

如果这对您不起作用,请告诉我,我们可以找出另一种解决方案。


推荐阅读