I have an MS Access form for entering song performances. It looks up titles from a table. If the title isn't there, it asks if you want to add it.
Private Sub SongTitle_NotInList(NewData As String, Response As Integer)
10 On Error GoTo SongTitle_NotInList_Error
Dim strSQL1 As String
Dim i As Integer
Dim Msg As String
'Exit this sub if the combo box is cleared
20 If NewData = "" Then Exit Sub
30 Msg = "'" & NewData & "' is not currently in the list." & vbCr & vbCr
40 Msg = Msg & "Do you want to add it?"
50 i = MsgBox(Msg, vbQuestion + vbYesNo, "This is a new Song Title...")
60 If i = vbYes Then
70 strSQL1 = "Insert Into tblClick ([SongTitle]) " & _
"values ('" & NewData & "');"
80 CurrentDb.Execute strSQL1, dbFailOnError
90 Response = acDataErrAdded
100 End If
110 On Error GoTo 0
120 Exit Sub
SongTitle_NotInList_Error:
130 MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure SongTitle_NotInList, line " & Erl & "."
End Sub
Unfortunately, I created that a long time ago and I don't really remember what any of the code does anymore.
It works just fine, except if the title has an apostrophe ("We're Going On", for instance).
Error 3075 (Syntax error (missing operator) in query expression ''We're');'.) in procedure SongTitle_NotInList, line 80.
I'm sure it's something simple, like escaping the apostrophe or putting it double-quotes. I've forgotten pretty much everything I learned about VB, over the years.