BarryCambridge
New member
- Local time
- Today, 04:26
- Joined
- Mar 10, 2017
- Messages
- 6
Hi there. My first post, so please be gentle!
I have a module in one of my databases which writes changes to an audit table. It works fine, but has always had an issue if there is an apostrophe in the field that's changed. The database is being used by more and more users, so it's time to sort it out. I think(!) I can use the replace() function to change the apostrophe to \' escape, but I can't get it in the right place in the code.
I'd be very grateful if anyone could advise me on this or if anyone has other suggestions. The code is:
Function WriteAudit(MyForm As Form)
Dim f As Form
Dim c As Control
Dim subject_id As Long
Dim frm As String
Dim user As String
Dim sql As String
Dim changes As String
Dim db As DAO.Database
Set f = MyForm
Set db = CurrentDb
subject_id = f.subject_id
frm = f.Name
user = Environ$("Username")
changes = ""
sql = "INSERT INTO tbl_audit " & _
"([subject_id], [form_name], [user], [change]) " & _
"VALUES ('" & subject_id & "', '" & frm & "', '" & user & "', "
For Each c In f.Controls
Select Case c.ControlType
Case acTextBox, acComboBox, acListBox, acOptionGroup
If IsNull(c.OldValue) And Not IsNull(c.Value) Then
changes = changes & _
c.Name & "--" & "BLANK" & "--" & c.Value & _
vbCrLf
ElseIf IsNull(c.Value) And Not IsNull(c.OldValue) Then
changes = changes & _
c.Name & "--" & c.OldValue & "--" & "BLANK" & _
vbCrLf
ElseIf c.Value <> c.OldValue Then
changes = changes & _
c.Name & "--" & c.OldValue & "--" & c.Value & _
vbCrLf
End If
End Select
Next c
sql = sql & "'" & changes & "');"
db.Execute sql, dbFailOnError
Set f = Nothing
Set db = Nothing
End Function
Many thanks, Barry
I have a module in one of my databases which writes changes to an audit table. It works fine, but has always had an issue if there is an apostrophe in the field that's changed. The database is being used by more and more users, so it's time to sort it out. I think(!) I can use the replace() function to change the apostrophe to \' escape, but I can't get it in the right place in the code.
I'd be very grateful if anyone could advise me on this or if anyone has other suggestions. The code is:
Function WriteAudit(MyForm As Form)
Dim f As Form
Dim c As Control
Dim subject_id As Long
Dim frm As String
Dim user As String
Dim sql As String
Dim changes As String
Dim db As DAO.Database
Set f = MyForm
Set db = CurrentDb
subject_id = f.subject_id
frm = f.Name
user = Environ$("Username")
changes = ""
sql = "INSERT INTO tbl_audit " & _
"([subject_id], [form_name], [user], [change]) " & _
"VALUES ('" & subject_id & "', '" & frm & "', '" & user & "', "
For Each c In f.Controls
Select Case c.ControlType
Case acTextBox, acComboBox, acListBox, acOptionGroup
If IsNull(c.OldValue) And Not IsNull(c.Value) Then
changes = changes & _
c.Name & "--" & "BLANK" & "--" & c.Value & _
vbCrLf
ElseIf IsNull(c.Value) And Not IsNull(c.OldValue) Then
changes = changes & _
c.Name & "--" & c.OldValue & "--" & "BLANK" & _
vbCrLf
ElseIf c.Value <> c.OldValue Then
changes = changes & _
c.Name & "--" & c.OldValue & "--" & c.Value & _
vbCrLf
End If
End Select
Next c
sql = sql & "'" & changes & "');"
db.Execute sql, dbFailOnError
Set f = Nothing
Set db = Nothing
End Function
Many thanks, Barry