基于函数输出更新表的方法

时间:2016-08-30 17:33:18

标签: access-vba

我使用的是一个访问函数,它使用case select语句遍历多个记录集,并在VBA立即窗口中显示字符串行。

有人可以建议我如何了解可能用于使用当前显示在VBA即时窗口中的结果更新表的方法吗?

到目前为止,我的搜索建议DoCmd.SQL可能有用。

    Case Is = "1"                    ' 1 bottle
        Debug.Print rsWCol!r4_wcol_outstring & rsBottl!bottleoutstring

        displays the string below in the immediate window:

        R41602T50 1  00   62  710 C 1120   9800 550     1 00S #135                     0

我希望能够使用以下内容

Dim writeRecSQL As String ' used to append records to a temp table
writeRecSQL = "INSERT INTO tbl_R_export_TEMP ( R_str ) select rsWCol!r4_wcol_outstring & rsBottl!bottleoutstring;" 


        Case Is = "1"                    ' 1 bottle
        ' Debug.Print rsWCol!r4_wcol_outstring & rsBottl!bottleoutstring
        DoCmd.RunSQL writeRecSQL        

我的SQL语句的选择部分似乎没有获取值。 我理解通常情况可能是select fieldX from tablex 我的陈述更像是:select rs!foo

引发'输入参数值' 消息框,询问 r4_wcol_outstring

的值

(在我的SQL中使用FROM后面的SELECT引发了VB "Run-time error '3134' syntax error in INSERT INTO statement."

我可以使用一些建议或一个如何写一个SQL语句的例子来记录我设置的参数值。

1 个答案:

答案 0 :(得分:0)

开始的好地方是微软的网站: https://msdn.microsoft.com/en-us/library/bb208861(v=office.12).aspx

但是,如果您只想将刚刚检索到的记录中的值插入另一个表中 尝试在转义的双引号中包装字段,并将值隐藏在硬编码字符串之外。

如:
writeRecSQL = "INSERT INTO tbl_R_export_TEMP ( [R_str] ) VALUES (""" & rsWCol!r4_wcol_outstring & rsBottl!bottleoutstring & """);"

Docmd.RunSQL之外,您还可以使用Querydefs对象查看CurrentDB.Execute和预定义参数查询