running sql in access

Here is my problem...trying to run a query to bring back a single record from a table using data from a form as the criteria. I then want to put the returned value as value of a text box in the form.  Here is what I have to start

DoCmd.RunSQL ("SELECT UPC AS UPC FROM tbl_Reference_Table WHERE strLongDesc = " & List95)
Text4 = UPC

I get the following error...3075 Syntax error (missing operator) in query expression 'strLongDesc = (value of List95 is shown, no parentheses)'

Any help is appreciated
avoelkerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
Try use the DAO object library, try add it from the Reference, and here is an example:

Dim db As DAO.Database
    Dim rs As DAO.Recordset
   
    Set db = CurrentDb()
   
    Set rs = db.OpenRecordset("SELECT UPC AS UPC FROM tbl_Reference_Table WHERE strLongDesc = " & List95)
    If rs.EOF Then
        MsgBox "Cannot open member data", vbCritical, "Member ID Not Found"
        Exit Sub
    Else
        Text4 = "" & Rs("UPCS")
   End If

Hope this helps
0
DhaestCommented:
You also have tho be shore of what kind of type your are searching and the type of your parameters.
Is your list95 a textvalue, then you have to adjust your sql to --> ... where strlongdesc = '" & list95 & "'"

together with teh dao-stuff:



Dim db As DAO.Database
Dim rs As DAO.Recordset
   
Set db = CurrentDb()
   
Set rs = db.OpenRecordset("SELECT UPC AS UPC FROM tbl_Reference_Table WHERE strLongDesc = '" & List95 & "'")
   If rs.EOF Then
       MsgBox "Cannot open member data", vbCritical, "Member ID Not Found"
       Exit Sub
   Else
       Text4 = "" & Rs("UPCS")
  End If
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SethiCommented:
If you want to access data from Access through VB then you will have to use objects like DAO or ADO. DAO gels best with Access 97 while ADO is the technology that is being recently used to access data from Access 2000 and above and SQL server or any other database. As you are not aware of the technology I would advise you to spend a few hours with ADO before jumping into data access through VB. This will help you in a great way in future. Here are certain links that will give you tutorials and articles for ADO. If you want to have feelers for ADO then look into code samples from these links or download some readymade applications from www/planet-source-code.com. The links are:
http://www.vbcode.com/asp/code.asp?lstCategory=Database
http://www.codeguru.com/vb/Database/index.shtml
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Mayank SAssociate Director - Product EngineeringCommented:
>> Syntax error (missing operator) in query expression 'strLongDesc =

Obviously, there is an error in the query too. What is the data type of strLongDesc. If it is string, then please make it as:

"SELECT UPC FROM tbl_Reference_Table WHERE strLongDesc = '" & List95 & "' ; " 

You should get into the habit of using ADO/ Recordsets.

Mayank.
0
Mayank SAssociate Director - Product EngineeringCommented:
"SELECT UPC FROM tbl_Reference_Table WHERE strLongDesc = '" & List95 & "' ; " // please notice the single quotes - they are hardly visible here

Mayank.
0
VBtomCommented:
The error is because you have to write single quotes around the string in the criteria.
Your code would make access show the results of the query in an access-window, it's not the way for getting data and certainly no for showing them in a textbox.

You could use DAO or ADODB like ryancys and dhaest described. But you can do it easier:

There's an easy function in access that returns one field from one record from a query: DLookup(fieldname, sourcename, criteria).

This should do what you want:

Text4 = DLookup("UPC", "tbl_Reference_Table", strLongDesc = '" & List95 & "'"


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.