Access 2007:自动填充的 DLookup 不返回值

Access 2007: DLookup for Autofill not returning values

为视障人士开发一个数据库来安排捐赠拾取(使用音频 reader 字段)。有一个简单的 table“Truck_Routes”,其中包含客户 ID、街道地址、套房、公司、城市、州、邮编、联系人姓名、联系人 phone 号码和几个捐赠信息字段。

尝试开发一个自动填写的表格:套房、公司、城市、州、邮政编码、联系人姓名和联系人 phone 基于现有组合框的号码:cboAddress ....(街道地址).花了几个小时在网上模拟其他示例,但无法使 vba 工作。

街道地址 - "After_Update event procedure"

组合框
Private Sub cboAddress_AfterUpdate()
  PopulateFields
End Sub

Private Sub PopulateFields()
  Me.Suite = DLookup("Suite", "Truck_Routes", "Street Address=" & Me.[cboAddress & "'"])
  Me.Company = DLookup("Company", "Truck_Routes", "Street Address=" & Me.[cboAddress & "'"])
  Me.City = DLookup("City ", "Truck_Routes", "Street Address=" & Me.[cboAddress & "'"])
  Me.State = DLookup("State ", "Truck_Routes", "Street Address=" & Me.[cboAddress & "'"])
  Me.zip = DLookup("zip ", "Truck_Routes", "Street Address=" & Me.[cboAddress & "'"])
  Me.ContactName = DLookup("ContactName ", "Truck_Routes", "Street Address=" & Me.[cboAddress & "'"])
  Me.ContactPHone = DLookup("ContactPhone ", "Truck_Routes", "Street Address=" & Me.[cboAddress & "'"])
End Sub

表单中的字段使用 "tab" 键导航,音频 reader 读取字段。每个字段的默认值是字段的名称,因此可以将其读取给操作员。任何 thoughts/recommendations?

您的 DLookup 有点偏离,方括号在引号之外。它们也应该用单引号括起来,因为它们是文本类型。我已经为你修好了。

Private Sub cboAddress_AfterUpdate()
    PopulateFields
End Sub

Private Sub PopulateFields()
    Me.Suite = DLookup("Suite", "Truck_Routes", "[Street Address] = '" & Me.cboAddress & "'")
    Me.Company = DLookup("Company", "Truck_Routes", "[Street Address] = '" & Me.cboAddress & "'")
    Me.City = DLookup("City ", "Truck_Routes", "[Street Address] = '" & Me.cboAddress & "'")
    Me.State = DLookup("State ", "Truck_Routes", "[Street Address] = '" & Me.cboAddress & "'")
    Me.zip = DLookup("zip ", "Truck_Routes", "[Street Address] = '" & Me.cboAddress & "'")
    Me.ContactName = DLookup("ContactName ", "Truck_Routes", "[Street Address] = '" & Me.cboAddress & "'")
    Me.ContactPHone = DLookup("ContactPhone ", "Truck_Routes", "[Street Address] = '" & Me.cboAddress & "'")
End Sub

附带说明一下,Domain 函数是一项相当昂贵的操作。它是一个简化的SQL。因此,每次您使用 DLookup 时,您都在对上例中的 table 执行 READ,您在一次 AfterUpdate 中查询了 table 7 次。如果您要使用 RecordSet 对象,这可能会大大减少。

有点像,

Private Sub cboAddress_AfterUpdate()
    PopulateFields
End Sub

Private Sub PopulateFields()
    Dim rsObj As DAO.Recordset

    Set rsObj = CurrentDB.OpenRecordset("SELECT Suite, Company, City, State, zip, ContactName, ContactPhone " & _
                                    "FROM Truck_Routes WHERE [Street Address] = '" & Me.cboAddress & "'")

    If Not rsObj.EOF Then
        Me.Suite = rsObj.Fields("Suite")
        Me.Company = rsObj.Fields("Company")
        Me.City = rsObj.Fields("City")
        Me.State = rsObj.Fields("State")
        Me.zip = rsObj.Fields("zip")
        Me.ContactName = rsObj.Fields("ContactName")
        Me.ContactPHone = rsObj.Fields("ContactPhone")
    Else
        MsgBox "No Information matched."
    End If

    Set rsObj = Nothing
End Sub

还有其他优点,

  1. 使用 RecordSet 对象的 RecordCount 属性 检查 Recordset 是否 returns 多于一行。
  2. 这不仅减少了对数据库的访问,而且与域函数不同,此代码还可以自行清理。

希望对您有所帮助!