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
Solved

Not In List in Access ComboBox

Posted on 2013-12-13
4
386 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
  • 2
4 Comments
 
LVL 50

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 50

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 35

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

790 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