Using the code below, everything works well until one of the CompanyName values have an apostrophe in it.
VBA generates an error.
For Example: CompanyName = "John's Plumbing Supplies"
How can I modify the code to be able to update the CompanyName Field in Access when the string has an apostrophe in it?
I am using the following code:
VBA generates an error.
For Example: CompanyName = "John's Plumbing Supplies"
How can I modify the code to be able to update the CompanyName Field in Access when the string has an apostrophe in it?
I am using the following code:
VBA Code:
Public Sub QUpdateCompanyName()
Dim connDB As New ADODB.Connection
Dim adoRecSet As New ADODB.Recordset
Dim sSQL As String
TotalRow = ThisWorkbook.Worksheets("SQLQuoteLogStaging").Cells(Rows.count, 81).End(xlUp).Row
'your data source with which to establish connection - MS Access Database Name:
strDBName = "CCcalc.accdb"
'get path / location of the database, presumed to be in the same location as the host workbook:
strMyPath = "C:\Users\phili\Dropbox\"
'set the string variable to the Database:
strDB = strMyPath & strDBName
connDB.Open ConnectionString:="Provider = Microsoft.ACE.OLEDB.12.0; data source=" & strDB
TableName = "QuoteLog"
For i = 2 To TotalRow
CustID = ThisWorkbook.Worksheets("SQLQuoteLogStaging").Range("CC" & i).Value
CompanyName = ThisWorkbook.Worksheets("SQLQuoteLogStaging").Range("CD" & i).Value
sSQL = "UPDATE " & TableName & " SET CompanyName ='" & CompanyName & "' " & " WHERE CustID='" & CustID & "'"
'Performs the actual query
connDB.Execute (sSQL)
Next i
End Sub