help on DLookUp

  • Thread starter Thread starter Anthony
  • Start date Start date
A

Anthony

I wanted to know how to use the DLookUp, but in VB I need
to hard code a VB Lookup to another Table and it values.

How would I do that?

Thanks Anthony
 
Anthony said:
I wanted to know how to use the DLookUp, but in VB I need
to hard code a VB Lookup to another Table and it values.

How would I do that?

You're going to have to explain in more detail. At the moment I haven't
the faintest idea what you mean.
 
I should say, I want to look up some values in a table.
something like how you would use DLookUp from a text box.

But from VB, I want to hard code it with vb, but don't
know how.
 
Anthony said:
I should say, I want to look up some values in a table.
something like how you would use DLookUp from a text box.

But from VB, I want to hard code it with vb, but don't
know how.

When you say "VB", do you mean VBA running inside Access -- code in a
standard module or behind a form or report? In that case, you can use
DLookup just as easily as in a controlsource expression. Here's a
simple example:

Dim curPrice As Currency
' ...
curPrice = DLookup("Price", "tblPrices", "ProductID=1234")

On the other hand, if by "VB" you mean a Visual Basic application, not
an Access application, then the DLookup function is not available -- but
you can open a recordset on a table in an .mdb file and do your lookup
that way. For example,

Dim curPrice As Currency
Dim db As DAO.Database
Dim rs As DAO.Recordset

' ...
Set db = DBEngine.OpenDatabase("C:\My Path\MyDB.mdb")
Set rs = db.OpenRecordset( _
"SELECT Price FROM tblPrices WHERE ProductID=1234")

If rs.EOF Then
curPrice = 0
Else
curPrice = rs!Price
End If

rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

As you see, doing it in "true VB" is more complicated -- but this is
pretty much what the DLookup function does behind the scenes.
 
Back
Top