首页 > 解决方案 > 收集选项组值并将值插入表中

问题描述

我有一个带有几个切换按钮的表单。我希望能够为特定值切换每个按钮并将所选值用作insert into另一个表。

表单输入工作,表格和连接工作,表单子数据表查询是可更新的并显示相关字段

Select * from FRAMEQUERY(as Boolean) WHERE Value = True

INSERT INTO ORDERDETAILS

需要一些代码帮助来收集值并插入它们。

标签: sqlvbams-accessms-access-forms

解决方案


由于要插入的字段取决于表单控件的状态,因此您需要遍历相关的表单控件并构造适当的 SQL 语句,然后可以使用DoCmd.RunSQL或等方法执行该语句CurrentDb.Execute

我建议对表单上的控件进行迭代,并且对于每个控件,如果选择了该控件,请将相应的字段名称附加到 SQL 语句的insert intoselect部分。

例如,在pseudocode中,这可能类似于:

strFld = ""
strVal = ""
for each control on form
    if control.value = true then
        strFld = strFld & "YourFieldName, "
        strVal = strVal & "YourFieldValue, "
    end if
next control

if strFld <> "" then
    strFld = left(strFld, len(strFld)-2) 
    strVal = left(strVal, len(strVal)-2)
    strSQL = "insert into YourTable (" & strFld & ") values (" & strVal & ")"
end if

推荐阅读