473,395 Members | 1,681 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,395 software developers and data experts.

Syntax Error in String in Query Expression - Run time error 3075

I've seen many similar threads, but despite repeated efforts I cannot figure out my problem. I am running Access 2003, VB 6.5, Office XP Pro. Code excerpt is below (you can see where I've tried debugging myself). My problem is in the DLookup command.

UserName = Me.cboUserName.Value
Debug.Print "User Name is "; UserName
strPassword = DLookup("Password", "Employees", "EmpName ='" & UserName)
Debug.Print "Password is "; Password
If Me.txtPassword.Value = strPassword Then
DoCmd.Close acForm, "DatabaseLogin", acSaveNo
DoCmd.OpenForm "MenuMain", acViewNormal
Exit Sub

"cboUserName" is a combo box on form "DatabaseLogin". That is working fine. On same form users enter a password into text box "txtPassword". I am attempting to compare value of "txtPassword" to a cell "Password" in table "Employees" where "EmpName" is equal to value of "cboUserName". For explanation purposes only, let's say value of "cboUserName" is "Davis". With syntax as shown above, I receive error message {Syntax error in string in query expression 'EmpName ='Davis'.} (items inside braces are verbatim). "EmpName" is a text field. "Password" is also a text field.

I have tried multilple variations of syntax, getting other error messages in the process (compile error, you canceled the previous operation ,etc.). What am I doing wrong? Thanks in advance for your help.
Nov 13 '09 #1

✓ answered by ajalwaysus

Try this:
Expand|Select|Wrap|Line Numbers
  1. strPassword = DLookup("Password", "Employees", "EmpName ='" & UserName & "'")
  2. Exit Sub
You need to wrap the UserName in single quotes on both ends of the value.

Hope this helps,
-AJ

5 8804
ajalwaysus
266 Expert 100+
Try this:
Expand|Select|Wrap|Line Numbers
  1. strPassword = DLookup("Password", "Employees", "EmpName ='" & UserName & "'")
  2. Exit Sub
You need to wrap the UserName in single quotes on both ends of the value.

Hope this helps,
-AJ
Nov 13 '09 #2
Thank you! It worked perfectly.
Nov 13 '09 #3
NeoPa
32,556 Expert Mod 16PB
One of the most popular (frequently occurring rather than best liked) problems we get is with SQL strings being manipulated in VBA code.

The reason this is so difficult is that all the work is being done at a level of redirection. What I mean by this is that the coder is never working directly with the SQL itself, but rather with code which in turn, is relied on to produce the SQL that they are envisaging is required. It's rather similar to the problems coders have historically had dealing with pointers.

Anyway, a technique I often suggest to coders struggling with this (at any level. This happens to experienced coders too.) is to use either the MsgBox() function, or Debug.Print into the Immediate Pane of the debugger window, to display the value of the SQL in the string before using it (That's assuming you're not good with debugging generally. Personally I would trace to the line, then display the value prior to allowing execution of the string - See Debugging in VBA). It's really much easier to appreciate what a SQL string is meant to do, and where there may be problems, when you can see it in its entirety, and in its true form, rather than as the code is about to create it.
Nov 15 '09 #4
Thank you, NeoPa for your insight. It's helpful to a novice like me.
Nov 16 '09 #5
NeoPa
32,556 Expert Mod 16PB
I'm very pleased to hear that.

Good luck and Welcome to Bytes!
Nov 16 '09 #6

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

Similar topics

8
by: Dalan | last post by:
Please help - just take a quick look at the function code below. It probably just needs some minor tweaking. The function module is based on an intermediate query to provide a group record...
1
by: Dalan | last post by:
I can't seem to find a workaround of Query Syntax Error. Actually, the query performs just fine, except when the last record on a related subform is deleted, then it generates a Runtime Error 3075...
1
by: Dalan | last post by:
I'm experiencing a Query Syntax Error with an Access 97 Db. Actually, the query performs as expected when adding any new records or editing existing ones and even deleting records, EXCEPT when the...
2
by: jwa6 | last post by:
syntax for a dlookup ok I give up! here is the error... ' Run-time error 3075 Syntax error in query expression '...' this is the code before that a contractor wrote. (I dont know if it ever...
24
by: deko | last post by:
I'm trying to log error messages and sometimes (no telling when or where) the message contains a string with double quotes. Is there a way get the query to insert the string with the double...
4
by: T. Wintershoven | last post by:
Hi Can someone please tell me whats wrong with the last line of the query below. The first three lines work fine but when i add the fourth line i get an error message (see text at ERROR...
3
by: injanib via AccessMonster.com | last post by:
Hello Access Monsters, I have a table with three columns. "CostCenter", "ProjectNumber", "GLCode" On my form I have three texboxes, "txtCostCenter", "txtProjectNumber" and "txtGLCode". the...
4
by: srinathvs | last post by:
Hi, I have an access db that I am trying to query from a vb6 program. I've the following code: Dim sSQLQuery As String sSQLQuery = "SELECT * FROM TblData WHERE ID = " & Chr(39) & ID &...
3
by: timber910 | last post by:
Hello All, I'm in need of help here. I have build my query in a query builder in access as a select query. Query runs fine. Changed it to a make table query. Query makes table ok. Copy and pasted...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.