2016-05-16 57 views
0

我試圖用vba中的FindFirst命令訪問一個數組,該數組包含用戶名,並且我想通過一個表進行搜索並找到每個用戶名的ID陣列。但是當我按下訪問表單上的搜索按鈕時,我總是收到「編譯錯誤:類型不匹配」。訪問使用FindFirst命令與數組

任何想法? - 它是否看起來像我正確地傳遞數組到下一個私人子?

當我創建username()數組時,我試圖用數組搜索的那一點開始。

Private Sub createrel_Click() 
'declare variables 
    Dim stDocName As String, db As Database, RS As Recordset, FindPraNumber As String 
    Dim vary As Variant 
    Dim Msg As String 
    Dim Response As Integer 
    Dim username() As String 
    Dim varx() As Variant 

    If IsNull(Me![userlist]) Then 
     Msg = "Please choose a pra number from the list" 
     MsgBox Msg, vbCritical, MsgText 
     DoCmd.GoToControl "userlist" 
     Exit Sub 
    End If 

    If IsNull(Me![folderlist]) Then 
     Msg = "Please choose a folder from the list" 
     MsgBox Msg, vbCritical, MsgText 
     DoCmd.GoToControl "folderlist" 
     Exit Sub 
    End If 

    username() = Split(Me.userlist, ",") 
    MsgBox Join(username()) 
    Set db = DBEngine(0)(0) 

    Set RS = db.OpenRecordset("tblPra", DB_OPEN_DYNASET) 
     RS.FindFirst "[praNo] = """ & username() & """" 
     varx() = DLookup("praID", "tblPra", "[praNo] = 'username()'") 

    Set RS = db.OpenRecordset("tblFolder", DB_OPEN_DYNASET) 
     RS.FindFirst "[folder] = """ & Me.folderlist & """" 
     vary = DLookup("folderID", "tblFolder", "[folder] = " & "forms!frmrelationship!folderlist") 

    Response = MsgBox("You are about to create a relationship. Continue?", vbYesNo) 
    If Response = vbNo Then 
     Exit Sub 
    Else 
     cmdAddRecord varx(), vary 
    End If 
End Sub 

Private Sub cmdAddRecord(x(), y) 

    Dim stDocName As String, db As Database, RS As Recordset, FindPraNumber As String 
    Dim exists As Boolean 
    Dim total As Integer 

    Set db = DBEngine(0)(0) 
    Set RS = db.OpenRecordset("tblRelationship", DB_OPEN_DYNASET) 

    exists = False 
    If Not RS.EOF Then RS.MoveLast 
    total = RS.RecordCount 

    'check to see if relationship exists already 
    RS.FindFirst "[praID] = " & x() & "" 

    If RS.NoMatch Then 

    exists = False 

    Else 

     If RS("folderID") = y Then 
     exists = True 

     Else 

     For i = 1 To total Or exists = True 
     RS.FindNext "[praID] = " & x & "" 
     If RS.NoMatch Then 
     Else 
     If RS("folderID") = y Then exists = True 
     End If 
     Next i 

     End If 

    End If 

    If exists = False Then 
    RS.addNew 
    RS("praID").Value = x 
    RS("folderID").Value = y 
    RS.Update 
    Msg = "Relationship has now been created" 
    MsgBox Msg, vbInformation, MsgText 
    Else 
    Msg = "Relationship already exists" 
    MsgBox Msg, vbCritical, MsgText 
    End If 


End Sub 
+0

我想你需要加入數組作爲連接(username(),「或」)也許,但爲什麼不只是使用SQL記錄集,這些作爲標準? –

回答

3

你不能做到這一點:

varx() = DLookup("praID", "tblPra", "[praNo] = 'username()'") 

您不能分配使用使用DLookup陣列,使用DLookup不能拉一組ID,用戶名()應該是用戶名(N) ,並且用戶名的連接是錯誤的。事實上,這句話中唯一有效的部分是「tblPra」和「[praNo] =」。

重新思考你的概念。當直接記錄或查詢可以完成這項工作時,沒有理由使事情複雜化。

+0

+一個用於「重新思考你的概念」。 @PaulMachin:你這樣做太複雜了。 – Andre