Solved

Dlookup

Posted on 2007-03-26
9
536 Views
Last Modified: 2013-11-27
Hi,

I have this Dlookup which i am trying to run in VBA, but i keep getting a runtime error.  Where am i going wrong?

ShredCC = InputBox("Enter the Shred Confirmation Code for the process you require an Invoice for.", "ShredIT - Print Invoice")

If DLookup("ShredConfirmationCode", "tblShreddingHistory", "ShrdConfirmationCode='" & ShredCC & "' AND '" & BagOrKg & "' Is Null") Then
MsgBox "This Shredding procedure does not have a billed by method associated with it!" & Chr(10) & Chr(10) & "Before you print the invoice you must set this up.?", vbOKOnly, "ShredIT - Assign DCN"
        Exit Function
    Else

Runtime Error 2001
You cancelled the previous operation

I am trying to show the message if the ShredConfirmationCode matches the Input box and th BagOrKg field is Null.
0
Comment
Question by:d10u4v
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 84
ID: 18791971
Try using Nz instead:

If DLookup("ShredConfirmationCode", "tblShreddingHistory", "ShrdConfirmationCode='" & ShredCC & "' AND  Nz('" & BagOrKg & ","")' ='')"

Or IsNull:

If DLookup("ShredConfirmationCode", "tblShreddingHistory", "ShrdConfirmationCode='" & ShredCC & "' AND IsNull(" & BagOrKg & ")")
0
 
LVL 27

Accepted Solution

by:
MikeToole earned 500 total points
ID: 18792015
Your code seems to be trying to include the value of a local variable, BagOrKg as the name of a column in the criteria of your DLookup. Did you mean:
If DLookup("ShredConfirmationCode", "tblShreddingHistory", "ShrdConfirmationCode='" & ShredCC & "' AND BagOrKg Is Null") Then
...
0
 

Author Comment

by:d10u4v
ID: 18792025
Hi,

I have used:
If DLookup("ShredConfirmationCode", "tblShreddingHistory", "ShredConfirmationCode='" & ShredCC & "' AND IsNull(" & BagOrKg & ")")

It works once whn running the code from the debug window, but when it runs on its own, it doesn't.  I get the following error:

RunTime Error 3075
Wrong Number of Aguments used with function in query expression
'ShredConfimrationCode='550012830' AND IsNull()'.

Any ideas?
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 27

Expert Comment

by:MikeToole
ID: 18792088
If BagOrKg is a VB variable then you don't need it as part of your DLookup Criteria:

If DLookup("ShredConfirmationCode", "tblShreddingHistory", "ShredConfirmationCode='" & ShredCC & "'") AND IsNull(BagOrKg)

If BagOrKg is a column name then it needs to be a literal in the DLoolUp criteria - as in my previous post.

The run time error is because you're setting the value of BagOrKg as the variable name in the IsNull() function - So when BagOrKg is null, there is no parameter for IsNull to test
0
 

Author Comment

by:d10u4v
ID: 18792092
I have just seen the comment with:
If DLookup("ShredConfirmationCode", "tblShreddingHistory", "ShredConfirmationCode='" & ShredCC & "' AND BagOrKg Is Null") Then

I get a datatype mismatch error.
0
 

Author Comment

by:d10u4v
ID: 18792108
No its not a column it is a field heading in the tblShreddingHistory table

BagOrKg is Text
ShrdConfirmationCode is Number
0
 
LVL 27

Expert Comment

by:MikeToole
ID: 18792136
You don't require quotes for number in creiteria:

If DLookup("ShredConfirmationCode", "tblShreddingHistory", "ShredConfirmationCode=" & ShredCC & " AND BagOrKg Is Null") Then
0
 
LVL 14

Expert Comment

by:bluelizard
ID: 18792329
another thing (tackle this once you get dlookup running):

you write

  "If Dlookup(......) Then ....".

Dlookup will return something, in your case a ShredConfirmationCode, which is a number.  if no matching record is found in the DB, Dlookup will return NULL.  to test for this, you should write

   "If IsNull( Dlookup(......) ) Then ...."

instead of simply "If Dlookup..."  (this would be ok if you looked up a boolean and you're sure that Dlookup will find a record.)


--bluelizard
0
 
LVL 27

Expert Comment

by:MikeToole
ID: 18792496
In fact, if your're looking for existance then you'd be better off with dcount() :

If DCount("*", "tblShreddingHistory", "ShredConfirmationCode=" & ShredCC & " AND BagOrKg Is Null") = 0 Then

- it gets simpler as we go along...
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question