简体   繁体   English

MS Access表单:控件默认值和DLookup

[英]MS Access Forms: Control Default Values and DLookup

I've tried searching on here for my answer but have had no luck. 我试图在这里搜索我的答案,但是没有运气。 I've tried many things to get this to work but haven't been successful so I thought I would post my question. 我已经尝试了很多方法来使它起作用,但是还没有成功,所以我想我会提出我的问题。 I may repeat myself several times but I'm trying to be as specific/detailed as I can. 我可能会重复几次,但我会尽力而为。

I have a form which has an append query to transfer all fields on the form into a table. 我有一个带有附加查询的表单,可以将表单上的所有字段转移到表中。 On this form, I have an invisible control (textbox) which pulls the users Windows login ID and uses that as its default value. 在此表单上,我有一个不可见的控件(文本框),该控件拉出用户的Windows登录ID,并将其用作默认值。 I have another field (textbox) for the user's full name. 我还有一个用于用户全名的字段(文本框)。

I have ran a query to pull employee data from my tables to have the employee ID (same as their Window's login ID, first name, and last name. I have also ran an expression to concatenate the two name fields into one to show their full name. 我运行了一个查询,从表中提取员工数据以获取员工ID(与他们的Window的登录ID,名字和姓氏相同。我还运行了一个表达式,将两个名称字段连接为一个以显示其完整信息名称。

What I've been trying to do is have it so that when someone opens up the form, their full name is already pre-loaded. 我一直想做的就是拥有它,以便当有人打开表单时,他们的全名已经预先加载。 As I stated above, this is pre-loaded based on a comparison of their Window's login ID with their employee ID that we have on record as they're the same. 如前所述,这是根据他们的Window的登录ID与我们记录的他们相同的员工ID进行比较而预先加载的。 It's essentially Excel's version of vlookup() that I'm trying to do. 我要尝试的基本上是Excel的vlookup()版本。 I've tried dlookup() but was unsuccessful. 我尝试了dlookup(),但未成功。

Does anyone know how to make the default value of the employee name control on the form to be determined by employee ID control which has its default value based on the user's Window login ID? 有谁知道如何在要由员工ID控件确定的表单上设置员工名称控件的默认值,该控件的默认值基于用户的Window登录ID?

Example: Let's say that I have an employee called Fred Flintstone. 示例:假设我有一个叫Fred Flintstone的员工。 In the database there's a table with fields for his first name and last name. 在数据库中,有一个表,其中包含他的名字和姓氏的字段。 There's also a field for his employee ID which is A111111. 他的员工ID的字段也为A111111。

When opening up the form, there's an invisible textbox control which has its default value derived from the Windows login ID that the employee used to sign onto the computer with. 打开表单时,有一个不可见的文本框控件,其默认值源自员工用来登录计算机的Windows登录ID。 This Windows login ID happens to be A111111 which is the same as the employee's employee ID. 该Windows登录ID恰好是A111111,与员工的员工ID相同。

A query was created that has fields for the employee ID, First Name, Last Name, and an expression to concatenate the two name fields. 创建了一个查询,该查询具有用于员工ID,名字,姓氏的字段,以及用于连接两个姓名字段的表达式。 So the first row would be: | 因此,第一行将是: A111111 | A111111 | Fred | 弗雷德| Flintstone | 打火石| Fred Flintstone |. 弗雷德·弗林特斯通|。 I don't know how to make tables on here yet so bear with me. 我还不知道如何在这里做桌子,所以请多多包涵。

Going back to the form, there's a field for Employee Name. 返回表格,其中有一个“雇员姓名”字段。 How would I get it to show Fred Flintstone as the default value upon opening the form? 打开表单后,如何显示Fred Flintstone作为默认值?

Note: The employee name field will eventually be locked so that it cannot be changed by the employee. 注意:员工姓名字段最终将被锁定,以便员工无法更改。 I don't know if that would have any impact but I thought that I would mention it. 我不知道这是否会产生影响,但我想我会提及。

In the onload event of your form (really rough code, will require some tweaking..): 在表单的onload事件中(真正的粗略代码,将需要进行一些调整。):

dim db as dao.database
Dim rs as dao.recordset
Dim username as string 
dim SQL as string

username = Environ("USERNAME") 'There are better ways to get username, but this is just a quick example.

SQL = "SELECT * FROM Your_Query WHERE username = '" & username & "'"
set db = currentdb
set rs = db.openrecordset(SQL,dbopensnapshot)
if rs.recordcount > 0
  rs.movefirst
  me.UserNameField = rs!username
else
  msg "User Not Found"
End If

set rs = nothing
set db = nothing

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

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