avoelker
asked on
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
http://www.vbcode.com/asp/code.asp?lstCategory=Database
http://www.codeguru.com/vb/Database/index.shtml
>> 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.
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.
"SELECT UPC FROM tbl_Reference_Table WHERE strLongDesc = '" & List95 & "' ; " // please notice the single quotes - they are hardly visible here
Mayank.
Mayank.
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 & "'"
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 & "'"
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