Solved

Not In List in Access ComboBox

Posted on 2013-12-13
4
380 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 48

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 48

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 34

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now