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

时间:2015-02-04 06:03:56

标签: access-vba ms-access-2007

为视障人士开设数据库,安排捐赠拾取(使用音频读取器进行字段)。有一张简单的桌子" Truck_Routes"包括客户ID,街道地址,套房,公司,城市,州,邮政编码,联系人姓名,联系电话号码以及多个捐赠信息字段。

尝试开发一个自动填充的表单:套件,公司,城市,州,邮政编码,联系人姓名和基于现有组合框的联系电话号码:cboAddress ....(街道地址)。花在网上的时间模仿其他例子,但无法让vba工作。

街道地址 - 带有" After_Update事件程序"

的组合框
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"进行导航。键,音频阅读器读取字段。每个字段的默认值是字段的名称,因此可以将其读取给操作员。有什么想法/建议吗?

1 个答案:

答案 0 :(得分:0)

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

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时,您都在上面的示例中对表执行READ,您在一个AfterUpdate中查询表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是否返回多行。
  2. 这不仅减少了数据库的命中率,这个代码也可以自行清理,而不像Domain函数。
  3. 希望这有帮助!