MS Access中查询的字段默认值 [英] Field default value from query in MS Access

查看:193
本文介绍了MS Access中查询的字段默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MS Access中的表上有一个字段,tblMyTable.SomeID,我想在tblUserPref.DefaultSomeID中将默认值设置为用户首选项。在tblMyTable的表定义中,我似乎没有设置默认值来使用查询。我有一个表单,其中记录输入到tblMyTable。我试图在表单上设置字段的默认值,但似乎也不接受查询。所以,作为最后的手段,我试图用VBA来做。我可以在VBA中查询我想要的值,但是我无法确定哪个事件附加到代码中。

I have a field on a table in MS Access, tblMyTable.SomeID, and I want to set the default value as a user preference in tblUserPref.DefaultSomeID. It doesn't appear that I can set the default value to use a query in the table definition of tblMyTable. I have a form where records are entered into tblMyTable. I've tried to set the default value of the field on the form, but doesn't seem to accept a query either. So, as a last resort, I'm trying to do it with VBA. I can query the value that I want in VBA, but I can't figure out which event to attach the code to.

在用户开始输入之前,窗体中打开空白记录。当打开或编辑现有记录时,我不想运行代码。但是,如果代码为新的空白记录和现有记录运行,我可以编写代码。到目前为止,我在场上尝试过的所有事件和表单本身都没有运行。任何人都可以建议我应该使用哪个事件,以及哪个对象?

I want to run the code whenever a new blank record is opened in the form, before the user starts to type into it. I do not want to run the code when an existing record is opened or edited. However, if the code runs for both new blank records and for existing records, I can probably code around that. So far, all of the events I have tried on the field and on the form itself have not run when I wanted them to. Can anyone suggest which event I should use, and on which object?

推荐答案

我不确定我已经明白了,但是我想你根据一些运行时信息(如用户名)要求在从另一个表中绘制的字段中插入一个值。在这种情况下,您可以使用域查找功能(DLookup()),并将传递要返回的字段的名称,您要查找的表或查询的名称,以及将结果限制为一行(我认为这取决于您可以在运行时收集的值)。然后可以将DLookup()公式永久设置为表单控件上的默认值,并且在创建真实记录之前不会导致表单弄脏。

I'm not certain I've understood the problem, but I think you're asking to insert a value in the field that is drawn from a different table, based on some runtime information (such as the user name). In that case, you could use the domain lookup function, DLookup(), and you'd pass it the name of the field you want returned, the name of the table or query you're looking it up from, and the criteria for limiting the result to one row (which would, I assume, depend on values you can gather at runtime). That DLookup() formula could then be set permanently as the default value on the form control, and would not cause the form to be dirtied before you've created a real record.

当然,我可能完全误解了你想要做的事情,所以这可能不起作用,但是你似乎想在记录集中查找一些东西,并将结果用作新记录的值,而DLookup )将允许您完全没有任何编码(以及有利于不过早地玷污记录)。

Of course, I may have completely misinterpreted what you're trying to do, so this may not work, but you seemed to want to look something up in a recordset and use the result as your value for new records, and DLookup() would allow you to do that without any coding at all (as well as having the benefit of not dirtying the record prematurely).

这篇关于MS Access中查询的字段默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆