简体   繁体   English

使用 Microsoft Access 2010 中的 VBA 代码将表字段中的值获取到变量中

[英]Get a value from a table field into a variable using VBA code in Microsoft Access 2010

In MS Access 2010, I use a table having three fields:在 MS Access 2010 中,我使用一个包含三个字段的表:

  1. ID(Auto Number) ID(自动编号)
  2. BatchEndDate(Text)批次结束日期(文本)
  3. IsImported(Yes/No). IsImported(是/否)。

表的图片。

I am using the following VBA Code to read a value from field 3 with reference to field 2. This code works well but when I change the data type of field 2 from Text to Date/Time, it doesn't work and an error message is displayed showing我正在使用以下 VBA 代码从字段 3 中读取字段 2 中的值。此代码运行良好,但是当我将字段 2 的数据类型从文本更改为日期/时间时,它不起作用并显示错误消息显示显示

Data type mismatch in criteria expression条件表达式中的数据类型不匹配

Please suggest me the changes in code to resolve the issue.请建议我更改代码以解决问题。

Dim selectedBatchDate As Variant
selectedBatchDate = Me.cmboBatchDate

Dim importCheck As Variant
importCheck = DLookup("[Is Imported]", "BatchEndDate", "[Batch End Date] = '" & selectedBatchDate & "'")
    MsgBox ("Import Status is " & importCheck), vbInformation, "Import Status"

If you change the datatype of the field you also have to change the datatype of the parameter you use in your DLookup:如果您更改字段的数据类型,您还必须更改您在 DLookup 中使用的参数的数据类型:

Try the code below, the # specifies that the parameter is a date value.试试下面的代码, #指定参数是一个日期值。

importCheck = DLookup("[Is Imported]", "BatchEndDate", "[Batch End Date] = #" & selectedBatchDate & "#")

You system defaults to the dd/mm/yyyy format for dates, and as you need string expressions for these, you have to use Format to apply a format of yyyy/mm/dd :您的系统默认为dd/mm/yyyy日期格式,并且由于您需要这些字符串表达式,您必须使用Format来应用yyyy/mm/dd格式:

Dim selectedBatchDate As String
selectedBatchDate = Format(Me.cmboBatchDate, "yyyy\/mm\/dd")

Dim importCheck As Variant
importCheck = DLookup("[Is Imported]", "BatchEndDate", "[Batch End Date] = #" & selectedBatchDate & "#")

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

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