I have written the below VBA. What it is trying to do: It pulls data from another sheet copies and pastes it, does a find and replace and then uploads the data to a database.
The problem: something in the code is turning off the Find and Replace function, so once it gets to this point, it gives an error. Once I close the macro and try a regular find and replace, no matter what I try to find, it says it cannot find it. Does anyone see anything in this code that would cause the find and replace to no longer work?
The problem: something in the code is turning off the Find and Replace function, so once it gets to this point, it gives an error. Once I close the macro and try a regular find and replace, no matter what I try to find, it says it cannot find it. Does anyone see anything in this code that would cause the find and replace to no longer work?
Code:
Sub RRUpload()
Dim cnt As New ADODB.Connection, _
rst As New ADODB.Recordset, _
dbPath As String, _
tblName As String, _
rngColHeads As Range, _
rngTblRcds As Range, _
colHead As String, _
rcdDetail As String, _
ch As Integer, _
cl As Integer, _
notNull As Boolean
Dim ws As Worksheet
Set ws = Worksheets("RentRollUpload")
Set ms = Worksheets("DB")
'Set the string to the path of the database as defined on the worksheet
dbPath = ms.Range("A50")
tblName = ms.Range("A57")
Set rngColHeads = ws.Range("RRhdrs")
Set rngTblRcds = ws.Range("RRdat")
'Concatenate a string with the names of the column headings
colHead = " ("
For ch = 1 To rngColHeads.Count
colHead = colHead & rngColHeads.Columns(ch).Value
Select Case ch
Case Is = rngColHeads.Count
colHead = colHead & ")"
Case Else
colHead = colHead & ","
End Select
Next ch
'Open connection to the database
cnt.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & dbPath & ";"
'Begin transaction processing
'On Error GoTo EndUpdate
cnt.BeginTrans
'Insert records into database from worksheet table
For cl = 1 To rngTblRcds.Rows.Count
'Assume record is completely Null, and open record string for concatenation
notNull = False
rcdDetail = "('"
'Evaluate field in the record
For ch = 1 To rngColHeads.Count
Select Case rngTblRcds.Rows(cl).Columns(ch).Value
'if empty, append value of null to string
Case Is = Empty
Select Case ch
Case Is = rngColHeads.Count
rcdDetail = Left(rcdDetail, Len(rcdDetail) - 1) & "NULL)"
Case Else
rcdDetail = Left(rcdDetail, Len(rcdDetail) - 1) & "NULL,'"
End Select
'if not empty, set notNull to true, and append value to string
Case Else
notNull = True
Select Case ch
Case Is = rngColHeads.Count
rcdDetail = rcdDetail & rngTblRcds.Rows(cl).Columns(ch).Value & "')"
Case Else
rcdDetail = rcdDetail & rngTblRcds.Rows(cl).Columns(ch).Value & "','"
End Select
End Select
Next ch
'If record consists of only Null values, do not insert it to table, otherwise
'insert the record
Select Case notNull
Case Is = True
rst.Open "INSERT INTO " & tblName & colHead & " VALUES " & rcdDetail, cnt
Case Is = False
'do not insert record
End Select
Next cl
EndUpdate:
'Check if error was encounted
If Err.Number <> 0 Then
'Error encountered. Rollback transaction and inform user
On Error Resume Next
cnt.RollbackTrans
MsgBox "Error! RentRoll Data upload was not succesful!", vbCritical, "Error!"
Else
On Error Resume Next
cnt.CommitTrans
MsgBox "RentRoll Data Upload was Succesful.", vbInformation, "Success!"
End If
'Close the ADO objects
cnt.Close
Set rst = Nothing
Set cnt = Nothing
On Error GoTo 0
End Sub