MS Access Collection阵列成员未保持“设置”

时间:2012-02-02 03:37:31

标签: ms-access

所以,我想要一组整数整数元组。

对于集合,我使用集合。

然后我调用子程序来填充集合。我通过引用参数发送集合。有一个循环重新定义数组,然后使用集合的add方法将该数组(元组)添加到集合中。检查是否添加重复的成员。

但是,随着子例程的完成,返回定义集合的级别,所有这些条目都“不再设置”。

这似乎是数据持久性的问题。我发现使用相同的变量来填充集合存在危险。但是,如果那是问题(重新定义也会改变旧成员),那么在一天结束时集合中是否至少会有某些内容?

另一方面,我怀疑数组在已终止的子程序中被定义(Dim)时消失。如果是这种情况,如何使用副本而不是引用来填充数组?

我意识到解决方案是使用表格或记录集,但是因为我花了很多时间在这个集合上,所以我至少想在继续之前理解它。欢呼声。

ERROR UPDATE

我忘记提供错误代码:它是3420,“对象无效或不再设置。”

代码更新

好的,这里有。 (如果你看到一些不相关的坏习惯,请告诉我。)

' starts here
Private Sub cmdGetConfigurations_Click()
    Dim configs As New Collection
    get_student_configurations configs

'    This is just for testing and it works.
'    The other stuff (not out-commented) does not work.
'    Dim work_config As Variant
'    work_config = Array("1", "2")
'    add_config configs, work_config
'    print_config configs(1)

    Dim work_config As Variant

    For Each work_config In configs
        ' This printing will not work (compare above)
        print_config work_config
    Next work_config
End Sub

' ... continues here ...
Public Sub get_student_configurations(ByRef configs As Object)
    Dim db As DAO.Database
    Set db = CurrentDb

    Dim students As DAO.Recordset
    Set students = db.OpenRecordset("Tv_Students")

    Dim work_config As Variant

    Do While Not students.EOF
        work_config = _
            Array(students!CourseIdNo, students!PracticeLocationNo)
        If complete_config(work_config) Then
            add_config configs, work_config
        End If
        students.MoveNext
    Loop
    students.Close
    db.Close
End Sub

' ... third
Public Sub add_config(_
         ByRef configs As Object, _
       ByRef add_config As Variant)
    Dim work_config As Variant
    For Each work_config In configs
        If config_equality(add_config, work_config) Then
            Exit Sub
        End If
    Next work_config
'    This fires so I know I add stuff:
'    MsgBox "Adding: "
'    print_config add_config
    configs.add add_config
End Sub

CODE UPDATE 2

Const COURSE_INDEX As Integer = 0
Const LOCATION_INDEX As Integer = 1

Public Sub print_config(ByRef config As Variant)
    MsgBox _
        "Course: " & str(config(COURSE_INDEX)) & CRLF & _
        "Location: " & str(config(LOCATION_INDEX))
End Sub

Public Function complete_config(ByRef config As Variant) As Boolean
    Dim course_null As Boolean
    course_null = IsNull(config(COURSE_INDEX))

    Dim location_null As Boolean
    location_null = IsNull(config(LOCATION_INDEX))

    complete_config = Not (course_null Or location_null)
End Function

0 个答案:

没有答案