简体   繁体   English

损坏的VB6集合。 无效的预选赛

[英]Broken VB6 Collection. Invalid Qualifier

I am attempting to use a collection to store only one unique copy of each string returned from a database query. 我试图使用一个集合来存储从数据库查询返回的每个字符串的唯一副本。

When I come across a new one. 当我遇到一个新的。 I see if I already have it. 我看看我是否已经拥有它。 If I do not, I add it to my collection and to my combobox. 如果不这样做,则将其添加到我的收藏夹和组合框中。 else I keep parsing. 否则我会继续解析。

When I compile, I get this error (code in picture too): 编译时,出现此错误(图片中也有代码):

在此处输入图片说明

What do I need to change about my collection? 我需要对收藏进行哪些更改?

I would suggest that you try this. 我建议您尝试一下。

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

You will notice that the code is a lot simpler to code. 您会注意到该代码更易于编写。 I added a distinct to the select clause of your query and added another where clause condition on FactorType Is Not NULL. 我向查询的select子句添加了一个独特的字符,并在FactorType上添加了另一个where子句条件为Not NULL。 This will cause the database to only return rows that you want so there is no need to write code to filter out unique values. 这将导致数据库仅返回所需的行,因此无需编写代码来过滤出唯一值。

You should also notice that this code executes considerably faster. 您还应该注意,此代码的执行速度明显加快。 If you're only getting a dozen or so rows from your database (with your original code) you may not notice a difference, but with more rows, the difference in execution time will become more obvious. 如果仅从数据库中获得十几行(使用原始代码),您可能不会注意到差异,但是如果行越多,执行时间的差异就会越明显。

You have defined mostRecentList as array of collections and use it driectly. 您已将mostRecentList定义为集合数组,并直接使用它。

Either you write: 您要么写:

Dim mostRecentList as Collection

[...]

Do While cnt < mostRecentList.count()

or you write 或者你写

Dim mostRecentList() as Collection

[...]

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

Besides you need to instanciate your collection before use ... 此外,您需要在使用前实例化收藏夹...

I think you need to change the declaration of your collection. 我认为您需要更改集合的声明。 Because this way you declared array of collections and therfore you got the error. 因为这样您就声明了集合数组,然后才得到错误。 Besides of this I agree with G Mastros, you can change the sql statement and so let the db server do the work for you. 除此以外,我同意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

Output: 输出:

Object()
True
 1 
Collection
False
 0 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM