The code below is functional and works as expected, but I imagine there is a better way to test for the error that I am testing for .
The scenario is that data is transferred between two different servers throughout the year, so I built in an error handler that checks to see if the connection to that server is valid; if the connection is not valid then it goes to an Error Handler. I am seeking review of this code to streamline it and hopefully process it more efficiently.
CODE:
Option Explicit
Sub CIFIncoming()
Dim adoConn As New ADODB.Connection
Dim cfRS As New ADODB.Recordset
Dim Name As String, Address1 As String, Address2 As String
Dim City As String, State As String, Zip As String
Dim HomePhone As String, CellPhone As String
Dim BSA As String
Dim strConn As String
Dim CIFstr As String, CIF As String
On Error GoTo ErrHandler
'\\\\BEGIN DATABASE INFORMATION GRAB////
' 1. Sets the Connection String to the Data Base
' 2. Opens the connection to the database
' 3. Sets the SQL String to get the fields from the Data Base
' 4. Defines the CIF Number to use in the SQL String
' 5. Opens the Recordset
' 6. Checks to see where the cursor in the DataBase is and runs the code based on that conditon
' BOF = Begining of File
' EOF = End of File
strConn = REDACTED
adoConn.Open strConn
CIF = UCase(Sheet1.Range("B103").Text)
CIFstr = "SELECT " & _
"cfna1, cfna2, cfna3, cfcity, cfstat, LEFT(cfzip, 5), cfhpho, cfcel1, cfudsc6 " & _
"FROM cncttp08.jhadat842.cfmast cfmast " & _
"WHERE cfcif# = '" & CIF & "'"
cfRS.Open CIFstr, adoConn
If Not (cfRS.BOF And cfRS.EOF) Then
'\\\\END DATABASE INFORMATION GRAB////
'\\\\BEGIN WORKSHEET INFORMATION PLACEMENT////
' 1. Assigns each field from the Database to a variable
' 2. Moves data from Database to specific cells
Name = Trim(cfRS(0)) 'cfna1
Address1 = Trim(cfRS(1)) 'cfna2
Address2 = cfRS(2) 'cfna3
City = Trim(cfRS(3)) 'cfcity
State = Trim(cfRS(4)) 'cfstat
Zip = cfRS(5) 'cfzip
HomePhone = cfRS(6) 'cfhpho
CellPhone = cfRS(7) 'cfcel1
BSA = cfRS(8) 'cfudsc6
With Sheet1
.Range("B104") = Name
.Range("B105") = Address1
.Range("B106") = Address2
.Range("B107") = City & ", " & State & " " & Zip
End With
End If
If Sheet1.Range("B103") = vbNullString Then
With Sheet1
.Range("B104") = vbNullString
.Range("B105") = vbNullString
.Range("B106") = vbNullString
.Range("B107") = vbNullString
End With
End If
'\\\\END WORKSHEET INFORMATION PLACEMENT////
'\\\\BEGIN FINAL DATABASE OPERATIONS////
' 1. Closes connection to Database
' 2. Sets the Recordset from the Database to Nothing
' 3. Exits sub when there are no errors
cfRS.Close
Set cfRS = Nothing
Exit Sub
'\\\\END FINAL DATABASE OPERATIONS
ErrHandler:
If Err.Number = -2147467259 Then GoTo Branson
Branson:
CIF = UCase(Sheet1.Range("B103").Text)
CIFstr = "SELECT " & _
"cfna1, cfna2, cfna3, cfcity, cfstat, LEFT(cfzip, 5), cfhpho, cfcel1, cfudsc6 " & _
"FROM bhschlp8.jhadat842.cfmast cfmast " & _
"WHERE cfcif# = '" & CIF & "'"
cfRS.Open CIFstr, adoConn
If Not (cfRS.BOF And cfRS.EOF) Then
'\\\\END DATABASE INFORMATION GRAB////
'\\\\BEGIN WORKSHEET INFORMATION PLACEMENT////
' 1. Assigns each field from the Database to a variable
' 2. Moves data from Database to specific cells
Name = Trim(cfRS(0)) 'cfna1
Address1 = Trim(cfRS(1)) 'cfna2
Address2 = cfRS(2) 'cfna3
City = Trim(cfRS(3)) 'cfcity
State = Trim(cfRS(4)) 'cfstat
Zip = cfRS(5) 'cfzip
HomePhone = cfRS(6) 'cfhpho
CellPhone = cfRS(7) 'cfcel1
BSA = cfRS(8) 'cfudsc6
With Sheet1
.Range("B104") = Name
.Range("B105") = Address1
.Range("B106") = Address2
.Range("B107") = City & ", " & State & " " & Zip
End With
End If
If Sheet1.Range("B103") = vbNullString Then
With Sheet1
.Range("B104") = vbNullString
.Range("B105") = vbNullString
.Range("B106") = vbNullString
.Range("B107") = vbNullString
End With
End If
'\\\\END WORKSHEET INFORMATION PLACEMENT////
'\\\\BEGIN FINAL DATABASE OPERATIONS////
' 1. Closes connection to Database
' 2. Sets the Recordset from the Database to Nothing
' 3. Exits sub when there are no errors
cfRS.Close
Set cfRS = Nothing
Exit Sub
'\\\\END FINAL DATABASE OPERATIONS
End Sub