使用变量在Userform的Control中调用值

时间:2015-11-23 07:46:31

标签: vba ms-access access-vba

我试图从表单中调用一个值来将它放在SQL上。但它不起作用。

Dim n As integer
Dim StrSQL As String
Dim mymain As Variant
Dim inputform as string
mymain = Array("well_name", "well_alias", "well_short")

n=0
--try to loop
inputform="form_inputwell"+mymain(n)
StrSQL = "INSERT INTO databasewell (bh_name) VALUES ('" & inputform & "');"
DoCmd.RunSQL StrSQL
n=n+1
--loop end

任何想法调用数组中哪个字段的值? 例如:

form_inputwell.well_name called by declaring "form_inputwell"+mymain(1)

2 个答案:

答案 0 :(得分:0)

我认为这就是你要做的事情:

Dim n As Integer
Dim StrSQL As String
Dim mymain As Variant
Dim inputform As String
mymain = Array("well_name", "well_alias", "well_short")

For n = LBound(mymain) To UBound(mymain)
    inputform = form_inputwell.Controls(mymain(n)).Value
    StrSQL = "INSERT INTO databasewell (bh_name) VALUES ('" & inputform & "');"
    DoCmd.RunSQL StrSQL
Next n

答案 1 :(得分:0)

我找到了我的解决方案,但无论如何都要感谢您的帮助。

paramtemp = Forms("form name").Controls(field in form).Value