Solved

Not In List in Access ComboBox

Posted on 2013-12-13
4
385 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 49

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 49

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

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…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of utilizing SQL Server functions 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 Ac…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

776 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