PC Review


Reply
Thread Tools Rate Thread

DLookup vs. query result

 
 
EJO
Guest
Posts: n/a
 
      26th Jun 2006
Hello and thanks!

The following code is causing the error 'invalid use of null', but if I
use the same parmeters in a regular query, the correct ID is pulled,
how can I correct for the difference?


Dim intCktID As Integer, intSiteID As Integer, strCktID As String

intSiteID = Forms!Site!Text2
strCktID = Me.CktID

intCktID = DLookup("[ID]", "CktsList", "[Site_ID]= " & intSite & " AND
[Display]=' " & strCktID & " ' ")

DoCmd.OpenForm "CktModify", , , "[ID] = " & intCktID

 
Reply With Quote
 
 
 
 
Allen Browne
Guest
Posts: n/a
 
      26th Jun 2006
It appears you have extra spaces inside the quotes for the 2nd phrase of the
3rd argument. This could mean that no ID is found, so the result of
DLookup() is null, and the attempt to assign Null to the integer variable
results in Error 94 (invalid use of Null).

Try:
Dim varChkID As Variant

varCktID = DLookup("[ID]", "CktsList", _
"([Site_ID]= " & intSite & ") AND ([Display]= """ & strCktID & """)")
If IsNull(varCktID) Then
MsgBox "Not found"
Else
DoCmd.OpenForm "CktModify", , , "[ID] = " & varCktID
End If

If those quotes don't make sense, see:
Quotation marks within quotes
at:
http://allenbrowne.com/casu-17.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"EJO" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello and thanks!
>
> The following code is causing the error 'invalid use of null', but if I
> use the same parmeters in a regular query, the correct ID is pulled,
> how can I correct for the difference?
>
>
> Dim intCktID As Integer, intSiteID As Integer, strCktID As String
>
> intSiteID = Forms!Site!Text2
> strCktID = Me.CktID
>
> intCktID = DLookup("[ID]", "CktsList", "[Site_ID]= " & intSite & " AND
> [Display]=' " & strCktID & " ' ")
>
> DoCmd.OpenForm "CktModify", , , "[ID] = " & intCktID



 
Reply With Quote
 
Rick Brandt
Guest
Posts: n/a
 
      26th Jun 2006
EJO wrote:
> Hello and thanks!
>
> The following code is causing the error 'invalid use of null', but if
> I use the same parmeters in a regular query, the correct ID is pulled,
> how can I correct for the difference?
>
>
> Dim intCktID As Integer, intSiteID As Integer, strCktID As String
>
> intSiteID = Forms!Site!Text2
> strCktID = Me.CktID
>
> intCktID = DLookup("[ID]", "CktsList", "[Site_ID]= " & intSite & " AND
> [Display]=' " & strCktID & " ' ")
>
> DoCmd.OpenForm "CktModify", , , "[ID] = " & intCktID


I would guess that the space after your first single quote and before your
last single quote should not be there. That will literally look for a space
followed by your ID followed by another space.

People sometimes write code examples like that so that the single quote can
be easily seen, but you don't want those spaces in your actual code.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com


 
Reply With Quote
 
EJO
Guest
Posts: n/a
 
      26th Jun 2006

> I would guess that the space after your first single quote and before your
> last single quote should not be there. That will literally look for a space
> followed by your ID followed by another space.
>
> People sometimes write code examples like that so that the single quote can
> be easily seen, but you don't want those spaces in your actual code.



I intentionally put the spaces there for the intent of the post for the
reason you state; i've seen in other posts where that is an issue with
string criteria.

 
Reply With Quote
 
Rick Brandt
Guest
Posts: n/a
 
      26th Jun 2006
EJO wrote:
>> I would guess that the space after your first single quote and
>> before your last single quote should not be there. That will
>> literally look for a space followed by your ID followed by another
>> space.
>>
>> People sometimes write code examples like that so that the single
>> quote can be easily seen, but you don't want those spaces in your
>> actual code.

>
>
> I intentionally put the spaces there for the intent of the post for
> the reason you state; i've seen in other posts where that is an issue
> with string criteria.


All I can suggest then is to put that into the immediate window and break it
into poieces ot see what you get. Try it once each with only one of the
criteria to see what you get.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com


 
Reply With Quote
 
Pat Hartman\(MVP\)
Guest
Posts: n/a
 
      26th Jun 2006
Nice article Allen. My solution is a little more of a hammer. I add a
constant to each database.

Const QUOTE = """"
Four quotes = the two outside quotes plus the double inside to store a
single = "--""--"
I think this makes the statement a little easier to read since you never
have to see multiple quotes. You just concatenate the QUOTE constant
whenever you want to put a quote inside a string.

varCktID = DLookup("[ID]", "CktsList", _
"[Site_ID]= " & intSite & " AND [Display]= " & QUOTE & strCktID & QUOTE)

"Allen Browne" <(E-Mail Removed)> wrote in message
news:%23H%(E-Mail Removed)...
> It appears you have extra spaces inside the quotes for the 2nd phrase of
> the 3rd argument. This could mean that no ID is found, so the result of
> DLookup() is null, and the attempt to assign Null to the integer variable
> results in Error 94 (invalid use of Null).
>
> Try:
> Dim varChkID As Variant
>
> varCktID = DLookup("[ID]", "CktsList", _
> "([Site_ID]= " & intSite & ") AND ([Display]= """ & strCktID & """)")
> If IsNull(varCktID) Then
> MsgBox "Not found"
> Else
> DoCmd.OpenForm "CktModify", , , "[ID] = " & varCktID
> End If
>
> If those quotes don't make sense, see:
> Quotation marks within quotes
> at:
> http://allenbrowne.com/casu-17.html
>
> --
> Allen Browne - Microsoft MVP. Perth, Western Australia.
> Tips for Access users - http://allenbrowne.com/tips.html
> Reply to group, rather than allenbrowne at mvps dot org.
>
> "EJO" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hello and thanks!
>>
>> The following code is causing the error 'invalid use of null', but if I
>> use the same parmeters in a regular query, the correct ID is pulled,
>> how can I correct for the difference?
>>
>>
>> Dim intCktID As Integer, intSiteID As Integer, strCktID As String
>>
>> intSiteID = Forms!Site!Text2
>> strCktID = Me.CktID
>>
>> intCktID = DLookup("[ID]", "CktsList", "[Site_ID]= " & intSite & " AND
>> [Display]=' " & strCktID & " ' ")
>>
>> DoCmd.OpenForm "CktModify", , , "[ID] = " & intCktID

>
>



 
Reply With Quote
 
EJO
Guest
Posts: n/a
 
      26th Jun 2006

Allen Browne wrote:
> It appears you have extra spaces inside the quotes for the 2nd phrase of the
> 3rd argument. This could mean that no ID is found, so the result of
> DLookup() is null, and the attempt to assign Null to the integer variable
> results in Error 94 (invalid use of Null).
>



Nope. You guys were right...

In double checking to make sure I had the single quotes in there, i did
in fact unintentionally leave spaces in there. Thanks, I hope not to
waste your time on my inability to be more thorough any longer!

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
#Error as DLOOKUP result GD Microsoft Access Getting Started 2 14th Jan 2009 09:16 PM
How to make query show result if result is Null Kind regards Donatas Microsoft Access Queries 10 1st Nov 2008 11:34 AM
Dlookup condition result =?Utf-8?B?UmF1bCBTb3VzYQ==?= Microsoft Access Form Coding 4 9th Sep 2006 03:38 PM
executing Dlookup result Aika Microsoft Access 1 16th Oct 2003 01:53 PM
Assign Dlookup result to text box issues RTE 2471, HELP FrankBooth Microsoft Access Forms 0 1st Aug 2003 04:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:44 PM.