2013-08-02 28 views
1

我試圖.FindLast搜索一個特定的記錄,它與一個標準,但是當我試圖使用.FindLast與多個標準它停止工作。然而,我用.FindFirst幾乎使用了相同的語句,它的工作原理使我感到困惑。如何在VBA中使用.Find使用多個標準?

我得到的錯誤是「標準表達式中的數據類型不匹配」。錯誤是這條線:rst.FindLast(「DONOR_CONTACT_ID ='strDonor1'和ORDER_NUMBER ='strOrderNum1'」)。我通過我的代碼和行.FindFirst(「DONOR_CONTACT_ID ='strDonor1'和ORDER_NUMBER ='strOrderNum1'」)然而正常工作。

Option Compare Database 
Option Explicit 

Public dbs As DAO.Database 
Public rst As DAO.Recordset 
Public rstOutput As DAO.Recordset 
'Defines DAO objects 
Public strDonor1 As Variant 
Public strDonor2 As Variant 
Public strRecip1 As Variant 
Public strRecip2 As Variant 
Public strOrderNum1 As Variant 
Public strOrderNum2 As Variant 
Public strLastDonor As Variant 

Function UsingTemps() 

Set dbs = CurrentDb 
Set rst = dbs.OpenRecordset("T_RECIPIENT_SORT", dbOpenDynaset) 
'rst refers to the table T_RECIPIENT_SORT 
Set rstOutput = dbs.OpenRecordset("T_OUTPUT", dbOpenDynaset) 
'rstOutput refers to the table T_OUTPUT 

rst.MoveFirst 
'first record 
strDonor1 = rst!DONOR_CONTACT_ID 
'sets strTemp1 to the first record of the DONOR_CONTACT_ID 
strRecip1 = rst!RECIPIENT_CONTACT_ID 
strOrderNum1 = rst!ORDER_NUMBER 
rst.MoveNext 
'moves to the next record 

Do While Not rst.EOF 
'Loop while it's not the end of the file 
    strDonor2 = rst!DONOR_CONTACT_ID 
    'strTemp2 = DONOR_CONTACT_ID from T_RECIPIENT_SORT 
    strRecip2 = rst!RECIPIENT_CONTACT_ID 
    strOrderNum2 = rst!ORDER_NUMBER 
    'Sets strRecip = RECIPIENT_CONTACT_ID FROM T_RECIPIENT_SORT 
    With rstOutput 
    'Uses T_OUTPUT table 
    If (strDonor1 = strDonor2) And (strOrderNum1 = strOrderNum2) Then 
    'Runs if temps have same DONOR_CONTACT ID 

      If .RecordCount > 0 Then 
      'If table has records then you can check 

       rst.FindLast ("DONOR_CONTACT_ID= 'strDonor1' AND ORDER_NUMBER= 'strOrderNum1'") 
       strLastDonor = rst!RECIPIENT_CONTACT_ID 
       If strLastDonor = strRecip2 Then 
        Call LastDonor 
       Else 
        Call FirstDonor 
       End If 
      Else 
      'No records in T_Output so needs to add first record 
       .AddNew 
       !DONOR_CONTACT_ID = strDonor1 
       !RECIPIENT_1 = strRecip1 
       !ORDER_NUMBER = strOrderNum1 
       .Update 
      End If 
    Else 
     .FindFirst ("DONOR_CONTACT_ID= 'strDonor1' and ORDER_NUMBER= 'strOrderNum1'") 
     If .NoMatch Then 
      .AddNew 
      !DONOR_CONTACT_ID = strDonor1 
      !RECIPIENT_1 = strRecip1 
      !ORDER_NUMBER = strOrderNum1 
      .Update 
     End If 

    End If 
    End With 
    'Slides variables down 
    rst.FindFirst "[RECIPIENT_CONTACT_ID] = " & strRecip2 
    strDonor1 = strDonor2 
    strRecip1 = strRecip2 
    strOrderNum1 = strOrderNum2 
    rst.MoveNext 

Loop 

Call LastRecord 

Set dbs = Nothing 
Set rst = Nothing 
Set rstOutput = Nothing 

End Function 

編輯:

我剛添加以下代碼:

Dim strFind As Variant 
strFind = "DONOR_CONTACT_ID= '" & strDonor1 & "' AND ORDER_NUMBER= '" & strOrderNum1 & "'" 
Debug.Print strFind 
rst.FindLast strFind 

它顯示此與Debug.Print:

DONOR_CONTACT_ID= '10136851341' AND ORDER_NUMBER= '112103071441001' 

這些是DONOR_CONTACT_ID正確的值和ORDER_NUMBER,但我收到錯誤「數據類型不匹配在標準表達式」與rst.F行indLast strFind。可能是因爲我將變量定義爲變體?在表格中,我將DONOR_CONTACT_ID定義爲11精度的Decimal,將RECIPIENT_CONTACT_ID定義爲10精度的Decimal,將ORDER_NUMBER定義爲具有15精度的Decimal。然後我將代碼中的變量定義爲變體。你認爲這可能有問題嗎?

回答

4

我覺得你排除故障的努力將是,如果你改變這種容易...

rst.FindLast ("DONOR_CONTACT_ID= 'strDonor1' AND ORDER_NUMBER= 'strOrderNum1'") 

到這樣的事情...

Dim strFind As String 
strFind = "DONOR_CONTACT_ID= 'strDonor1' AND ORDER_NUMBER= 'strOrderNum1'" 
Debug.Print strFind 
rst.FindLast strFind 

當代碼拋出錯誤,或根本找不到你所期望的,轉到立即窗口(Ctrl + g)並檢查的輸出。您可能會立即發現問題。如果不是,請複製Debug.Print輸出,在查詢設計器中打開一個新查詢,切換到SQL View並使用WHERE子句中的複製文本。在這種情況下,我想查詢的SQL可能是:

SELECT * 
FROM T_RECIPIENT_SORT 
WHERE yadda_yadda; 

更換yadda_yadda你從即時窗口複製的文本。

這更像是一般的故障排除建議。對於這個特定的問題,我認爲你正在構建Find文本以包含名稱的變量,而不是那些變量的值爲的值。看看你在Debug.Print這兩個字符串表達式中得到了什麼。

"DONOR_CONTACT_ID= 'strDonor1' AND ORDER_NUMBER= 'strOrderNum1'" 
"DONOR_CONTACT_ID= '" & strDonor1 & "' AND ORDER_NUMBER= '" & strOrderNum1 & "'" 

你的代碼使用了第一個,但我認爲你實際上需要第二個。

在您更新的問題中,您報告的DONOR_CONTACT_IDORDER_NUMBER都是數字數據類型。在這種情況下,請勿在Find字符串中引用這些搜索值。

"DONOR_CONTACT_ID= " & strDonor1 & " AND ORDER_NUMBER= " & strOrderNum1 
+0

我編輯了我添加代碼時發生的事情的問題。絕對是朝着正確方向邁出的一步,但仍然是一個錯誤。 – nedstark179

+0

刪除'Find'字符串中的引號。查看更新的答案。 – HansUp

+0

工作!再次感謝HansUp! – nedstark179

0

我們能否在DONOR_CONTACT_ID匹配但ORDER_NUMBER爲空的情況下丟失一些數據?我認爲Access會拋出你從這種情況中得到的那種錯誤。

FindFirst不會發生,除非第一次出現是罪魁禍首。

+0

感謝您的建議。我檢查了我的數據,所有的字段都填滿了,所以我不確定這是否是原因,除非我誤解了你所說的內容。 – nedstark179