VBA物业链?

时间:2015-11-12 19:58:16

标签: vba

如果您可以使用方法链接来获得结果,例如下面的行

cReportcard.student("doe").Metric("math").score

如何将物业带回连锁店?

我有一系列主题的自定义类(例如,数学,语言艺术,pe ...)。自定义课程有学生,分数,老师,课堂,时期。

我怎么能

cReportcard.student("doe").Metric("math").score=0.96

并将值链向后转到原始类集合?

我想这就像"父母" Worksheet类的属性。我只是想不出怎么做。

到目前为止,我设计了一个包含4个表(学生,公制,成绩,期间)的表格方案

Student(ID [pk], NAME, DOB)
Metric (ID [pk], NAME)
Period (ID [pk], METRIC_ID [fk], GRADE_LEVEL, START_TIME, LENGTH, CLASS_RM, TEACHER_ID [fk])
Grade (ID [pk], PERIOD_END_DATE, PERIOD_ID [fk], STUDENT_ID [fk], METRIC_SCORE

1 个答案:

答案 0 :(得分:2)

我认为您可能需要花一点时间来定义确切的用例,以确保这些表能够快速执行您需要的任何操作。但假设这是最终的结构,我会设计这样的类:

clsGrade:

'It would be better to have them all private and provide get/set properties
Private ID As Long
Public Student As clsStudent
Public Period As clsPeriod
Public Period_End_Date As Date
Public Metric_Score As Long

'This assumes that, per your example, the only fields necessary to find a "grade" are student name and metric
Public Sub Load(studentName As String, Metric As String)
    Dim rs As Recordset
    'SQL query to get the grade for a particular student/metric

    Set Student = New clsStudent
    Student.Load rs!STUDENT_ID

    Set Period = New clsPeriod
    Period.Load rs!PERIOD_ID

End Sub

Public Sub Save()
    'SQL query to save the grade

    'You would have to decide whether or not this cascades and saves the Student/Period/Metric info as well.
    Student.Save
    Period.Save
End Sub

clsMetric:

'It would be better to have them all private and provide get/set properties
Private ID As Long
Public Name As String

Public Sub Load(ID As Long)
    'SQL to load a metric by ID
End Sub

Public Sub Save()
    'SQL to save the metric
End Sub

clsPeriod:

'It would be better to have them all private and provide get/set properties
Private ID As Long
Public Metric As clsMetric
Public Grade_Level As Long
Public Start_Time As Date
Public Length As Date
Public Class_Rm As String
Public Teacher_ID As Long

Public Sub Load(ID As Long)
    Dim rs As Recordset
    'SQL to load a period by ID

    'Also load the child metric
    Set Metric = New clsMetric
    Metric.Load rs!METRIC_ID

End Sub

Public Sub Save()
    'SQL to save the period

    'Also save the child metric
    Metric.Save
End Sub

clsStudent:

'It would be better to have them all private and provide get/set properties
Private ID As Long
Public Name As String
Public DOB As Date

Public Sub Load(ID As Long)
    'SQL to load a student by ID
End Sub

Public Sub Save()
    'SQL to save the student
End Sub

然后你可以这样使用它们:

Public Sub Main()
    Dim grade As clsGrade
    Set grade = New clsGrade
    grade.Load "doe", "math"
    grade.Metric_Score = 0.96
    grade.Save
End Sub

如果你想简化它并获得想象力,你可以做Get / Let属性并添加一个"脏"对每个类进行控制以跟踪自加载以来是否有任何更改。这样你就不会保存那些没有改变的信息。