473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Syntax problem using DLookup

101 100+
I am getting a runtime error 2471 on the expression LoginID using Dlookup and dont know how to correct it.
My code is:-
Expand|Select|Wrap|Line Numbers
  1. Dim LoginID As Integer
  2. Dim Priv As Variant
  3. LoginID = [basMyEmpID].lngMyEmpID
  4. On Error GoTo Ad_Err
  5. MsgBox (LoginID)
  6.                      Priv = DLookup("[strAccess]", _
  7.                      "[tblEmployees]", _
  8.                      "[lngEmpID] = LoginID")
  9.                       MsgBox (Priv)
  10.  
  11.  
  12.                      Exit Sub
  13.  
  14. Ad_Err:
  15.             MsgBox "Error " & Err.Number & vbCrLf & Err.Description
  16.             Exit Sub 
LoginId is an Integer or Long and is evaluated correctly using Debug. But the Priv variant is returning Empty?

Could someone please advise
Nov 16 '11 #1

✓ answered by TheSmileyCoder

There you go
Expand|Select|Wrap|Line Numbers
  1. Priv = DLookup("[strAccess]", _ 
  2.                      "[tblEmployees]", _ 
  3.                      "[lngEmpID] =" & LoginID) 
What you need to understand is that the information you supply in the Dlookup gets sent to the JET database engine, and JET has no idea what LoginID is. Therefore you need to parse the string together first (The correction I made in your code) forcing VBA to piece together the string, before sending it.

7 3395
TheSmileyCoder
2,322 Expert Mod 2GB
There you go
Expand|Select|Wrap|Line Numbers
  1. Priv = DLookup("[strAccess]", _ 
  2.                      "[tblEmployees]", _ 
  3.                      "[lngEmpID] =" & LoginID) 
What you need to understand is that the information you supply in the Dlookup gets sent to the JET database engine, and JET has no idea what LoginID is. Therefore you need to parse the string together first (The correction I made in your code) forcing VBA to piece together the string, before sending it.
Nov 16 '11 #2
Cyd44
101 100+
Thank You SmileyCoder, that works just fine. I had tried almost every combination in the syntax but could not get it to work.

It does now......Thanks
Nov 16 '11 #3
NeoPa
32,556 Expert Mod 16PB
@Cyd.
More important than the solution is why it's the solution. Smiley tried to explain this after providing the solution and it's important you understand it otherwise you'll forever be posting questions at the same level (More questions we like, but not rehashes of the same ones over again). If you still don't understand then read again what he posted. Fundamentally it comes down to understanding what is done by the VBA code (which creates a SQL syntax string), and what is done by the SQL engine when it is passed that SQL string.
Nov 17 '11 #4
Cyd44
101 100+
Hi NeoPa

Thank you, I was aware that my problem was a Syntax one and I can see that I had missed out the =" & LoginID. I was not aware of the JetEngine parse requirement but am assuming the quotes an ampersand do this.

Thank you for your advice
Nov 17 '11 #5
sierra7
446 Expert 256MB
Hi
Whenever I use DLookup I specify the destination as a Variant just incase nothing is found!
Then test it is not null before proceeding, of course.
S7
Nov 17 '11 #6
NeoPa
32,556 Expert Mod 16PB
That's not quite it Cyd, and I'll explain as even minor confusions in this area (such an important area within database work) can cause you so many problems.

The point is that everything between the quotes (") in VBA is actually a string. The ampersand (&) sticks two strings together, and when used with numbers this causes the number to be converted to a string automatically so that string can be added to the other to create the bigger (longer generally) string.

Consider the difference between :
Expand|Select|Wrap|Line Numbers
  1. "[lngEmpID] = LoginID"
and
Expand|Select|Wrap|Line Numbers
  1. "[lngEmpID] = " & LoginID
The former is a single string which results in [lngEmpID] = LoginID. The latter, if we assume for the moment an example value of the Me.LoginID control of your form to be 32 for instance, results in [lngEmpID] = & 32 or [lngEmpID] = 32.

When we look at these two scenarios at the point where these strings are passed over to SQL (via the DLookup() call) we see they both start with [lngEmpID] = . This is fine as the fieldname [lngEmpID] is recognised by SQL. All good so far.

Now we get to the difference between the two versions of the SQL command string. The first compares it to a value LoginID. SQL has no way of understanding this reference as it is actually a control on a form, but not fully specified. SQL wets its pants and stops working. In the second version though, it sees a simple numeric value - 32 - and is happy to continue processing.

The second version works because we have VBA referencing the value of Me.LoginID before adding the result to the other part of the string.
Nov 17 '11 #7
Cyd44
101 100+
Thats very well explained.Many thanks
Nov 19 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Christine Henderson | last post by:
I have a problem using the above function in the following simplified circumstance: In the lookup table called "Klms Travelled" I have 3 fields, eg: Receiver Name Receiver Suburb ...
2
by: Eric | last post by:
Can any one please tell me the syntax is correct Me.List17=DLookUp(,,="" And =") Thanks,
2
by: jonvan20 | last post by:
I have been having trouble with a simple Dlookup command that was Reccommended to me by a nice fellow named Vic, On the other hand I have statements like this that wont run they give me a run time...
1
by: afernandeziii | last post by:
Hi everyone, Please help! what's the correct syntax using DateFormat in SQL Server 2005. Example. 05/04/07 00:00:00 i need this date show like this 05/04/07 i this regard the time. Anyone...
2
by: John | last post by:
To prevent the null-error from showing up when dlookup returns false I created the code beneath which doesn't seem very elegant. How can I code this without having to use the dlookup twice? If...
4
by: Mark1978 | last post by:
Hi All Apologies if someone has asked this question before, but I have hunted high and low for the solution. I am relatively new to using access so am not sure if this is possible. I am using...
2
by: hr833 | last post by:
i'm having trouble with Dlookup.I'm trying to insert a Dlookup statement in a update query, so that the user need not upate each record manually. Supplier Catergory sSupplierName sCatergory ...
3
reginaldmerritt
by: reginaldmerritt | last post by:
I'm using Dlookup to search a table for the first record it comes across that has a date >= the date i specifiy. e.g. formateddate = format(Me.SelectedDate,"mmddyy") VarX = DLookup("",...
3
by: Constantine AI | last post by:
Hi can anybody help me with this problem? I have a customer order form with a sales order line subform. The subform contains products ordered by customers, each (wooden) product can come in two...
1
by: toadmaster | last post by:
I am trying to use the DLookup function to check a table to see if a user exist in there; if not refuse them access. The following is the code I am using but it seems to just pop up the "Error...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.