MS Access将记录移入字段

3

我有一个到一个不属于我且无法更改的数据库的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

任何建议?

你使用的编程语言是什么? - Spikolynn
另一个类似的问题是:http://stackoverflow.com/questions/747021/flatten-relational-data-for-summary-export,尽管那里的被接受的解决方案要求您选择单行包括多少个学生的上限。 - mavnn
考虑到这是Access,我认为我们可以安全地假设他正在使用VBA。 - Billious
1个回答

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

我同意这不是最有效的做事方式,但如果你只是为了报告而生成数据,这种方法可以在最小的麻烦下完成工作。这是我的处理方法。 - Billious

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接