2

In MS Access 2010, I use a table having three fields:

  1. ID(Auto Number)
  2. BatchEndDate(Text)
  3. IsImported(Yes/No).

Picture of Table.

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

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"

2 Answers 2

1

If you change the datatype of the field you also have to change the datatype of the parameter you use in your DLookup:

Try the code below, the #specifies that the parameter is a date value.

importCheck = DLookup("[Is Imported]", "BatchEndDate", "[Batch End Date] = #" & selectedBatchDate & "#")
Sign up to request clarification or add additional context in comments.

Comments

0

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:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.