Microsoft Access - 循环遍历我的 sql 插入查询的表单字段名称
Microsoft Access - Loop through form field names for my sql Insert query
我有一个表格,我想在其中跟踪许多资产的资产信息(资产已经写在员工填写的表格上)
Assets
Hours
DSL
DEF
COOL
10W
30W
40W
*Then 12 more fields
Asset 1
Asset 2
*Then 55 more assets
字段未绑定,字段名称完全相同,只是按时间顺序编号(asset1、hours1、dsl1...asset2、hours2、dsl2...等)。
我想知道是否可以通过更改字段名称创建一个循环来同时保存所有行。
我是循环的新手,但我想看看是否可以通过循环更改字段名称,所以我使用了:
For i = 1 To 3
Me.varlooptest = Me("asset" & i).Value
MsgBox "checkpoint"
Next i
这样做是将资产编号放入我的 Me.varlooptest 文本框中,然后在它对每一行起作用时给我消息“检查点”(确实如此)。
知道更改字段名称的代码可以工作,我尝试保存它,它会给我一个“查询输入必须包含至少一个 table 或查询”错误,我基于此查询我其他的,所以我觉得 应该 是正确的,但我想我遗漏了一些东西
For i = 1 To 3
Set db = DBEngine.Workspaces(0).Databases(0)
If Len(Me.Controls("asset" & i).Value) <> 0 Then
insert_shop_qry = "INSERT INTO tbl_shop(tracking_shop_id,asset_id,hours,dsl,def,cool,W10,W30,W40,glass,fittings,airfilter,wheels,hose,mirrors,battery,lights,grease,leaks,blowout,comment) SELECT " & Me.shop_tracking_id & " AS tracking_shop_id, " & _
Me("asset" & i).Value & " AS asset_id, " & _
Me("hours" & i).Value & " AS hours, '" & _
Me("dsl" & i).Value & "' AS dsl, '" & _
Me("def" & i).Value & "' AS def, '" & _
Me("cool" & i).Value & "' AS cool, '" & _
Me("W10_" & i).Value & "' AS W10, '" & _
Me("W30_" & i).Value & "' AS W30, '" & _
Me("W40_" & i).Value & "' AS W40, '" & _
Me("glass" & i).Value & "' AS glass, '" & _
Me("fittings" & i).Value & "' AS fittings, '" & _
Me("airfilter" & i).Value & "' AS airfilter, '" & _
Me("wheels" & i).Value & "' AS wheels, '" & _
Me("hose" & i).Value & "' AS hose, '" & _
Me("mirrors" & i).Value & "' AS mirrors, '" & _
Me("battery" & i).Value & "' AS battery, '" & _
Me("lights" & i).Value & "' AS lights, '" & _
Me("grease" & i).Value & "' AS hose, '" & _
Me("leaks" & i).Value & "' AS mirrors, '" & _
Me("blowout" & i).Value & "' AS blowout, '" & _
Me("comment" & i).Value & "' AS comment );"
CurrentDb.Execute insert_shop_qry
End If
Next i
MsgBox "saved!"
看问题下的评论!
谢谢@MichaelMurphy 和@June7;它完美运行!!
编辑:书面说明
我很笨,这些字段不正确(谢谢@MichaelMurphy):
Me("grease" & i).Value & "' AS hose, '" & _
Me("leaks" & i).Value & "' AS mirrors, '" & _
更正为:
Me("grease" & i).Value & "' AS grease, '" & _
Me("leaks" & i).Value & "' AS leaks, '" & _
而且我在评论末尾有一个不成对的括号,我猜 SQL 没有使用分号(谢谢@June7):
Me("comment" & i).Value & "' AS comment );"
更正为:
Me("comment" & i).Value & "' AS comment "
所以更正后的工作代码是:
For i = 1 To 3
Set db = DBEngine.Workspaces(0).Databases(0)
If Len(Me.Controls("asset" & i).Value) <> 0 Then
insert_shop_qry = "INSERT INTO tbl_shop(tracking_shop_id,asset_id,hours,dsl,def,cool,W10,W30,W40,glass,fittings,airfilter,wheels,hose,mirrors,battery,lights,grease,leaks,blowout,comment) SELECT " & Me.shop_tracking_id & " AS tracking_shop_id, " & _
Me("asset" & i).Value & " AS asset_id, '" & _
Me("hours" & i).Value & "' AS hours, '" & _
Me("dsl" & i).Value & "' AS dsl, '" & _
Me("def" & i).Value & "' AS def, '" & _
Me("cool" & i).Value & "' AS cool, '" & _
Me("W10_" & i).Value & "' AS W10, '" & _
Me("W30_" & i).Value & "' AS W30, '" & _
Me("W40_" & i).Value & "' AS W40, '" & _
Me("glass" & i).Value & "' AS glass, '" & _
Me("fittings" & i).Value & "' AS fittings, '" & _
Me("airfilter" & i).Value & "' AS airfilter, '" & _
Me("wheels" & i).Value & "' AS wheels, '" & _
Me("hose" & i).Value & "' AS hose, '" & _
Me("mirrors" & i).Value & "' AS mirrors, '" & _
Me("battery" & i).Value & "' AS battery, '" & _
Me("lights" & i).Value & "' AS lights, '" & _
Me("grease" & i).Value & "' AS grease, '" & _
Me("leaks" & i).Value & "' AS leaks, '" & _
Me("blowout" & i).Value & "' AS blowout, '" & _
Me("comment" & i).Value & "' AS comment "
CurrentDb.Execute insert_shop_qry
Else
Cancel = True
End If
Next i
考虑使用 QueryDef parameters 和保存的 Access 查询将 SQL 和 VBA 分开:
SQL(另存为查询对象,无VBA变量、引号、符号换行符)
PARAMETERS [prm_shop_tracking_id] INTEGER, [prm_asset] INTEGER, [prm_hours] DOUBLE,
[prm_dsl] TEXT, [prm_def] TEXT, [prm_cool] TEXT,
[prm_W10] TEXT, [prm_W30] TEXT, [prm_W40] TEXT,
[prm_glass] TEXT, [prm_fittings] TEXT, [prm_airfilter] TEXT,
[prm_wheels] TEXT, [prm_hose] TEXT, [prm_mirrors] TEXT,
[prm_battery] TEXT, [prm_lights] TEXT, [prm_grease] TEXT,
[prm_leaks] TEXT, [prm_blowout] TEXT, [prm_comment] TEXT;
INSERT INTO tbl_shop (tracking_shop_id, asset_id, hours, dsl, def, cool,
W10, W30, W40, glass, fittings, airfilter, wheels,
hose, mirrors, battery, lights, grease, leaks,
blowout, comment)
VALUES ([prm_shop_tracking_id], [prm_asset], [prm_hours], [prm_dsl],
[prm_def], [prm_cool], [prm_W10], [prm_W30], [prm_W40],
[prm_glass], [prm_fittings], [prm_airfilter], [prm_wheels],
[prm_hose], [prm_mirrors], [prm_battery], [prm_lights],
[prm_grease], [prm_leaks], [prm_blowout], [prm_comment]);
VBA (无 SQL 命令、标点符号或句法)
' INITIALIZE DAO OBJECTS ONCE
Set db = CurrentDb
Set qdef = db.QueryDefs("mySavedInsertQuery")
For i = 1 To 3
If Len(Me.Controls("asset" & i).Value) <> 0 Then
' BIND PARAMETERS
qdef![prm_shop_tracking_id] = Me.shop_tracking_id
qdef![prm_asset] = Me("asset" & i).Value
qdef![prm_hours] = Me("hours" & i).Value
qdef![prm_dsl] = Me("dsl" & i).Value
qdef![prm_def] = Me("def" & i).Value
qdef![prm_cool] = Me("cool" & i).Value
qdef![prm_W10] = Me("W10_" & i).Value
qdef![prm_W30] = Me("W30_" & i).Value
qdef![prm_W40] = Me("W40_" & i).Value
qdef![prm_glass] = Me("glass" & i).Value
qdef![prm_fittings] = Me("fittings" & i).Value
qdef![prm_airfilter] = Me("airfilter" & i).Value
qdef![prm_wheels] = Me("wheels" & i).Value
qdef![prm_hose] = Me("hose" & i).Value
qdef![prm_mirrors] = Me("mirrors" & i).Value
qdef![prm_battery] = Me("battery" & i).Value
qdef![prm_lights] = Me("lights" & i).Value
qdef![prm_grease] = Me("grease" & i).Value
qdef![prm_leaks] = Me("leaks" & i).Value
qdef![prm_blowout] = Me("blowout" & i).Value
qdef![prm_comment] = Me("comment" & i).Value
' EXECUTE ACTION
qdef.Execute
End If
Next i
' RELEASE DAO OBJECTS
Set qdef = Nothing: Set db = Nothing
我有一个表格,我想在其中跟踪许多资产的资产信息(资产已经写在员工填写的表格上)
Assets | Hours | DSL | DEF | COOL | 10W | 30W | 40W | *Then 12 more fields |
---|---|---|---|---|---|---|---|---|
Asset 1 | ||||||||
Asset 2 | ||||||||
*Then 55 more assets |
字段未绑定,字段名称完全相同,只是按时间顺序编号(asset1、hours1、dsl1...asset2、hours2、dsl2...等)。 我想知道是否可以通过更改字段名称创建一个循环来同时保存所有行。
我是循环的新手,但我想看看是否可以通过循环更改字段名称,所以我使用了:
For i = 1 To 3
Me.varlooptest = Me("asset" & i).Value
MsgBox "checkpoint"
Next i
这样做是将资产编号放入我的 Me.varlooptest 文本框中,然后在它对每一行起作用时给我消息“检查点”(确实如此)。
知道更改字段名称的代码可以工作,我尝试保存它,它会给我一个“查询输入必须包含至少一个 table 或查询”错误,我基于此查询我其他的,所以我觉得 应该 是正确的,但我想我遗漏了一些东西
For i = 1 To 3
Set db = DBEngine.Workspaces(0).Databases(0)
If Len(Me.Controls("asset" & i).Value) <> 0 Then
insert_shop_qry = "INSERT INTO tbl_shop(tracking_shop_id,asset_id,hours,dsl,def,cool,W10,W30,W40,glass,fittings,airfilter,wheels,hose,mirrors,battery,lights,grease,leaks,blowout,comment) SELECT " & Me.shop_tracking_id & " AS tracking_shop_id, " & _
Me("asset" & i).Value & " AS asset_id, " & _
Me("hours" & i).Value & " AS hours, '" & _
Me("dsl" & i).Value & "' AS dsl, '" & _
Me("def" & i).Value & "' AS def, '" & _
Me("cool" & i).Value & "' AS cool, '" & _
Me("W10_" & i).Value & "' AS W10, '" & _
Me("W30_" & i).Value & "' AS W30, '" & _
Me("W40_" & i).Value & "' AS W40, '" & _
Me("glass" & i).Value & "' AS glass, '" & _
Me("fittings" & i).Value & "' AS fittings, '" & _
Me("airfilter" & i).Value & "' AS airfilter, '" & _
Me("wheels" & i).Value & "' AS wheels, '" & _
Me("hose" & i).Value & "' AS hose, '" & _
Me("mirrors" & i).Value & "' AS mirrors, '" & _
Me("battery" & i).Value & "' AS battery, '" & _
Me("lights" & i).Value & "' AS lights, '" & _
Me("grease" & i).Value & "' AS hose, '" & _
Me("leaks" & i).Value & "' AS mirrors, '" & _
Me("blowout" & i).Value & "' AS blowout, '" & _
Me("comment" & i).Value & "' AS comment );"
CurrentDb.Execute insert_shop_qry
End If
Next i
MsgBox "saved!"
看问题下的评论! 谢谢@MichaelMurphy 和@June7;它完美运行!!
编辑:书面说明
我很笨,这些字段不正确(谢谢@MichaelMurphy):
Me("grease" & i).Value & "' AS hose, '" & _
Me("leaks" & i).Value & "' AS mirrors, '" & _
更正为:
Me("grease" & i).Value & "' AS grease, '" & _
Me("leaks" & i).Value & "' AS leaks, '" & _
而且我在评论末尾有一个不成对的括号,我猜 SQL 没有使用分号(谢谢@June7):
Me("comment" & i).Value & "' AS comment );"
更正为:
Me("comment" & i).Value & "' AS comment "
所以更正后的工作代码是:
For i = 1 To 3
Set db = DBEngine.Workspaces(0).Databases(0)
If Len(Me.Controls("asset" & i).Value) <> 0 Then
insert_shop_qry = "INSERT INTO tbl_shop(tracking_shop_id,asset_id,hours,dsl,def,cool,W10,W30,W40,glass,fittings,airfilter,wheels,hose,mirrors,battery,lights,grease,leaks,blowout,comment) SELECT " & Me.shop_tracking_id & " AS tracking_shop_id, " & _
Me("asset" & i).Value & " AS asset_id, '" & _
Me("hours" & i).Value & "' AS hours, '" & _
Me("dsl" & i).Value & "' AS dsl, '" & _
Me("def" & i).Value & "' AS def, '" & _
Me("cool" & i).Value & "' AS cool, '" & _
Me("W10_" & i).Value & "' AS W10, '" & _
Me("W30_" & i).Value & "' AS W30, '" & _
Me("W40_" & i).Value & "' AS W40, '" & _
Me("glass" & i).Value & "' AS glass, '" & _
Me("fittings" & i).Value & "' AS fittings, '" & _
Me("airfilter" & i).Value & "' AS airfilter, '" & _
Me("wheels" & i).Value & "' AS wheels, '" & _
Me("hose" & i).Value & "' AS hose, '" & _
Me("mirrors" & i).Value & "' AS mirrors, '" & _
Me("battery" & i).Value & "' AS battery, '" & _
Me("lights" & i).Value & "' AS lights, '" & _
Me("grease" & i).Value & "' AS grease, '" & _
Me("leaks" & i).Value & "' AS leaks, '" & _
Me("blowout" & i).Value & "' AS blowout, '" & _
Me("comment" & i).Value & "' AS comment "
CurrentDb.Execute insert_shop_qry
Else
Cancel = True
End If
Next i
考虑使用 QueryDef parameters 和保存的 Access 查询将 SQL 和 VBA 分开:
SQL(另存为查询对象,无VBA变量、引号、符号换行符)
PARAMETERS [prm_shop_tracking_id] INTEGER, [prm_asset] INTEGER, [prm_hours] DOUBLE,
[prm_dsl] TEXT, [prm_def] TEXT, [prm_cool] TEXT,
[prm_W10] TEXT, [prm_W30] TEXT, [prm_W40] TEXT,
[prm_glass] TEXT, [prm_fittings] TEXT, [prm_airfilter] TEXT,
[prm_wheels] TEXT, [prm_hose] TEXT, [prm_mirrors] TEXT,
[prm_battery] TEXT, [prm_lights] TEXT, [prm_grease] TEXT,
[prm_leaks] TEXT, [prm_blowout] TEXT, [prm_comment] TEXT;
INSERT INTO tbl_shop (tracking_shop_id, asset_id, hours, dsl, def, cool,
W10, W30, W40, glass, fittings, airfilter, wheels,
hose, mirrors, battery, lights, grease, leaks,
blowout, comment)
VALUES ([prm_shop_tracking_id], [prm_asset], [prm_hours], [prm_dsl],
[prm_def], [prm_cool], [prm_W10], [prm_W30], [prm_W40],
[prm_glass], [prm_fittings], [prm_airfilter], [prm_wheels],
[prm_hose], [prm_mirrors], [prm_battery], [prm_lights],
[prm_grease], [prm_leaks], [prm_blowout], [prm_comment]);
VBA (无 SQL 命令、标点符号或句法)
' INITIALIZE DAO OBJECTS ONCE
Set db = CurrentDb
Set qdef = db.QueryDefs("mySavedInsertQuery")
For i = 1 To 3
If Len(Me.Controls("asset" & i).Value) <> 0 Then
' BIND PARAMETERS
qdef![prm_shop_tracking_id] = Me.shop_tracking_id
qdef![prm_asset] = Me("asset" & i).Value
qdef![prm_hours] = Me("hours" & i).Value
qdef![prm_dsl] = Me("dsl" & i).Value
qdef![prm_def] = Me("def" & i).Value
qdef![prm_cool] = Me("cool" & i).Value
qdef![prm_W10] = Me("W10_" & i).Value
qdef![prm_W30] = Me("W30_" & i).Value
qdef![prm_W40] = Me("W40_" & i).Value
qdef![prm_glass] = Me("glass" & i).Value
qdef![prm_fittings] = Me("fittings" & i).Value
qdef![prm_airfilter] = Me("airfilter" & i).Value
qdef![prm_wheels] = Me("wheels" & i).Value
qdef![prm_hose] = Me("hose" & i).Value
qdef![prm_mirrors] = Me("mirrors" & i).Value
qdef![prm_battery] = Me("battery" & i).Value
qdef![prm_lights] = Me("lights" & i).Value
qdef![prm_grease] = Me("grease" & i).Value
qdef![prm_leaks] = Me("leaks" & i).Value
qdef![prm_blowout] = Me("blowout" & i).Value
qdef![prm_comment] = Me("comment" & i).Value
' EXECUTE ACTION
qdef.Execute
End If
Next i
' RELEASE DAO OBJECTS
Set qdef = Nothing: Set db = Nothing