Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 在添加到VBA集合时,按顺序迭代VBA集合_Ms Access_Vba - Fatal编程技术网

Ms access 在添加到VBA集合时,按顺序迭代VBA集合

Ms access 在添加到VBA集合时,按顺序迭代VBA集合,ms-access,vba,Ms Access,Vba,我试图在构建集合时迭代集合,原因是我有一个由三列组成的表: ID、ParentID、CategoryName 其中,表中的记录可以是另一个类别的“子项”,方法是填写ParentID列 作为表单删除功能的一部分,我需要检查所选项目: A) 有任何子类别 B) 是否有属于该类别的任何产品 C) 任何儿童类别中都有儿童和产品 然而,我制作的代码示例并没有达到我预期的效果,我尝试使用For,Next循环,但这只适用于集合,就像我启动循环时一样,在循环中添加到集合会被忽略 然后我尝试使用一个变量作为索引,

我试图在构建集合时迭代集合,原因是我有一个由三列组成的表: ID、ParentID、CategoryName

其中,表中的记录可以是另一个类别的“子项”,方法是填写ParentID列

作为表单删除功能的一部分,我需要检查所选项目:
A) 有任何子类别
B) 是否有属于该类别的任何产品
C) 任何儿童类别中都有儿童和产品

然而,我制作的代码示例并没有达到我预期的效果,我尝试使用For,Next循环,但这只适用于集合,就像我启动循环时一样,在循环中添加到集合会被忽略

然后我尝试使用一个变量作为索引,并将其递增,直到超过列表计数。但是,这会抛出一个错误3420-“对象无效或不再设置”

如果有人能帮助我,或者帮助我理解为什么我会犯这个错误,或者帮助我找到更好的方法,我将不胜感激

最终结果是我需要一个包含类别ID和所有子ID的列表

我的代码示例是:

Dim cat_index As Integer
Dim cat_list As New Collection

cat_list.Add ListCategories.Column(0) 'add top category id to list

cat_index = 1

Dim cat_rst As DAO.Recordset 'recordset to hold search results

Do Until cat_index > cat_list.Count
    MsgBox "Debug:" & vbCrLf & "cat_id:" & cat_id & vbCrLf & "cat_index:" & cat_index & vbCrLf & "cat_list.Count:" & cat_list.Count 'fault finding
    cat_id = cat_list.Item(cat_index)
    Set cat_rst = CurrentDb.OpenRecordset("SELECT ID FROM Categories WHERE ParentID = " & cat_id & ";")
    If cat_rst.RecordCount > 0 Then
        Do Until cat_rst.EOF 'loop until end of records
            MsgBox cat_rst![ID]
            cat_list.Add cat_rst![ID]
            cat_rst.MoveNext
        Loop
        cat_rst.Close
    End If
    cat_index = cat_index + 1
Loop
另外,如果有什么不同,我会使用access 2013

编辑: 为了澄清这一点,我总共有三个与此功能相关的表:
类别具有ID、ParentID、CategoryName
产品具有ID、名称
产品类别有ProductID、CategoryID


如果只有前两个表有主键(ID列),这意味着我使用前两个表定义类别和产品,第三个表将产品分类,这样一个产品可以有多个类别。

我在尝试使用“集合”时也遇到了奇怪的结果(尽管项目计数在增加,但无法引用)。因此,我将代码转换为使用字典

为了澄清你的A、B、C“规则”,似乎你需要更多的代码来满足这三个要求。但我不知道你所说的“产品”是什么意思?它们是如何关联/定义的

Private Sub cmdArray_Click()
Dim cat_index   As Integer
Dim cat_List    As Dictionary
Dim cat_id      As Integer
Dim strSQL      As String
Dim cat_rst     As DAO.recordSet    'recordset to hold search results

    Set cat_List = New Dictionary
    'Tabe contains:  ID, ParentID, CategoryName

    cat_List.add CStr(ListCategories.Column(0)), ListCategories.Column(0)
    Debug.Print "Added Value of: " & ListCategories.Column(0)
    Debug.Print "cat_List contains: " & cat_List.Count & " entries"

    cat_index = 0           ' Relative to zero

    Do              'Do Until cat_index > cat_List.Count
        cat_id = cat_List.Items(cat_index)

        Debug.Print "cat_id: " & vbTab & cat_id & vbTab & "cat_index: " & vbTab & cat_index & vbTab & "cat_list.Count: " & vbTab & cat_List.Count  'fault finding
        strSQL = "SELECT * FROM Categories WHERE ParentID = " & cat_id & ";"
        Set cat_rst = CurrentDb.OpenRecordset(strSQL)
        If Not cat_rst.EOF Then
            Do Until cat_rst.EOF        'loop until end of records
                Debug.Print "    Found ID: " & cat_rst![ID] & vbTab & "Parent: " & cat_rst!ParentID & vbTab & "CategoryName: " & cat_rst!CategoryName
                cat_List.add cat_rst![ID], CStr(cat_rst![ID])
                cat_rst.MoveNext
            Loop
        Else
            Debug.Print "    No records found for: " & cat_id
        End If
        cat_rst.Close

        cat_index = cat_index + 1

        If cat_index >= cat_List.Count Then Exit Do
    Loop
End Sub

我想我迷路了……你说‘作为删除函数的一部分……’,但我看到你的代码正在向集合中添加更多的项目?‘ListCategories’包含哪些内容?你的代码可能会添加重复项?它将添加到集合中,因为我首先需要一个所有子类别的完整列表,然后我希望使用它来创建一个所有产品的列表属于它们中的任何一个,然后提供删除所有这些表entriesthank的选项。为此,我直到现在都不知道dictionary对象。虽然要使其与我的access版本一起工作,我必须添加对microsoft脚本运行时的引用,并改为使用scripting.dictionary。