?
Solved

running sql in access

Posted on 2003-03-19
6
Medium Priority
?
146 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:avoelker
[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
6 Comments
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 8171914
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
 
LVL 53

Accepted Solution

by:
Dhaest earned 750 total points
ID: 8172061
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
 
LVL 18

Expert Comment

by:Sethi
ID: 8172067
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 30

Expert Comment

by:Mayank S
ID: 8172590
>> 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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8172600
"SELECT UPC FROM tbl_Reference_Table WHERE strLongDesc = '" & List95 & "' ; " // please notice the single quotes - they are hardly visible here

Mayank.
0
 
LVL 3

Expert Comment

by:VBtom
ID: 8172878
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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

777 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