访问 - 将值存储在ComboBox中以用作运行查询的参数(运行查询按钮)

时间:2014-04-17 21:54:31

标签: sql vba combobox access-vba ms-access-2010

Form有2个ComboBox&(boxLaborCore,boxPosition)和一个Button(runQuery)。通过控制向导构建的按钮,一个运行查询按钮。运行具有名为LaborCore和Position的参数的查询。根据所选工作区域/位置的类型,在“选择查询”中显示不同的技能。当我手动输入人工核心/位置时,查询运行正常,我希望能够自动执行此操作。

我将查询中的条件从[输入人工核心]切换到

[Forms]![Form1]![boxLaborCore]

以及从[输入位置]到

[Forms]![Form1]![boxPosition]

我已经读过我需要编辑BoundColumn属性才能存储值。

有人能指出我正确的方向来解决这个问题吗? VBA代码会使用事件After_Update吗?

你知道造成这个问题的原因是什么吗?我的梦想形式如下所示:(步骤1)在组合框中选择劳动核心/位置(步骤3)单击运行查询的按钮。在具有指定参数的正确技能集的表中生成结果。

此处为查询的SQL代码。涉及5个表,2个劳动力和职位,一个表格,用于所有可能的劳动力/职位组合称为工作区,以及一个包含所有技能的表格:

    SELECT tblWorkArea.ID_WorkArea, tblWorkArea.[Labor Core], tblWorkArea.Position, tblSkills.Wiring, tblSkills.[Wire Type], tblSkills.[Terminal Types], tblSkills.[NEMA Ratings], tblSkills.[UL 508], tblSkills.[Attention to Detail], tblSkills.[Lifting 50lbs], tblSkills.[Knowledge of Procedure], tblSkills.Crimper, tblSkills.[Heat Gun], tblSkills.Screwdriver, tblSkills.Stripper, tblSkills.[Impact Drill], tblSkills.[Radial Saw], tblSkills.Multimeter, tblSkills.[Torque Screwdriver/Wrench], tblSkills.[Tape Measure], tblSkills.Vacuum, tblSkills.[Drill Press], tblSkills.[Jig Saw], tblSkills.[Troubleshooting Components], tblSkills.[Problem Solving], tblSkills.Organization, tblSkills.[Large Panel], tblSkills.[Test Procedures], tblSkills.[Functional Testing], tblSkills.[Writing Test Procedures], tblSkills.[Material Management], tblSkills.[Set Meter Ability], tblSkills.[Tone Generator], tblSkills.[Megger Testing], tblSkills.[Network Tracer], tblSkills.Components, tblSkills.Fuses, tblSkills.[Heat Shrink Color Codes], tblSkills.[Proper Lug or Crimping], tblSkills.[Resistors Knowledge], tblSkills.Oscilloscopes, tblSkills.Waveforms, tblSkills.[Voltage Separation], tblSkills.[Skill x_Stapling]

    FROM ((tblLaborCore INNER JOIN tblWorkArea ON tblLaborCore.[Labor Core] = tblWorkArea.[Labor Core]) INNER JOIN tblPosition ON tblWorkArea.Position = tblPosition.Postion) INNER JOIN tblSkills ON tblWorkArea.ID_WorkArea = tblSkills.ID_WorkArea

    WHERE (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Reading Schematics])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.Wiring)<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Wire Type])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Terminal Types])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[NEMA Ratings])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[UL 508])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Attention to Detail])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Lifting 50lbs])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Knowledge of Procedure])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.Crimper)<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Heat Gun])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.Screwdriver)<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.Stripper)<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Impact Drill])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Radial Saw])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.Multimeter)<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Torque Screwdriver/Wrench])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Tape Measure])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.Vacuum)<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Drill Press])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Jig Saw])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Troubleshooting Components])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Problem Solving])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.Organization)<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Large Panel])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Test Procedures])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Functional Testing])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Writing Test Procedures])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Material Management])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Set Meter Ability])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Tone Generator])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Megger Testing])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Network Tracer])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.Components)<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.Fuses)<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Heat Shrink Color Codes])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Proper Lug or Crimping])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Resistors Knowledge])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.Oscilloscopes)<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.Waveforms)<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblSkills.[Voltage Separation])<>0)) OR (((tblWorkArea.[Labor Core])=[Forms]![SkillsMatrix]![boxLaborCore]) AND ((tblWorkArea.Position)=[Forms]![SkillsMatrix]![boxPosition]) AND ((tblSkills.[Skill x_Stapling])<>0));

谢谢!

1 个答案:

答案 0 :(得分:0)

我使用的技巧是在字符串中构建查询,将字符串打印到即时窗口,并在视觉上对其进行修改即可。然后创建一个新查询并将该字符串用作SQL。当您尝试切换到设计视图时,Access将告诉您是否存在语法错误。如果没有语法错误,请尝试运行查询,看看您是否在数据集中得到了所需的内容。

在您创建的字符串中,不要将表单引用放在引号中。强制VBA在将这些字段值添加到字符串之前解析它们。执行此操作时,您对查询构建器执行的SQL将不包含任何表单引用。

PS&GT;如果任何表单引用解析为字符串,请确保在构建SQL语句时引用这些字符串。这与我上面写的内容没有冲突:你实际上是在你的SQL字符串中编码引号。