473,386 Members | 1,864 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.

OpenForm with single quote in criteria

matrekz42
Hello there! It's been a long time since I've reached out to the community for assistance, I thought I had this pretty much figured out! But, I've ran into a problem where I cant get the following to work, because in the Invention_Desc, one of the descriptions has a single quote in the string.

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "Inventions", , , "[Invention_Idea]=" & Me.[Invention_Idea] & " And [Design]='" & Me.[Design] & "'" & " And Invention_Desc = '" & Invention_Desc & "'"
Any recommendations would be awesome! I've been trying to figure this out for hours with no success.

Thank you in advance!
May 7 '19 #1
7 1118
NeoPa
32,556 Expert Mod 16PB
It's a good idea always to protect such code by using a function such as SQLSafe().
Expand|Select|Wrap|Line Numbers
  1. 'SQLSafe()  returns a value where all single-quotes (') are doubled up.
  2. '27/03/2019 Updated to handle Variants - especially for Null values.
  3. Public Function SQLSafe(ByVal varIn As Variant) As Variant
  4.     SQLSafe = varIn
  5.     If varIn > "" Then SQLSafe = Replace(CStr(varIn), "'", "''")
  6. End Function
Thus, this particular line of code would be :
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "Inventions", , , "[Invention_Idea]=" & SQLSafe(Me.[Invention_Idea]) & " And [Design]='" & SQLSafe(Me.[Design]) & "'" & " And Invention_Desc = '" & SQLSafe(Me.Invention_Desc) & "'"
May 7 '19 #2
Hi Neo, good to hear from you. I still received the same error. The name has the following: customer's and hence the reason I get the error because of the '.
May 7 '19 #3
NeoPa
32,556 Expert Mod 16PB
Hi Matrekz. You too.

You shouldn't if it's done properly so I wonder exactly how you've implemented the suggestion. Care to share some details?
May 7 '19 #4
twinnyfo
3,653 Expert Mod 2GB
This is more of a workaround, rather than a solution, but you can replace the straight quote "'" with a smart quote "’".

This prevents the issue, but does not "resolve" it.
May 8 '19 #5
Hi Neo, I created a module and entered the function. I then replaced the docmd.openform with
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "Inventions", , , "[Invention_Idea]=" & SQLSafe(Me.[Invention_Idea]) & " And [Design]='" & SQLSafe(Me.[Design]) & "'" & " And Invention_Desc = '" & SQLSafe(Me.Invention_Desc) & "'"
May 8 '19 #6
thank you twinnyfo, I changed this in the meantime to keep working. I would ultimately like to get the SQLSafe working, but this definitely helped.
May 8 '19 #7
NeoPa
32,556 Expert Mod 16PB
I was sloppy in my earlier post and just made the necessary changes without checking the rest was ok. Try this version :
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "Inventions", , , "([Invention_Idea]='" & SQLSafe(Me.Invention_Idea) & "') AND ([Design]='" & SQLSafe(Me.Design) & "') AND ([Invention_Desc]='" & SQLSafe(Me.Invention_Desc) & "')"
Or try the way I would do it in my own code :
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2.  
  3. strSQL = "([Invention_Idea]='%II') AND " _
  4.        & "([Design]='%D') AND " _
  5.        & "([Invention_Desc]='%ID')"
  6. strSQL = Replace(strSQL, "%II", SQLSafe(Me.Invention_Idea))
  7. strSQL = Replace(strSQL, "%D", SQLSafe(Me.Design))
  8. strSQL = Replace(strSQL, "%ID", SQLSafe(Me.Invention_Desc))
  9. Call DoCmd.OpenForm(FormName:="Inventions", WhereCondition:=strSQL)
That makes it more legible as well as giving you the opportunity to see the resolved string before you run it when debugging.

As a general rule I'm not a great fan of using other types of quotes, either in the code or in the data. It can often get you past bumps in the road but you tend to hit them again later when you're going faster.
May 9 '19 #8

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

Similar topics

9
by: Hugo Coolens | last post by:
I need to use the str_replace function for a php-script which works together with html/javascript. In the script I have to perform things like: $arabic=str_replace ("'","\'", $row);...
4
by: sankofa | last post by:
hi, i can't seem to be able to escape my single quote properly... is it even possible in javascript? this is a portion of my code.. var DLEWIS="Pastor Lewis"; .... Sermon is a yser-defined...
8
by: Steve | last post by:
How do I get a single quote (') in a NVARCHAR string in MS SQL Server? e.g. SELECT @strsql = "SELECT * FROM tblTest WHERE Field1 Like 'blah'' Obviously this is invalid as the single quote...
3
by: micmic | last post by:
Deear all experts, In the MySQL, we can use escape character '\' to save the STRING WITH single quote into database (eg. we would like to insert into table "tbl_ABC"with the string ab'c, we...
4
by: Paul | last post by:
I've got some code that adds a single quote to any ad hoc queries that appear to look like hacks. For instance, if somebody enters ' OR 1=1 -- then this code adds another single quote the string...
5
by: Mark Woodward | last post by:
Hi all, I'm trying to validate text in a HTML input field. How do I *allow* a single quote? // catch any nasty characters (eg !@#$%^&*()/\) $match = '/^+$/'; $valid_srch = preg_match($match,...
3
by: Eric Layman | last post by:
Hi, I've saved data into the db by doing a replace() on single quote. Right now on data display on a datagrid, it shows double single quote. How do I make changes during run time of datagrid...
4
by: fniles | last post by:
I am looping thru DataReader and constructing a sql query to insert to another database. When the data type of the field is string I insert the field value using a single quote. When the value of...
0
by: dhascuba | last post by:
The @name field can contain a single quote in it such as: Mike O'Grady. Since this is creating and SQL statement on the server side, it will not process the name if it has a single quote in it. Id'...
2
by: gimme_this_gimme_that | last post by:
var a = 'what goes in here to get a single quote?';
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.