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

SQL Syntax for Strings in Select Queries

I need help! I think that my problem is one of syntax using strings. Specifically I have defined a string field called issuername that I am later trying to use as an input into select queries. I have tried different syntaxes because I believe I need to use the & operator in SQL to let it know that it is searching for a string. Despite trying several approaches I have not succeeded. Any ideas? My code is below: Thanks, Adrian

Expand|Select|Wrap|Line Numbers
  1. Public Sub RatingCalc()
  2.  
  3. Dim db As Database
  4. Dim bsmatrix As DAO.Recordset
  5. Dim ratinggroup As DAO.Recordset
  6. Dim ratings1 As DAO.Recordset
  7. Dim ratings2 As DAO.Recordset
  8. Dim selldate As String
  9. Dim buydate As String
  10. Dim ratingtypeID As Double
  11. Dim recentrating As Double
  12. Dim previousrating As Double
  13. Dim ratingchange As Double
  14. Dim issuername As String
  15. Dim ratingscount As Double
  16. Set db = CurrentDb
  17.  
  18. 'Create recordset of all sell tradedates and corresponding preceding buydates (many-to-many relationship)
  19.  
  20. Dim mySQL As String
  21. mySQL = "SELECT * FROM zBuySellMatrix"
  22.  
  23. Set bsmatrix = db.OpenRecordset(mySQL, dbOpenDynaset)
  24.  
  25. bsmatrix.MoveFirst
  26. Do While Not bsmatrix.EOF
  27. selldate = bsmatrix!selldate
  28. buydate = bsmatrix!buydate
  29. issuername = bsmatrix!Issuer_Name
  30.  
  31. 'vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  32. 'Creates ratinggroup recordset for the Issuer
  33.  
  34. Dim mySQL3 As String
  35.  
  36. mySQL3 = "SELECT z_RatingsByIssuer.Rating_RatingType_ID, z_RatingsByIssuer.Rating_RatingType_Name"
  37. mySQL3 = mySQL3 + " FROM z_RatingsByIssuer"
  38. mySQL3 = mySQL3 + " WHERE (((z_RatingsByIssuer.Issuer_Name)= ' & issuername & '))"
  39. mySQL3 = mySQL3 + " GROUP BY z_RatingsByIssuer.Rating_RatingType_ID, z_RatingsByIssuer.Rating_RatingType_Name"
  40.  
  41. Set ratinggroup = db.OpenRecordset(mySQL3, dbOpenDynaset)
  42.  
Jun 30 '10 #1
3 1598
QVeen72
1,445 Expert 1GB
Hi,

A Double Quote to End and Restart the string...

....Issuer_Name)= '" & issuername & "'))"

Regards
Veena
Jun 30 '10 #2
Hi Veena,

Thanks for your help. I tried your suggestion but got the following error - any ideas? Copy of the new code below:

Run-tim error '3075'

Syntax error (missing operator) in query expression '(((z_RatingsByIssuer.Issuer_Name)='Reader's Digest Association, Inc.'))GROUP BY z_RatingsByIssuer.Rating_RatingType_ID,zRatingsByI ssuer.Rating_Rating_Type_Name'

Expand|Select|Wrap|Line Numbers
  1. Dim mySQL3 As String
  2.  
  3. mySQL3 = "SELECT z_RatingsByIssuer.Rating_RatingType_ID, z_RatingsByIssuer.Rating_RatingType_Name"
  4. mySQL3 = mySQL3 + " FROM z_RatingsByIssuer"
  5. mySQL3 = mySQL3 + " WHERE (((z_RatingsByIssuer.Issuer_Name)='" & issuername & "'))"
  6. mySQL3 = mySQL3 + " GROUP BY z_RatingsByIssuer.Rating_RatingType_ID, z_RatingsByIssuer.Rating_RatingType_Name"
  7.  
  8. Set ratinggroup = db.OpenRecordset(mySQL3, dbOpenDynaset)
  9.  
Jul 7 '10 #3
QVeen72
1,445 Expert 1GB
Hi,

Ok... Since your String contains Single Quote, you have to write an extra Single quote for each one...

Try this :

Expand|Select|Wrap|Line Numbers
  1. issuerName = Replace(issuername,"'","''")
  2.  
  3. mySQL3 = "SELECT z_RatingsByIssuer.Rating_RatingType_ID, z_RatingsByIssuer.Rating_RatingType_Name" 
  4. mySQL3 = mySQL3 + " FROM z_RatingsByIssuer" 
  5. mySQL3 = mySQL3 + " WHERE (((z_RatingsByIssuer.Issuer_Name)='" & issuername & "'))" 
  6. mySQL3 = mySQL3 + " GROUP BY z_RatingsByIssuer.Rating_RatingType_ID, z_RatingsByIssuer.Rating_RatingType_Name" 
  7.  
  8.  
Regards
Veena
Jul 8 '10 #4

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

Similar topics

0
by: Tinku | last post by:
Hi All We are migrating one of our application from Informix to Oracle. Now one of our tables contains a column named "MODE" which is a reserved word. I could create the table with this name...
0
by: Vlad | last post by:
I operate with 2 tables: tbLocations and tbDepartments. Both have a column named Town. tbLocations.Town looks like long unique list of different towns world wide: Copenhagen Madrid Roma
4
by: Paul | last post by:
I have run into a strange problem with a site I am working on. My SELECT queries work fine, but I just tried to add an UPDATE statement and it fails. A test showed that INSERT fails also. I created...
9
by: mfyahya | last post by:
Hi, I'm new to databases :) I need help speeding up select queries on my data which are currently taking 4-5 seconds. I set up a single large table of coordinates data with an index on the fields...
14
by: Martin Lacoste | last post by:
Access 2000 Trying even the most basic queries - select queries joining (inner join) two tables - and the results are not updatable, and I can't tell why they aren't. All permissions are on,...
4
MMcCarthy
by: MMcCarthy | last post by:
To view Access queries in SQL rather than Access query design - open the query design window and change the view to SQL: Select Statement SELECT FROM ; Append Statement INSERT INTO (, , )...
2
by: gggram2000 | last post by:
Hi! I have a SQLDataAdapter and I want to retrieve data from my database in SQL Server 2005 . Supposing I have two tables named Guest, Sales and one of the columns is GuestFirstName. I want to...
0
by: Rahul Babbar | last post by:
Hi, We have a situation in which we want to specify the isolation level ='UR' for all the select queries if no other isolation level is explicitly specified in the query. Is there some...
1
by: canugi | last post by:
I need to store the contents of an SQL "in clause" in an MS Access 2000 table. I use MS Access version 9.0.8960 (SP3) This is my statement (and it works fine with the explicit "in clause"...
11
by: pattyd | last post by:
I have multiple queries that will run on my onopen event of my report. I use the docmd.openquery for the select queries the problem is I can see all these popup in the background. I do use the close...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.