首先,让我先说一下我对Access,VBA或SQL几乎一无所知。但是,当老板要求你做某事时,你会这样做。我提前为格式不佳,数据库管理不当以及我普遍缺乏知识而道歉。
无论如何,我在查询中有一个大表,我需要使用组合框来组织。查询的名称是FinalTable,我想要命名的组合框的字段是ID Maker.Billet Material,ID Maker.Billet Number,ID Maker.Test Type和ID Maker.Axis。更新每个框后,我想显示该行的其余字段。
到目前为止,这是我在表单代码中的内容。它是从我在Google上搜索时发现的教程中扯下来的,但我现在已经绝望地迷失了,并且无法弄清楚接下来要做什么。
Private Sub TestType_AfterUpdate()
On Error Resume Next
Dim query As String
Dim Fields(3)
Fields(0) = "[ID Maker.Billet Material]"
Fields(1) = "[ID Maker.Billet Number]"
Fields(2) = "[ID Maker.Test Type]"
Fields(3) = "[ID Maker.Axis]"
query = "Select DISTINCT {replace} " & _
"FROM FinalTable " & _
"WHERE [ID Maker.Test Type] = '" & TestType.Value & "' " & _
"WHERE [ID Maker.Billet Number] = " & BilletNumber.Value & " " & _
"WHERE [ID Maker.Billet Material] = '" & BilletMaterial.Value & "' " & _
"ORDER BY {replace};"
'Assign Queries
'=========================
Me.BilletMaterial.RowSource = Replace(query, "{replace}", Fields(0)) 'Billet Material Query
Me.BilletNumber.RowSource = Replace(query, "{replace}", Fields(1)) 'Billet Number Query
Me.TestType.RowSource = Replace(query, "{replace}", Fields(2)) 'Test Type Query
Me.Axis.RowSource = Replace(query, "{replace}", Fields(3)) 'Axis Query
'requery combobox's
Me.BilletMaterial.Requery
Me.BilletNumber.Requery
Me.TestType.Requery
Me.Axis.Requery
End Sub
Private Sub Axis_AfterUpdate()
On Error Resume Next
Dim query As String
Dim Fields(3)
Fields(0) = "[ID Maker.Billet Material]"
Fields(1) = "[ID Maker.Billet Number]"
Fields(2) = "[ID Maker.Test Type]"
Fields(3) = "[ID Maker.Axis]"
query = "Select DISTINCT {replace} " & _
"FROM FinalTable " & _
"WHERE [ID Maker.Axis] = '" & Axis.Value & "' " & _
"WHERE [ID Maker.Test Type] = '" & TestType.Value & "' " & _
"WHERE [ID Maker.Billet Number] = " & BilletNumber.Value & " " & _
"WHERE [ID Maker.Billet Material] = '" & BilletMaterial.Value & "' " & _
"ORDER BY {replace};"
'Assign Queries
'=========================
Me.BilletMaterial.RowSource = Replace(query, "{replace}", Fields(0)) 'Billet Material Query
Me.BilletNumber.RowSource = Replace(query, "{replace}", Fields(1)) 'Billet Number Query
Me.TestType.RowSource = Replace(query, "{replace}", Fields(2)) 'Test Type Query
Me.Axis.RowSource = Replace(query, "{replace}", Fields(3)) 'Axis Query
'requery combobox's
Me.BilletMaterial.Requery
Me.BilletNumber.Requery
Me.TestType.Requery
Me.Axis.Requery
我现在在那里的query = ...
语句会导致错误。在我尝试之前,只有第一个WHERE语句。
答案 0 :(得分:3)
很少有变化的项目。
正如Query a table that has spaces in its name, MS Access C# VS2008
中所述"用方括号包围间隔开的项目:
[公共电台]
然后拍打设计数据库的人。"
所以请试一试,让我知道它是怎么回事 - 注意:不能在我的系统上测试它。
Private Sub Material_AfterUpdate()
On Error Resume Next
Me.BilletNumber.RowSource = "Select [ID Maker.Billet Number] " & _
"FROM FinalTable " & _
"WHERE [ID Maker.Billet Material] = '" & Material.Value & "' " & _
"ORDER BY [ID Maker.Billet Number];"
Me.BilletNumber.Requery
End Sub
<强>更新强>
当然,您只获得唯一记录,您可以在访问中使用DISTINCT
命令。
其次,要使用相同的信息填充另一个组合框,您可以将查询存储在字符串中并重新查询这两个框。
Private Sub Material_AfterUpdate()
On Error Resume Next
Dim basequery As String
Dim Fields(3)
Fields(0) = "[ID Maker.Billet Material]"
Fields(1) = "[ID Maker.Billet Number]"
Fields(2) = "[ID Maker.Test Type]"
Fields(3) = "[ID Maker.Axis]"
basequery = "Select DISTINCT {replace} " & _
"FROM FinalTable " & _
"WHERE [ID Maker.Billet Material] = '" & Material.Value & "' " & _
"ORDER BY {replace};"
'Assign Queries
'=========================
'I do NOT know your combobox names.
'Me.COMBOBOXNAME.RowSource - Change COMBOBOXNAME.
Me.BilletMaterial.RowSource = Replace(query, "{replace}", Fields(0)) 'Billet Material Query
Me.BilletNumber.RowSource = Replace(query, "{replace}", Fields(1)) 'Billet Number Query
Me.TestType.RowSource = Replace(query, "{replace}", Fields(2)) 'Test Type Query
Me.Axis.RowSource = Replace(query, "{replace}", Fields(3)) 'Axis Query
'requery combobox's
Me.BilletMaterial.Requery
Me.BilletNumber.Requery
Me.TestType.Requery
Me.Axis.Requery
End Sub