简体   繁体   English

在MS Access中,我需要查找记录并将其用作报告的“控制源”

[英]In MS Access I need to look up a record and use it as a 'Control Source' for a report

My situation is that I need to pull an additional name for a report. 我的情况是我需要为报告添加其他名称。 The name I need has to match a specific employee ID . 我需要的姓名必须与特定的员工ID匹配。

I read that I could use =DLookup() to do this but I am getting a problem. 我读到可以使用=DLookup()来执行此操作,但遇到了问题。 Here is my current attempt at solving this problem. 这是我当前解决这个问题的尝试。

I set up a query called [EE Query] with the contents 我用内容设置了一个名为[EE Query]的查询

SELECT Employees.FirstName, Employees.LastName 
FROM Employees WHERE (((Employees.ID)=[Escalations].[EE]));

In my report I used the following as my control source in the desired field "=DLookUp([FirstName],[EE Query])" 在我的报告中,我将以下内容用作所需字段"=DLookUp([FirstName],[EE Query])"控制源

So when I run my report, I get an input box that asks me to enter a value for "EE Query" . 因此,当我运行报表时,会看到一个输入框,要求我输入"EE Query"的值。 I find this strange as that isn't a parameter. 我发现这很奇怪,因为这不是参数。
So I assumed maybe I messed up and it was asking for a value for [Escalations].[EE] however when I entered the ID for the desired EE, my textbox shows "#Error" 因此,我想也许是我搞砸了,它在询问[Escalations].[EE]的值[Escalations].[EE]但是,当我输入所需EE的ID时,我的textbox shows "#Error"

I do not know where I went wrong. 我不知道我哪里出了错。

Also if it helps here is my Record Source for the report, keep in mind that the FirstName field in this refers to a TSE and not to the needed EE. 同样,如果这对我的报告记录来源有所帮助,请记住,其中的“名字”字段是指TSE而不是所需的EE。

SELECT [Report Query].DateTime, [Report Query].EscalationNeed, 
[Report Query].EscalationOutcome, Employees.FirstName, Employees.LastName, 
[Report Query].TSE, [Report Query].EE
FROM Employees INNER JOIN [Report Query] 
ON Employees.[ID] = [Report Query].[TSE];

[Escalations] [Employees] [Titles] are all tables. [上报] [雇员] [标题]都是表格。 Access 2010 Access 2010

Here are two different solutions 这是两种不同的解决方案

SOLUTION 1 - in VBA 解决方案1-在VBA中


This solution can be useful if you need to create text box dynamically. 如果您需要动态创建文本框,则此解决方案很有用。 In the Form_Open event add this 3 lines of code 在Form_Open事件中,添加以下3行代码

Dim s As String
s = "=DLookup(""FirstName"",""Employees"",""(Employees.ID)=[Escalations].[EE]"")"
txtYourTextBox.ControlSource = s

SOLUTION 2 - Directly in the interface 解决方案2-直接在界面中


Set ControlSource property of your control to =DLookup("FirstName";"Employees";"Employes.ID=Escalations.EE") 将控件的ControlSource属性设置为= DLookup(“ FirstName”;“ Employees”;“ Employes.ID = Escalations.EE”)

WARNING 警告

Please, note that there is a fundamental difference (:-() when you use VBA and the interface: the separator between the arguments in the 1st case is comma (,), in the 2nd case is the semicolomn (;). 请注意,使用VBA和接口时,存在根本区别(:-():第一种情况下参数之间的分隔符为逗号(,),第二种情况下为分号(;)。

I have not solved the actual problem but I changed my DB design so that it is easier to work with in access. 我还没有解决实际的问题,但是我更改了数据库设计,以便更轻松地进行访问。

To do this I created a separate table for the EE and TSE type employees so that I could easily include the names of each in the record source for my report. 为此,我为EE和TSE类型的员工创建了一个单独的表,这样我就可以轻松地在报表的记录源中包括每个人的姓名。 I then set the needed boxes to the now available Control Source fields for the EE name. 然后,将所需的框设置为EE名称的现在可用的“控制源”字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM