I have built a macro, wherein data is first imported into 'Raw' worksheet. Before importing, 'Raw' sheet is first cleared of previous data.
Then i query it using SQL in VBA and get the final output in desired format.
Lately i have been facing an issue, which gives a error message "The Connection for viewing your linked Microsoft Excel Worksheet was lost".
The same macro works fine if i dont clear the 'Raw' sheet before importing data. Any help is appreciated.
Option Explicit
Public ROW As Integer
Public Cnn As New ADODB.Connection
Public Rs As New ADODB.Recordset
Public StrSQL As String
Public Sub OpenDB()
If Cnn.State = adStateOpen Then Cnn.Close
Cnn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & _
ActiveWorkbook.Path & Application.PathSeparator & ThisWorkbook.Name
Cnn.Open
End Sub
Public Sub CloseRS()
If Rs.State = adStateOpen Then Rs.Close
Rs.CursorLocation = adUseClient
End Sub
Sub MonthlyYorN()
StrSQL1 = "SELECT * FROM [RAW$] where SOME CONDITION"
StrSQL1 = "SELECT * FROM [RAW$] where SOME CONDITION2"
StrSQL = StrSQL1 & " UNION " & StrSQL2
CloseRS
OpenDB
Rs.Open StrSQL, Cnn, adOpenKeyset, adLockOptimistic
If Rs.RecordCount > 0 Then
'Do Something
End If
End Sub
→