【问题标题】:access use FindFirst command with an array通过数组访问使用 FindFirst 命令
【发布时间】:2016-05-16 16:09:18
【问题描述】:

我正在尝试使用 vba 中的 FindFirst 命令访问数组,该数组包含用户名,我想搜索一个表并找到数组中每个用户名的 ID。但是每当我按下访问表单上的搜索按钮时,我都会不断收到“编译错误:类型不匹配”。

有什么想法吗? - 看起来我是否将数组正确传递给下一个私有子?

当我创建 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

【问题讨论】:

  • 我认为您可能需要以 join(username()," OR ") 的形式加入数组,但为什么不使用 SQL 记录集,以这些为标准呢?

标签: arrays ms-access vba


【解决方案1】:

你不能这样做:

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

使用DLookup不能赋值数组,DLookup不能拉取ID数组,username()应该是username(n),username的拼接是错误的。事实上,这句话中唯一有效的部分是“tblPra”和“[praNo] =”。

所以重新考虑你的概念。当直接记录集或查询可以完成这项工作时,没有理由使事情复杂化。

【讨论】:

  • +一个用于“重新思考你的概念”。 @PaulMachin:你这样做太复杂了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 2018-11-06
  • 2012-10-03
相关资源
最近更新 更多