我一直在网上搜索如何使用 VBA 和 SQL 在 Access 中运行查询并想出这个。一般的想法是,从组合框中选择的值应该运行基于 ID Selected 的查询,例如。用户选择公司表中 ID 为 5 的公司。然后用相关数据填充下面的文本框。我还有另一个包含员工数据的表,其中与使用 CompanyID 作为主键的公司表有关系,如果员工公司 id = 5,他们在这种情况下为 asda 工作。出于某种原因,在运行此查询时,我收到一条错误消息,指出可能存在评估函数的错误
Public Function DataLookup()
Dim CompDetailSQL As String
Dim rst As DAO.Recordset
Dim CompID As String
CompID = Me.lstBoxCompanyName.Value
CompDetailSQL = "SELECT Companies.CompanyID, Companies.CompanyName, Companies.AddressNo, Companies.AddressLine1, Companies.AddressLine2, Companies.AddressLine3, Companies.AddressPostcode, Companies.AddressCounty, Link_Table.FirstName, Link_Table.LastName FROM Companies INNER JOIN Link_Table ON Companies.CompanyID = Link_Table.CompanyID WHERE " = CompID
Set rst = CurrentDb.OpenRecordset(CompDetailSQL, dbOpenSnapshot)
Me.lblAddressLine1.Value = rst!Companies.AddressLine1
Me.lblAddressLine2.Value = rst!Companies.AddressLine2
Me.lblAddressLine3.Value = rst!Companies.AddressLine3
Me.lblAddressPostcode.Value = rst!Companies.AddressPostcode
Me.lblAddressCounty.Value = rst!Companies.AddressCounty
rst.Close
Set rst = Nothing
End Function