Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 147
  • Last Modified:

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
0
avoelker
Asked:
avoelker
1 Solution
 
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
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now