DLookup is hard

  • Thread starter Thread starter Robert Solomon
  • Start date Start date
R

Robert Solomon

I am trying to get the DLookup function to work, and the criteria field
is frustrating

This is a simplification of what I'm trying to do w/ a command button:

Dim casesrst, casedetailsrst As Recordset
Dim str1, str2 As String

Set casesrst = CurrentDb.OpenRecordset("Cases")
casesrst.MoveLast
Set casedetailsrst = CurrentDb.OpenRecordset("CaseDetails")
str1 = "Abd Aorta Vascular Family 3rd order"
str2 = DLookup("CPTCode", "CPTList", "[CPT Description]= '" & str1 &
"'")


Thanks
 
Robert,

I don't think you quite have a handle on recordsets. There are two ways to
return the CPTCode from CPTList.

First - using DLookup:
str1 = "Abd Aorta Vascular Family 3rd order"
str2 = Nz(DLookup("CPTCode", "CPTList", "[CPT Description] = """ & str1
& """", "")

Second - Using a recordset:
Dim db As Database
Dim rs As DAO.Recordset
Dim str1 As String
Dim str2 As String

Set db = CurrentDb
str1 = SELECT CPTCode FROM CPTList " & _
"WHERE [CPT Description] = 'Abd Aorta Vascular Family 3rd
order'"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
If rs.AbsolutePosition > -1 Then
str2 = rs!CPTCode
Else
str2 = ""
End If

rs.Close
Set rs = Nothing
Set db = Nothing

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html
 
Robert,

I don't think you quite have a handle on recordsets. There are two ways to
return the CPTCode from CPTList.

First - using DLookup:
str1 = "Abd Aorta Vascular Family 3rd order"
str2 = Nz(DLookup("CPTCode", "CPTList", "[CPT Description] = """ & str1
& """", "")
I'm getting an error:
Wrong number of arguments or invalid property assignment
 
You're missing a ) near the end. Try this:

str2 = Nz(DLookup("CPTCode", "CPTList", "[CPT Description] = """ & str1
& """"), "")


--

Ken Snell
<MS ACCESS MVP>

Robert Solomon said:
Robert,

I don't think you quite have a handle on recordsets. There are two ways to
return the CPTCode from CPTList.

First - using DLookup:
str1 = "Abd Aorta Vascular Family 3rd order"
str2 = Nz(DLookup("CPTCode", "CPTList", "[CPT Description] = """ & str1
& """", "")
I'm getting an error:
Wrong number of arguments or invalid property assignment
 
Missed that one. Thanks Ken.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html


Ken Snell said:
You're missing a ) near the end. Try this:

str2 = Nz(DLookup("CPTCode", "CPTList", "[CPT Description] = """ & str1
& """"), "")


--

Ken Snell
<MS ACCESS MVP>

ways
to
return the CPTCode from CPTList.

First - using DLookup:
str1 = "Abd Aorta Vascular Family 3rd order"
str2 = Nz(DLookup("CPTCode", "CPTList", "[CPT Description] = """ & str1
& """", "")
I'm getting an error:
Wrong number of arguments or invalid property assignment
 
You're missing a ) near the end. Try this:
str2 = Nz(DLookup("CPTCode", "CPTList", "[CPT Description] = """ & str1
& """"), "")

I'm getting an "Item not found in this collection." msg; I know the
item is there because I pasted the text into str1 from the CPTList table
to be sure the item is there.

Anything else I should try?
 
You're missing a ) near the end. Try this:

str2 = Nz(DLookup("CPTCode", "CPTList", "[CPT Description] = """ & str1
& """"), "")

I'm getting an "Item not found in this collection." msg; I know the
item is there because I pasted the text into str1 from the CPTList table
to be sure the item is there.

Anything else I should try?
Nevermind. The Item not found message was a typo in the code. I fixed
that. But the DLookup doesn't work when str1 is the description of the
code. When I reverse the process and str1 is the code and I try to
lookup the description, that is working.

Not sure why I cannot have str1 be the description and lookup the code,
but I can live w/ the way it's working now.

Thanks for those who helped.

Rob
 
Robert,

It should work. Show me what you have now.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html


Robert Solomon said:
You're missing a ) near the end. Try this:

str2 = Nz(DLookup("CPTCode", "CPTList", "[CPT Description] = """ & str1
& """"), "")

I'm getting an "Item not found in this collection." msg; I know the
item is there because I pasted the text into str1 from the CPTList table
to be sure the item is there.

Anything else I should try?
Nevermind. The Item not found message was a typo in the code. I fixed
that. But the DLookup doesn't work when str1 is the description of the
code. When I reverse the process and str1 is the code and I try to
lookup the description, that is working.

Not sure why I cannot have str1 be the description and lookup the code,
but I can live w/ the way it's working now.

Thanks for those who helped.

Rob
 
Dim casedetailsrst As Recordset
Dim s1 As String
Dim s2 As String

Set casedetailsrst = Me!Child13.Form.RecordsetClone
s1 = 36246
s2 = Nz(DLookup("[CPT Description]", "[CPTList]", _
"[CPT Code] = '36246' "), "Not Found")
With casedetailsrst
.AddNew
!CasesRef = Me!CaseNo
!CPTDescription = s2
!CPTCode = s1
.Update
.Move 0, .LastModified
End With
casedetailsrst.Close
Set casedetailsrst = Nothing

And this code does not work if "[CPT Code] = ' s1 ' "; I have to repeat
the numberical code for this lookup to succeed.
 
Robert,

Well of course it won't work when [CPT Code] = ' s1 '. You have to put s1
OUTSIDE the literal string.

Also, you've declared s1 and s2 as strings, yet you assigned s1=36246. If
it's really a string, you should assign its value like so:
s1 = "36246"

The following will work:

s1 = "36246"
s2 = Nz(DLookup("[CPT Description]", "[CPTList]", _
"[CPT Code] = '" & s1 & "'"), "Not Found")

If it's not meant to be a string, then declare it as a numeric data type,
maybe Long Integer. In that case, the following will work:

Dim s1 As Long
Dim s2 As String
s1 = 36246
s2 = Nz(DLookup("[CPT Description]", "[CPTList]", _
"[CPT Code] = " & s1), "Not Found")

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html


Robert Solomon said:
Dim casedetailsrst As Recordset
Dim s1 As String
Dim s2 As String

Set casedetailsrst = Me!Child13.Form.RecordsetClone
s1 = 36246
s2 = Nz(DLookup("[CPT Description]", "[CPTList]", _
"[CPT Code] = '36246' "), "Not Found")
With casedetailsrst
.AddNew
!CasesRef = Me!CaseNo
!CPTDescription = s2
!CPTCode = s1
.Update
.Move 0, .LastModified
End With
casedetailsrst.Close
Set casedetailsrst = Nothing

And this code does not work if "[CPT Code] = ' s1 ' "; I have to repeat
the numberical code for this lookup to succeed.
It should work. Show me what you have now.

Regards,
Graham R Seach
Microsoft Access MVP
 
s1 is meant to be a string. When I pasted your code in, it works!
Finally! But I have no clue as to why the syntax is structured as it
is.

Maybe DLookup is easy, but its criteria syntax remains quite hard.

Thanks again!
Well of course it won't work when [CPT Code] = ' s1 '. You have to put s1
OUTSIDE the literal string.

Also, you've declared s1 and s2 as strings, yet you assigned s1=36246. If
it's really a string, you should assign its value like so:
s1 = "36246"

The following will work:

s1 = "36246"
s2 = Nz(DLookup("[CPT Description]", "[CPTList]", _
"[CPT Code] = '" & s1 & "'"), "Not Found")

If it's not meant to be a string, then declare it as a numeric data type,
maybe Long Integer. In that case, the following will work:

Dim s1 As Long
Dim s2 As String
s1 = 36246
s2 = Nz(DLookup("[CPT Description]", "[CPTList]", _
"[CPT Code] = " & s1), "Not Found")

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html


Robert Solomon said:
Dim casedetailsrst As Recordset
Dim s1 As String
Dim s2 As String

Set casedetailsrst = Me!Child13.Form.RecordsetClone
s1 = 36246
s2 = Nz(DLookup("[CPT Description]", "[CPTList]", _
"[CPT Code] = '36246' "), "Not Found")
With casedetailsrst
.AddNew
!CasesRef = Me!CaseNo
!CPTDescription = s2
!CPTCode = s1
.Update
.Move 0, .LastModified
 
Robert,

It's really not that hard.

If we have the following DLookup:
DLookup("myField", "myTable", "someField = 3")

....what this equates to is:
SELECT myField FROM myTable WHERE someField = 3

So you see, DLookup simply presents this query in a different way.

Each of the arguments is enclosed in quotes, because VBA must pass these
arguments to the query engine, which will build a valid SQL query out of
them. The third argument of the DLookup function must be a valid SQL WHERE
clause, without the keyword "WHERE".

If the variable comparison operator is a literal value, you can include it
inside the quotes, as shown below:
"someField = 3"
"someField = 'ABC'"
But if it is contained within a variable, you don't want the variable name
included in the WHERE clause; you want its value; so the variable must be
excluded from the string, like so:
"someField = " & intMyNumber
"someField = '" & strMyString & "'"

Of course, as you can infer from the above example, string values must be
passed to the query engine as literal string values, which is why we include
either single quotes ( ' ) or double quotes ( " ).

You can test this in the Immediate window:
?"someField = 3"
someField = 3

?"someField = " & 3
someField = 3

intMyNumber = 3
?"someField = intMyNumber"
someField = intMyNumber

?"someField = " & intMyNumber
someField = 3

strMyString = "ABC"
?"someField = strMyString"
someField = strMyString

?"someField = " & strSomeField
someField = ABC

?"someField = '" & strSomeField & "'"
someField = 'ABC'

?"someField = """ & strSomeField & """"
someField = "ABC"

?"someField = """ & "ABC" & """"
someField = "ABC"

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html


Robert Solomon said:
s1 is meant to be a string. When I pasted your code in, it works!
Finally! But I have no clue as to why the syntax is structured as it
is.

Maybe DLookup is easy, but its criteria syntax remains quite hard.

Thanks again!
Well of course it won't work when [CPT Code] = ' s1 '. You have to put s1
OUTSIDE the literal string.

Also, you've declared s1 and s2 as strings, yet you assigned s1=36246. If
it's really a string, you should assign its value like so:
s1 = "36246"

The following will work:

s1 = "36246"
s2 = Nz(DLookup("[CPT Description]", "[CPTList]", _
"[CPT Code] = '" & s1 & "'"), "Not Found")

If it's not meant to be a string, then declare it as a numeric data type,
maybe Long Integer. In that case, the following will work:

Dim s1 As Long
Dim s2 As String
s1 = 36246
s2 = Nz(DLookup("[CPT Description]", "[CPTList]", _
"[CPT Code] = " & s1), "Not Found")

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html


Robert Solomon said:
Dim casedetailsrst As Recordset
Dim s1 As String
Dim s2 As String

Set casedetailsrst = Me!Child13.Form.RecordsetClone
s1 = 36246
s2 = Nz(DLookup("[CPT Description]", "[CPTList]", _
"[CPT Code] = '36246' "), "Not Found")
With casedetailsrst
.AddNew
!CasesRef = Me!CaseNo
!CPTDescription = s2
!CPTCode = s1
.Update
.Move 0, .LastModified
 
Robert,

It's really not that hard.

If we have the following DLookup:
DLookup("myField", "myTable", "someField = 3")

...what this equates to is:
SELECT myField FROM myTable WHERE someField = 3

So you see, DLookup simply presents this query in a different way.

Each of the arguments is enclosed in quotes, because VBA must pass these
arguments to the query engine, which will build a valid SQL query out of
them. The third argument of the DLookup function must be a valid SQL WHERE
clause, without the keyword "WHERE".

If the variable comparison operator is a literal value, you can include it
inside the quotes, as shown below:
"someField = 3"
"someField = 'ABC'"
But if it is contained within a variable, you don't want the variable name
included in the WHERE clause; you want its value; so the variable must be
excluded from the string, like so:
"someField = " & intMyNumber
"someField = '" & strMyString & "'"
I appreciate your long explanation. It's the first time I actually
understand the complex syntax.

Rob
 
Back
Top