基于组合框值在文本框中显示多个值

ejk8hzay  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(329)

我有一张table——tblmain
我有4个id字段-mainid,fatherid,motherid和fullname
1表格-FRM主
1个组合框-cDomain
2个文本框-txtfather,txtmother
我正在尝试用vba编写一个sql语句,它将在组合框cbomain中选择一条记录,并通过这样做填充两个文本框。
罗伯特被选入cbomain,他的主要身份证号码是20
罗伯特的母亲是露丝,她有一个30岁的女儿
罗伯特父亲的身份证是40岁
因此,在txtfather中,它将显示记录40/fullname,而在txtmother中,它将显示记录mainid30的fullname。
我想添加文本字段,并显示露丝的母亲和罗伯特的父亲的父亲。
我有个主意,但不知道下一步该怎么办。

Dim sqlME As String
Dim sqlFATHER As String
Dim db As Database
Dim rs As DAO.Recordset

sqlFATHER = "SELECT * FROM tblMAIN WHERE MainID = " & Forms![MAIN]![cboMAIN] & ";"

'AND NOT SURE WHAT I NEED TO DO HERE!

 Set db = CurrentDb
 Set rs = db.OpenRecordset(sqlFATHER)
 Me.txtFather.Value = rs!FullName
 Set rs = Nothing
 Set db = Nothing
dgtucam1

dgtucam11#

未绑定文本框的vba设置值将为每条记录显示相同的值。
这些数据本质上是递归的,accesssql不容易操作递归的数据。其他数据库平台也有一些实用程序需要更好地处理。
生成名为qryancestors的自联接查询对象:

SELECT tblMain.mainID, tblMain.FullName, tblMain_1.FullName AS Father, 
tblMain_2.FullName AS Mother, tblMain_3.FullName AS PGFather, 
tblMain_4.FullName AS PGMother, tblMain_5.FullName AS MGFather, tblMain_6.FullName AS MGMother
FROM tblMain AS tblMain_6 
RIGHT JOIN (tblMain AS tblMain_5 
RIGHT JOIN (tblMain AS tblMain_4 
RIGHT JOIN (tblMain AS tblMain_3 
RIGHT JOIN (tblMain AS tblMain_2 
RIGHT JOIN (tblMain AS tblMain_1 
RIGHT JOIN tblMain 
ON tblMain_1.mainID = tblMain.FatherID) 
ON tblMain_2.mainID = tblMain.MotherID) 
ON tblMain_3.mainID = tblMain_1.FatherID) 
ON tblMain_4.mainID = tblMain_1.MotherID) 
ON tblMain_5.mainID = tblMain_2.FatherID) 
ON tblMain_6.mainID = tblMain_2.MotherID;

然后使用该查询的选项:
引用查询作为combobobox rowsource,然后textbox controlsource按索引引用combobox列 =[cboMain].Column(2) textbox controlsource使用dlookup()表达式,例如: =DLookUp("Father", "qryAncestors", "mainID=" & mainID) textbox controlsource调用vba自定义函数以返回值,如: =GetAncestor(mainID, "Father") ```
Function GetAncestor(intID As Integer, strAnc As String)
GetAncestor = DLookUp(strAnc, "qryAncestors", "mainID=" & intID)
End Function

如果你想超越祖父母,达到任何一个层次,方法就必须完全不同。递归过程很棘手。返回祖先全名的函数可以如下所示:

Function GetAncestor(intID As Integer, intGen As Integer, strParent As String)
Dim x As Integer
GetAncestor = intID
For x = 1 To intGen
GetAncestor = DLookup(strParent, "tblMain", "mainID=" & Nz(GetAncestor,0))
Next
GetAncestor = DLookup("FullName", "tblMain", "mainID=" & Nz(GetAncestor,0))
End Function

要获取特定生成的祖先,请调用函数: `GetAncestor(mainID, 1, "MotherID")` 

相关问题