Solved

Not In List in Access ComboBox

Posted on 2013-12-13
4
388 Views
Last Modified: 2013-12-13
I have the following code that fires in the NotInList Event for my combobox but when someone enters a name with a ' in it I get a run-time error. How can I fix my code to allow for characters?

Dim strsql As String
Dim i As Integer
Dim Msg As String
Dim s As String

'Exit this sub if the combo box is cleared
If NewData = "" Then Exit Sub


Msg = "'" & NewData & "' is not currently in the list." & vbCr & vbCr
Msg = Msg & "Do you want to add it?"

i = MsgBox(Msg, vbQuestion + vbYesNo, "Unknown Item...")
strsql = fProperCase(NewData, 1)
If i = vbYes Then
s = fProperCase(NewData, 1)
strsql = "INSERT INTO tblCompany(CompanyName) " _
& " SELECT '" & s & "';"

CurrentDb.Execute strsql, dbFailOnError
Response = acDataErrAdded
Else
Response = acDataErrContinue
Me.Undo
End If

Open in new window

0
Comment
Question by:Lawrence Salvucci
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 39716939
Hi,

use  Replace(strSQL,"'","''")

Regards
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 39716945
do I put that on both lines where it says strSQL?
0
 
LVL 51

Accepted Solution

by:
Rgonzo1971 earned 300 total points
ID: 39716985
Hi

Actually No I would do it like this

If NewData = "" Then Exit Sub
NewData = Replace(NewData ,"'","''") 

Open in new window

Regards
0
 
LVL 37

Assisted Solution

by:PatHartman
PatHartman earned 200 total points
ID: 39717068
Use double quotes as the delimiter rather than single quotes.  To make this easy for myself, I always create a public constant I can use.

Public Const QUOTE = """"

Then
strsql = "INSERT INTO tblCompany(CompanyName) " _
& " SELECT '" & s & "';"

Becomes

strsql = "INSERT INTO tblCompany(CompanyName) " _
& " SELECT " & QUOTE & s & QUOTE & ";"
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question