简体   繁体   English

MS Access DLookUp使用变量

[英]MS Access DLookUp using variables

I am building a form in MS Access and I want some information to be pulled from one of several tables and populated into a text box, based on two drop downs that the user interacts with on the same form. 我正在MS Access中构建一个表单,我希望根据用户在同一表单上进行交互的两个下拉列表,从几个表之一中提取一些信息并填充到文本框中。 After researching this, I found that DLOOKUP does what I am looking to do - the only problem is that I am having issues with getting it to work properly and I keep getting "#NAME" appearing in the text box. 经过研究之后,我发现DLOOKUP可以完成我想要做的事情-唯一的问题是我在使其正常工作方面遇到问题,并且在文本框中始终出现“ #NAME”。

I did some tutorials with DLOOKUP and had successful results but applying it to my projects has not been successful. 我使用DLOOKUP做过一些教程,并获得了成功的结果,但是将其应用于我的项目却没有成功。 In my project the FieldName of the Dlookup is static, which is "Description" but both the table name and Criteria are dynamic being passed to it as variables. 在我的项目中,Dlookup的FieldName是静态的,它是“ Description”,但是表名和Criteria都是动态的,并作为变量传递给它。

Currently I have four tables: tblAC, tblAT, tblAU, and tblCA - all following the design: 目前,我有四个表:tblAC,tblAT,tblAU和tblCA-都遵循以下设计:

Table (Control_Number, Description) 表格(Control_Number,说明)

Each table has four records, with the 'Control_Number" as the primary key. The control number is a string - because it can have letters in it. 每个表有四个记录,其中“ Control_Number”为主键,控制号是一个字符串-因为它可以包含字母。

What I have so far is: 到目前为止,我有:

  Dim controlfamily1 As String
  Dim control1 As String

  controlfamily1 = "tbl" & CStr(Me.cboControlFamily.Value)
  control1 = CStr(Me.cboControls.Value)

     Me.txtDescription.ControlSource = DLookup("[Description]", controlfamily1, "[Control_Number] ='" & control1 & "'")

Where controlfamily1 is a variable of the type string - representing the TableName in the DlookUp. 其中controlfamily1是字符串类型的变量-表示DlookUp中的TableName。 Control1 is a also a string variable representing the specific criteria to search on. Control1还是一个字符串变量,表示要搜索的特定条件。 I believe my issue all boils down to the my use of quotes, brackets and double quotes. 我认为我的问题归结为我使用引号,方括号和双引号。

Thanks! 谢谢!

If you put expression in ControlSource, you must use " 如果将表达式放在ControlSource中,则必须使用“

Me.txtDescription.ControlSource = "=DLookup(""[Description]"", ""[tbl" & controlfamily1 & "]"", ""[Control_Number] ='" & control1 & "'"")"

but try to set property in design mode to 但尝试将设计模式下的属性设置为

= DLookup("[Description]", "tbl" & CStr(Me!cboControlFamily), "[Control_Number] ='" & Me![cboControls] & "'")

and do Me.txtDescription.Requery when needed 并在需要时执行Me.txtDescription.Requery

I realise that this is a pretty old post, but it helped me so: 我意识到这是一篇很老的文章,但是它对我有帮助:

I had success with the following where fieldName is a string containing the name of the tables field: Note the position of the square brackets inside the parenthesis 我在以下方面取得了成功,其中fieldName是包含表字段名称的字符串: 注意括号内方括号的位置

DLookup("[" & fieldName & "]", "my_table", "id = " & my_id)

It helped me deal with a bad naming convention where there was field1, field2...8 that I had to collect data from. 它帮助我处理了一个不好的命名约定,即我不得不从中收集数据的field1, field2...8

when you pass the tablename as a string in some variable, you do not need to use "". 当您将表名作为字符串传递给某个变量时,不需要使用“”。

So dlookup("[FieldA]","tableName","criterion") 所以dlookup(“ [FieldA]”,“ tableName”,“ criterion”)

or 要么

Dim tableNamezzz as String
tableNamezzz = Cstr(me.textboxCarryingTableName.value)
dlookup("[FieldA]", tableNamezzz, "Criterion")

If you want to pass the column name as a variable and into the dlookup function, use below: 如果要将列名作为变量传递到dlookup函数中,请使用以下命令:

Dim columnNamez as String
Dim tableNamez as String
    tableNamez = Cstr(me.textboxCarryingTableName.value)
    ColumnNamez = Cstr(me.textboxCarryingColumnName.value) 
DLookup("ID", TableNamez, ColumnNamez & "='" & Me.txtFieldValue & "'")

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

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