Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Not In List in Access ComboBox

Posted on 2013-12-13
4
Medium Priority
?
391 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 52

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 52

Accepted Solution

by:
Rgonzo1971 earned 1200 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 39

Assisted Solution

by:PatHartman
PatHartman earned 800 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

722 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