我寫了一個函數,唯一的目的是要遍歷所有形式的連續形式,從「所有者」字段搶名稱,然後創建一個集合了出來只包含唯一值(沒有重複的名字)。下面比較兩個變體時類型不匹配,爲什麼?
的代碼是我當前的代碼,我意識到這可能似乎是做我想做的,但一些不可預見的問題,阻止我這樣做,我想的方式迂迴的方式。所以,雖然我意識到代碼並不是非常有效(而且編碼非常粗糙),但如果只是爲了學習體驗,我想完成這條路。這行代碼總是給我一個類型不匹配的錯誤消息。我用一個分界線,看看這些變量都在本地窗口,它們都包含這應該是相同的,因此應返回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變成了自己的數組?
你可以使用,而不是一個集合的Dictionary對象。字典對象有一個返回布爾值的'.Exists'方法。 – 2014-09-12 15:18:59
我肯定會給你一個機會,因爲如果是這樣的話,我知道至少有另外4個地方可以使用那個哈哈。雖然你看到爲什麼這種比較似乎不起作用? – Newd 2014-09-12 15:20:46
我不認爲我可以使用Split功能進入字典嗎? – Newd 2014-09-12 15:33:35