Some annotations by me:
1. There is no difference of "First" and "Second" cases.
Both doing the same things with a different workbook variables.
I thing that that could be enough:
Code:
Set xlWs = xlApp.Workbooks.Add.ActiveSheet
2. Note that the method Dir(lSQLFolder & Qname) may return an empty string.
So, when you try to open a file with the directory as file name, you get the error 52 (Bad file name or number).
I suggest:
Code:
strFile = Dir(lSQLFolder & Qname)
If Len(strFile)>0 Then
On Error GoTo EndMacro
strSQL = ""
FileNumber = FreeFile
Open lSQLFolder & strFile For Input Access Read As #FileNumber
'bla bla...
'bla bla...
3. In case of error, the code jumps to the EndMacro label and the object rst and FileNumber stays open and in memory.
Maybe in this case you get the error 55 (file already open).
Also, even in normal conditions, all Excel objects stays in memory.
I suggest:
Code:
[...]
ExitMacro:
On Error Resume Next
rst.Close
Set rst = Nothing
Set xlWs = Nothing
Close #FileNumber
Exit Sub
EndMacro:
Select Case Err.Number
'Case -2147467259
' MsgBox "The database is unavailable right now.", vbOKOnly, "Optum"
Case Else
MsgBox Err.Number & vbCrLf & vbCrLf & Err.Description, vbOKOnly, "Optum"
End Select
Resume ExitMacro
End Sub
4. Does this loop looks for the last "~" in WholeLine?
Code:
While NextPos >= 1
Pos = NextPos + 1
NextPos = InStr(Pos, WholeLine, "~")
Wend
This line does the same:
Code:
NextPos = InStrRev(WholeLine, "~")
Cheers,
John