我有一个绑定到SQL语句的子表单。在子窗体中,我有几个绑定到此SQL字段的文本框。但是,我有另一个文本框需要绑定到来自不同SQL语句的字段,其中包含来自第一个的标准。我的代码如下所示:
Dim subform As Object
Dim formFilter As String
formFilter = "SELECT * FROM my_table_1"
Set subform = Me!my_subform.Form
subform.RecordSource = formFilter
subform.field1.ControlSource = "tb1f1"
subform.field2.ControlSource = "tb1f2"
...
subform.f3.ControlSource = "= SELECT TOP 1 tb2f3 FROM my_table_2 WHERE tb2f1 = '" & [tb1f1] & "' AND tb2f2 = '" & [tb1f2] "' ORDER BY tb2f4"
我不能直接在这里使用DLOOKUP函数,因为我需要对表结果进行排序。
提前感谢您的帮助。
答案 0 :(得分:0)
您不能将同一表单上的绑定控件绑定到2个不同的记录集。您唯一能做的就是从2个不同的记录集中获取 pull 数据,但这可能不是最好的做法。
为了做到这一点,你必须创建第二个记录集并在其中获取该值。类似的东西:
Dim db as Database
Dim rec as Recordset
Set db = CurrentDB
Set rec = db.OpenRecordset("SELECT TOP 1 tb2f3 FROM my_table_2 WHERE tb2f1 = '" & [tb1f1] & "' AND tb2f2 = '" & [tb1f2] "' ORDER BY tb2f4")
Me.MyControlName = rec(0)
答案 1 :(得分:0)
我想我会创建一个小函数来获得你想要的结果。最好简单地在你自己的函数中重写DLookup并添加sort,但我不会在这里这样做。
首先是表单代码。请注意,我没有设置记录源,只是可能不是您想要的值。
subform.f3 = fGet_tb2f3([tb1f1], [tb1f2])
然后是功能代码(放入你自己的错误处理中)
Public Function fGet_tb2f3(tblf1 as String,tblf2 as String) as String
Dim rst as dao.recordset
Dim db as database
set db = currentdb
set rst = db.openrecordset("SELECT TOP 1 tb2f3 FROM my_table_2 WHERE tb2f1 = '" & tb1f1 & "' AND tb2f2 = '" & tb1f2 "' ORDER BY tb2f4",dbopensnapshot
if not rst is nothing then
if not rst.eof then fGet_tb2f3 = rst!tb2f3
rst.close
set rst = nothing
end if
db.close
set db = nothing
end Function