2014-09-12 83 views
0

我寫了一個函數,唯一的目的是要遍歷所有形式的連續形式,從「所有者」字段搶名稱,然後創建一個集合了出來只包含唯一值(沒有重複的名字)。下面比較兩個變體時類型不匹配,爲什麼?

的代碼是我當前的代碼,我意識到這可能似乎是做我想做的,但一些不可預見的問題,阻止我這樣做,我想的方式迂迴的方式。所以,雖然我意識到代碼並不是非常有效(而且編碼非常粗糙),但如果只是爲了學習體驗,我想完成這條路。這行代碼總是給我一個類型不匹配的錯誤消息。我用一個分界線,看看這些變量都在本地窗口,它們都包含這應該是相同的,因此應返回true的字符串。我似乎無法找到一種方法來使這種比較真正起作用。

ElseIf var = o Then 

碼(重註釋,以確保我很清楚):

Private Sub Command39_Click() 

    Dim intRecordCount As Integer 
    Dim rs As DAO.Recordset 
    Dim colNames As Collection 
    Set colNames = New Collection 

    Set rs = Me.RecordsetClone 

    intRecordCount = rs.RecordCount 

    DoCmd.GoToRecord , , acFirst 


    If intRecordCount > 0 Then 

     Dim thisCol As Collection 
     Set thisCol = New Collection 

     'For each record on the form 
     Do While Not rs.EOF 

      Dim str As String 
      Dim o As Variant 

      str = Me.txtOwners.Value & "" 

      'If the textbox isn't empty 
      If Len(str) > 0 Then 

       'Send the string containing names ("Bob, Cheryl, Jeff, Tim") 
       Set thisCol = SplitNames(str) 

       'Loop through all of the names found 
       For Each o In thisCol 

        Dim var As Variant 
        Dim blnFound As Boolean 

        'Loop through all names in the main collection 
        For Each var In colNames 

         'If the collection is empty simply add the first name 
         If colNames.Count = 0 Then 
          blnFound = False 

         'If the collection has items check each one to see if the name is already in the collection 
         'This line is where the problem lies, I can't find anyway to compare var to o 
         ElseIf var = o Then 
          blnFound = True 
         End If 

        Next var 

        'If the name was not found in the collection add it 
        If Not blnFound Then 
         colNames.Add (o) 
        End If 
       Next o 

      End If 

      'Go to the next record in the continuous 
      DoCmd.GoToRecord , , acNext 
      rs.MoveNext 
     Loop 

    End If 


End Sub 


'Accepts the name of the owners to be split 
Public Function SplitNames(strNames As String) As Collection 


    Dim colNames As Collection 
    Dim strThisName As String 

    Set colNames = New Collection 

    'Replaces ("Bob, Cheryl, Jeff, Tim") with ("Bob Cheryl Jeff Tim") 
    'I realize this isn't really needed simply my OCD requires I do 
    strNames = Trim(Replace(strNames, ", ", " ")) 

    'Create the collection of names 
    colNames.Add (Split(strNames, " ")) 

    'Send back the collection 
    Set SplitNames = colNames 

End Function 

更新 - 出於某種原因,我需要使用VAR訪問VAR字符串屬性格式(0),所以它看起來像不知何故var變成了自己的數組?

+1

你可以使用,而不是一個集合的Dictionary對象。字典對象有一個返回布爾值的'.Exists'方法。 – 2014-09-12 15:18:59

+0

我肯定會給你一個機會,因爲如果是這樣的話,我知道至少有另外4個地方可以使用那個哈哈。雖然你看到爲什麼這種比較似乎不起作用? – Newd 2014-09-12 15:20:46

+0

我不認爲我可以使用Split功能進入字典嗎? – Newd 2014-09-12 15:33:35

回答

0

以下是更新後的代碼,對我有什麼需要做的工作。我正在添加一個字符串數組(由Split()函數創建),這是我添加的,而不是字符串值本身。

Private Sub Command39_Click() 

    Dim intRecordCount As Integer 
    Dim rs As DAO.Recordset 
    Dim dictNames As New Collection 

    Set rs = Me.RecordsetClone 

    intRecordCount = rs.RecordCount 

    DoCmd.GoToRecord , , acFirst 
    rs.MoveFirst 

    If intRecordCount > 0 Then 

     Dim dictTheseNames As New Collection 

     'For each record on the form 
     Do While Not rs.EOF 

      Dim str As String 
      Dim o As Variant 

      str = Me.Controls("Text27").Value & "" 

      'If the textbox isn't empty 
      If Len(str) > 0 Then 

       'Send the string containing names ("Bob, Cheryl, Jeff, Tim") 
       Set dictTheseNames = SplitNames(str) 

       'Loop through all of the names found 
       For Each o In dictTheseNames 

        Dim var As Variant 
        Dim blnFound As Boolean 
        blnFound = False 

        'Loop through all names in the main collection 
        For Each var In dictNames 

         'If the collection is empty simply add the first name 
         If dictNames.Count = 0 Then 
          dictNames.Add (o) 

         'If the collection has items check each one to see if the name is already in the collection 
         'This line is where the problem lies, I can't find anyway to compare var to o 
         ElseIf o = var Then 
          blnFound = True 
         End If 

        Next var 

        'If the name was not found in the collection add it 
        If Not blnFound Then 
         dictNames.Add (o) 
        End If 
       Next o 

      End If 

      'Go to the next record in the continuous 
      rs.MoveNext 

      If (rs.RecordCount - rs.AbsolutePosition) > 2 Then 
       DoCmd.GoToRecord , , acNext 
      End If 

     Loop 

    End If 


End Sub 


'Accepts the name of the owners to be split 
Public Function SplitNames(strNames As String) As Collection 


    Dim dictNames As New Collection 
    Dim strThisName As String 
    Dim strArray() As String 

    Set dictNames = New Collection 


    'Replaces ("Bob, Cheryl, Jeff, Tim") with ("Bob Cheryl Jeff Tim") 
    'I realize this isn't really needed simply my OCD requires I do 
    strNames = Trim(Replace(strNames, ", ", " ")) 

    'Create the array of names 
    strArray = Split(strNames, " ") 

    Dim o As Variant 
    For Each o In strArray 
     dictNames.Add (o) 
    Next o 

    'Send back the collection 
    Set SplitNames = dictNames 

End Function 
1

下面是將SplitNames函數修改爲Dictionary對象的示例。

儘管有一個Exists方法,您可以在代碼中使用elseh,但不需要使用它來確保唯一性。僅僅用同樣的方法指到密鑰(如果存在或覆蓋它)將創建它,這樣你就可以創建一個新的密鑰:

dict(key) = value 

注意,這將覆蓋主要的部分/值對。但是由於您的SplitNames函數僅僅構建了唯一名稱的「列表」,我不認爲這會是一個問題。例如起見,我簡單分配nullstring每個

我添加了一個可選的參數,這個功能允許您返回唯一的名稱要麼字典,集合(從字典轉換)。未經測試,但我認爲它應該起作用。如果您有任何問題,請告訴我。

Public Function SplitNames(strNames As String, Optional returnCollection as Boolean=False) As Object 
    'returns a Dictionary of unique names, _ 
    ' or a Collection of unique names if optional returnCollection=True 

    Dim dictNames as Object 
    Dim strThisName As Variant 
    Dim coll as Collection 
    Set dictNames = CreateObject("Scripting.Dictionary") 

    'Replaces ("Bob, Cheryl, Jeff, Tim") with ("Bob Cheryl Jeff Tim") 
    'I realize this isn't really needed simply my OCD requires I do 
    strNames = Trim(Replace(strNames, ", ", " ")) 

    'Create the collection of names 
    For Each strThisName in Split(strNames, " ") 
     dictNames(strThisName) = "" 
    Next 

    If Not returnCollection Then 
     Set SplitNames = dictNames 
    Else 
     Set coll = New Collection 
     For each strThisName in dictNames.Keys() 
      coll.Add strThisName 
     Next 
     Set SplitNames = coll 
    End If 
End Function 

所以我認爲你可以減少像這樣的程序:

Private Sub Command39_Click() 

    Dim intRecordCount As Integer 
    Dim rs As DAO.Recordset 
    Dim dictNames As Object 
    Dim collNames as Collection 
    Dim str As String 
    Dim o As Variant 

    Set rs = Me.RecordsetClone 

    intRecordCount = rs.RecordCount 

    DoCmd.GoToRecord , , acFirst 
    rs.MoveFirst 

    If intRecordCount > 0 Then 

     'For each record on the form 
     Do While Not rs.EOF 
      str = Me.Controls("Text27").Value & "" 

      'If the textbox isn't empty 

      If Len(str) > 0 Then 

       'Send the string containing names ("Bob, Cheryl, Jeff, Tim") 
       Set dictNames = SplitNames(str) 

       'Alternatively, if you want to work with the Collection instead: 
       Set collNames = SplitNames(str, True) 

      End If 
     Loop 
    End If 
End Sub 
相關問題