您的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时,都会对表执行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
还有其他优点,
-
使用
记录计数
属性。
-
这不仅减少了对数据库的影响,而且与域函数不同,这段代码还会自行清理。
希望这有帮助!