我有一个ODBC连接到我不拥有的数据库,无法更改。我要做的是将相关记录合并到一个记录中。这种关系是一对多。
我有一个学生管理系统,想要导出一个呼叫列表,该列表提供自动呼出服务(通过呼叫收费)。如果有多个学生住在那里,我希望能够只召集一次房子。
所需的呼出文件结构:
PHONE Inst1 Inst2 Inst3
555-5555 John was absent today Jane was absent today Joe was absent today
与现有数据相关:
PHONE Inst
555-5555 John was absent today
555-5555 Jane was absent today
555-5555 Joe was absent today
有什么建议吗?
答案 0 :(得分:1)
我的第一个倾向是使用交叉表查询;然而,这可能会有点毛茸茸。
如何在模块中剪切一些VBA代码以将它们连接在一起然后插入另一个表中(如下所示 - 完全未经测试并可能在某处破坏)?
dim strAbsent as string
dim currPhone as string
dim lastPhone as string
Dim db As Database
Dim rstAbsent As Recordset
Dim rstAbsentReport As Recordset
Set db = CurrentDb
Set rstAbsent = dbV.OpenRecordset("select * from Absent", _
dbOpenDynaset, dbSeeChanges)
Set rstAbsentReport = dbV.OpenRecordset("select * from AbsentReport", _
dbOpenDynaset, dbSeeChanges)
'...
do while not rstAbsentReport.EOF
currPhone = rstAbsentReport("phone")
lastPhone = currPhone
do while currPhone = lastPhone _
and not rstAbsentReport.EOF
strAbsent = strAbsent & ";" & rstAbsentReport ("comment")
'Yes I know concatenating strings this way is terribly inefficient
rstAbsentReport.MoveNext
if not rstAbsentReport.EOF then
currPhone = rstAbsentReport("phone")
end if
last
rstAbsent.AddNew
rstAbsent ("call") = strAbsent
rstAbsent.Update
loop
'... clean up of recordset variables left as an exercise