ms-access - 禁用与 Ms Access 中的表单连接的所有控件
问题描述
目前我使用一个列表框来选择表单提供的要编辑的记录。如果列表框中没有选择记录怎么办?如何禁用和清空连接到表单数据源的所有控件?
解决方案
您必须从表格中取消绑定表单并使用编码将值放入未绑定的表单控件中。在编程之前需要设置一些东西。
- 确保所有表单控件名称与表字段名称匹配。
- 确保表单属性中的记录源为空白。
- 在选择列表框时使用循环“插入”来自查询的值。
这需要更多的编程,因为您必须在每次控件更新时添加更新查询。但我发现这是最好的选择,因为它有助于防止损坏的记录,因为它在查看时没有绑定到记录。
当表单打开时,所有内容都应该是空白的,包括列表框。在更新事件后的列表框上,放入您的编码以查询数据集并将值放入表单控件中。
我没有您的表格字段的名称,也没有您的表单名称,所以我将使用随机名称。
编码示例:
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
如果这对您不起作用,请告诉我,我们可以找出另一种解决方案。
推荐阅读
- java - Dropwizard 中的数据库设置单元测试
- woocommerce - Woo-commerce 产品图片打开直接图片网址而不是灯箱
- excel - 如何将数字转换为音乐字母?
- c# - 在 ARkit 中的 FocusSquare Scene 的任何教程。如何通过类似于 ARHitCubeScene 的一键显示模型?
- r - 在 Shiny 中,数据框可以用作 selectizeInput 中的选择吗?
- php - 如何将 JSON 字符串的引号写入回显的 PHP 以为 jQuery 准备?
- c# - Telerik RadAutoCompleteBox 重复选择问题
- java - 如何将下载的图像存储在缓存中
- windows - 排除最大上次修改日期的文件
- c# - 将未知的 JSON 字符串转换为正确的对象