破碎的VB6系列。无效的限定符

时间:2014-02-05 15:52:06

标签: vba collections vb6 qualifiers invalid-argument

我正在尝试使用集合来存储从数据库查询返回的每个字符串的唯一副本。

当我遇到一个新的。我看看我是否已经拥有它。如果我不这样做,我会把它添加到我的收藏夹和我的组合框中。否则我一直在解析。

当我编译时,我收到此错误(图片中的代码也是):

enter image description here

我需要更改我的收藏品?

3 个答案:

答案 0 :(得分:3)

我建议你试试这个。

Sub FillEstimatesRoofLaborTypeCombo()
    Dim rstEstimateFactorTypes As Recordset
    Dim Sqlstring As String

    Sqlstring = "Select Distinct FactorType " + _
                "From EstimateFactors " + _
                "Where Component = 'Roof' And ComponentPart = 'Labor' And FactorType Is Not NULL"

    Set rstEstimateFactorTypes = cnnSel.OpenRecordset(Sqlstring, dbOpenSnapshot)

    With rtsEstimateFactorTypes
        Do While Not .EOF
            frmEstimates.cboRoofLaborType.AddItem .Fields!FactorType
            .MoveNext
        Loop
    End With
End Sub

您会注意到代码编写起来要简单得多。我在查询的select子句中添加了一个distinct,并在FactorType Is Not NULL上添加了另一个where子句条件。这将导致数据库仅返回您想要的行,因此无需编写代码来过滤掉唯一值。

您还应该注意到此代码的执行速度要快得多。如果您只从数据库中获取十几行(使用原始代码),您可能不会注意到差异,但是如果行数越多,执行时间的差异就越明显。

答案 1 :(得分:1)

您已将mostRecentList定义为集合数组并使用它。

你写道:

Dim mostRecentList as Collection

[...]

Do While cnt < mostRecentList.count()

或者你写

Dim mostRecentList() as Collection

[...]

Do While cnt < mostRecentList(lindex).count()

此外,您需要在使用前实现您的收藏...

答案 2 :(得分:0)

我认为您需要更改收藏品的声明。因为这种方式你声明了集合数组,所以你得到了错误。除此之外,我同意G Mastros,您可以更改sql语句,让db服务器为您完成工作。 HTH

Sub FillEstimatesRoofLaborTypeCombo()
    ' this declares not a collection but a dynamic array of collections
    Dim mostRecentList() As Collection

    Debug.Print TypeName(mostRecentList)
    Debug.Print IsArray(mostRecentList)

    ' before using dynamic array ReDim must be used to alocate space in array
    ReDim mostRecentList(10)

    ' now mostRecentList has 10 elements each element is a collection
    ' before using any of this collection an new instance must be created
    Set mostRecentList(LBound(mostRecentList)) = New Collection

    ' now the first array element contains initialised instance of a collection
    ' and it is possible to add items to that collection
    mostRecentList(LBound(mostRecentList)).Add Item:="item_1", Key:="key_1"
    Debug.Print mostRecentList(LBound(mostRecentList)).Count

    ' *************************************************************************
    ' what you wanted was probably to have one collection instance like this
    ' declare without parenthesis
    Dim mostRecentListCol As Collection
    Set mostRecentListCol = New Collection

    Debug.Print TypeName(mostRecentListCol)
    Debug.Print IsArray(mostRecentListCol)
    Debug.Print mostRecentListCol.Count

End Sub
  

输出:

Object()
True
 1 
Collection
False
 0