473,396 Members | 2,121 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,396 software developers and data experts.

"wrapper" for raw INSERT INTO text?

Hi,

Question... Is there any way to put a wrapper on a raw string going
through SQL, or should I bite the bullet and use a recordset method
instead?

Situation:
I'm trying to have the user add comment data to a table. I calculate
and/or determine all of the variables except for that comment.

So, currently, I'm using an inputbox to grab a string, and then using
that string as part of an INSERT INTO SQL command...

strComments = InputBox("Add any comments about this
addition below. (can be blank)", "Comment")
strSQL = "INSERT INTO tblInstallationOrInUse " & _
"(relEquipmentID, relInstrumentID, CurrentlyInstalled,
Comments, DateInstalled, Location)" & _
" VALUES " & _
"(" & Forms!frminstall.EquipmentID & ", " &
Me.InstrumentID & ", Yes, '" & strComments & _
"', #" & datDate & "#, '" & strLocation & _
"')"
Debug.Print strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True

Works fine... Until someone decides to use a ", ', comma, or other
nasty punctuation in my comment box. Then it hoses my SQL statement
because it's just a simple text concatenation.

So....
- Scrub/wrap my comment somehow?
OR
- Just get over myself and use a recordset like a grownup Access
user...

Thanks,

Jon

Jan 30 '07 #1
2 2774
A comma shouln't be a problem, neither should a " as you are string
delimiting using ' .

To stop an ' embedded in the string being a problem simply double it up.

strComments = InputBox( _
"Add any comments about this addition below. (can be blank)", _
"Comment")

strComments = Replace(strComments , "'", "''",compare:=vbTextCompare)

' Rest of your code

Notes
"'" is a ' with a " either side
"''" is 2 ' with a " either side

--

Terry Kreft
"jonceramic" <jo********@gmail.comwrote in message
news:11**********************@a34g2000cwb.googlegr oups.com...
Hi,

Question... Is there any way to put a wrapper on a raw string going
through SQL, or should I bite the bullet and use a recordset method
instead?

Situation:
I'm trying to have the user add comment data to a table. I calculate
and/or determine all of the variables except for that comment.

So, currently, I'm using an inputbox to grab a string, and then using
that string as part of an INSERT INTO SQL command...

strComments = InputBox("Add any comments about this
addition below. (can be blank)", "Comment")
strSQL = "INSERT INTO tblInstallationOrInUse " & _
"(relEquipmentID, relInstrumentID, CurrentlyInstalled,
Comments, DateInstalled, Location)" & _
" VALUES " & _
"(" & Forms!frminstall.EquipmentID & ", " &
Me.InstrumentID & ", Yes, '" & strComments & _
"', #" & datDate & "#, '" & strLocation & _
"')"
Debug.Print strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True

Works fine... Until someone decides to use a ", ', comma, or other
nasty punctuation in my comment box. Then it hoses my SQL statement
because it's just a simple text concatenation.

So....
- Scrub/wrap my comment somehow?
OR
- Just get over myself and use a recordset like a grownup Access
user...

Thanks,

Jon

Jan 31 '07 #2
Yep, that's it. The replace method with two single quotes worked.
Thanks.

The reason the commas and " (double quotes) were issues was because of
how I was testing. If you have a ' (single quote) that causes the
initial problem, subsequent commas and double quotes _are_ parsed.

By eliminating the original problem of the single quote using the
"replace" method, then the comma and double quotes don't have chance
to be a problem.

Thanks Terry,

Jon

On Jan 31, 9:09 am, "Terry Kreft" <terry.kr...@mps.co.ukwrote:
A comma shouln't be a problem, neither should a " as you are string
delimiting using ' .

To stop an ' embedded in the string being a problem simply double it up.

strComments = InputBox( _
"Add any comments about this addition below. (can be blank)", _
"Comment")

strComments = Replace(strComments , "'", "''",compare:=vbTextCompare)

' Rest of your code

Notes
"'" is a ' with a " either side
"''" is 2 ' with a " either side

--

Terry Kreft

"jonceramic" <joncera...@gmail.comwrote in message

news:11**********************@a34g2000cwb.googlegr oups.com...
Hi,
Question... Is there any way to put a wrapper on a raw string going
through SQL, or should I bite the bullet and use a recordset method
instead?
Situation:
I'm trying to have the user add comment data to a table. I calculate
and/or determine all of the variables except for that comment.
So, currently, I'm using an inputbox to grab a string, and then using
that string as part of an INSERT INTO SQL command...
strComments = InputBox("Add any comments about this
addition below. (can be blank)", "Comment")
strSQL = "INSERT INTO tblInstallationOrInUse " & _
"(relEquipmentID, relInstrumentID, CurrentlyInstalled,
Comments, DateInstalled, Location)" & _
" VALUES " & _
"(" & Forms!frminstall.EquipmentID & ", " &
Me.InstrumentID & ", Yes, '" & strComments & _
"', #" & datDate & "#, '" & strLocation & _
"')"
Debug.Print strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
Works fine... Until someone decides to use a ", ', comma, or other
nasty punctuation in my comment box. Then it hoses my SQL statement
because it's just a simple text concatenation.
So....
- Scrub/wrap my comment somehow?
OR
- Just get over myself and use a recordset like a grownup Access
user...
Thanks,
Jon- Hide quoted text -

- Show quoted text -

Jan 31 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: x-herbert | last post by:
Hi, I have a small test to "compile" al litle script as a WMI-Tester. The script include a wmi-wrapper and "insert" the Win32-modeles. here the code: my "WMI-Tester.py" ----- import wmi
0
by: Roy Smith | last post by:
I had a class called rule and a subclass of rule, called bgpRule, in a file called bgp.py. I decided to move the rule class to its own file, called rule.py. The original file did "import rule"....
19
by: les_ander | last post by:
Hi, suppose I am reading lines from a file or stdin. I want to just "peek" in to the next line, and if it starts with a special character I want to break out of a for loop, other wise I want to...
1
by: David A Barajas | last post by:
Is is possible to launch an Access Application within another app, creating a Parent/Child relationship between the App/Access? I'm trying to create a system where I can distribute the Access app...
5
by: meenasamy | last post by:
Hi all i have used a script that i downloaded and uses the fopen($url,'r'); function to access a url to retrieve data and i get this error Fatal error: fopen(): Unable to find the wrapper...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
0
by: Charlie Miller | last post by:
Hi. I've run into a very strange problem with Python on the mac. I'm trying to write a python script which will monitor an application for exceptions. It turns out on a mac, to do this you want...
2
by: Peter Laman | last post by:
In my app I need to dynamically generate a series hyperlinks. Each hyperlink's action must be to focus a field in a <form>. I created the following function to create such a link (the argument is a...
0
by: Peter Frost | last post by:
We're running PHP4.3 on a Solaris 9 box. PHP was compiled with the following option: '--with-openssl=/usr/local/ssl' phpinfo() suggests that openssl is enabled. However, when we try to use...
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...
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...
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
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.