Help! DLookup w/Variable

  • Thread starter Thread starter swngdncr
  • Start date Start date
S

swngdncr

OK, this is driving me crazy. I'm going to pull all my hair out in a
moment. I've searched all the threads etc., and still cannot format
this code to work correctly. I've tried every version of single and
double quotation marks and ampersands that I've found in examples that
appear to be identical to what I'm trying to do. I've also tried to
dim the variables as String and Long instead of Variant. Nada works...
I would be ever so greatful if someone could assist me with this. The
relevant portions of the code are:

Dim varExistWO As Variant
Dim varExistWOLocation As Variant

varExistWOLocation = DLookup("[WorkOrderID]",
"tblWorkOrdersLocations", _
"[WorkOrderID]= '" & varNewWO & "'" & _
" And [LocationID] = '" & varNewLocation & "'")
 
Sorry, I left out :

Dim varNewWO As Variant
Dim varNewLocation As Variant

varNewWO = Forms!frmWorkOrders!WorkOrderID
varNewLocation =
Forms!frmWorkOrders!frmWorkOrdersLocationsSubform!cmbSite
 
Sorry, I left out :

Dim varNewWO As Variant
Dim varNewLocation As Variant

varNewWO = Forms!frmWorkOrders!WorkOrderID
varNewLocation =
Forms!frmWorkOrders!frmWorkOrdersLocationsSubform!cmbSite
 
OK, this is driving me crazy. I'm going to pull all my hair out in a
moment. I've searched all the threads etc., and still cannot format
this code to work correctly. I've tried every version of single and
double quotation marks and ampersands that I've found in examples that
appear to be identical to what I'm trying to do. I've also tried to
dim the variables as String and Long instead of Variant. Nada works...
I would be ever so greatful if someone could assist me with this. The
relevant portions of the code are:

Dim varExistWO As Variant
Dim varExistWOLocation As Variant

varExistWOLocation = DLookup("[WorkOrderID]",
"tblWorkOrdersLocations", _
"[WorkOrderID]= '" & varNewWO & "'" & _
" And [LocationID] = '" & varNewLocation & "'")

If WorkOrderID and LocationID are Number fields (Long Integer or
Autonumber ID fields) in tblWorkOrderLocations, you should not use ANY
delimiter:

varExistWOLocation = DLookup("[WorkOrderID]",
"tblWorkOrdersLocations", _
"[WorkOrderID]= " & varNewWO & _
" And [LocationID] = " & varNewLocation)

Text fields need either ' or " as delimiter; Date/Time fields need #;
Number or Currency fields get no delimiter at all.

Note that if varNewW0 or varNewLocation might be NULL you will indeed
get an error. You may want to use NZ() to convert NULL values to 0 (or
to some other value which will not match any record in your table).

John W. Vinson[MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top