J
Jac Tremblay
Hi,
I want to place the information for the connection in a text file and use
that file in the code to create an ODBC query. When I put the connection
information directly in the code, the query runs fine but I can't get it to
work when it is in a text file.
Can someone point out the problem. Here is the code.
' ***********************************************************
Sub ConnectionViaTextFile()
Dim wbkConnection As Workbook
Dim shtConnection As Worksheet
Dim rngConnection As Range
Dim strWbkPath As String
Dim strConnection As String
Const cstrTextFile As String = "ConnectionText.txt"
Const cstrCommandText As String = _
"SELECT TRET_PREV_ASRA.PROG_ASSU, TRET_PREV_ASRA.PROD_ASSU, " & _
"TRET_PREV_ASRA.AN_ASSU, TRET_PREV_ASRA.D_DEB_ASSU, " & _
"TRET_PREV_ASRA.D_FIN_ASSU, TRET_PREV_ASRA.REVE_STAB, " & _
"TRET_PREV_ASRA.PRIX_MARC, TRET_PREV_ASRA.TAUX_COTI_UNIT, " & _
"TRET_PREV_ASRA.TAUX_COTI_PLAN_CONJ, TRET_PREV_ASRA.UNIT_COTI, " & _
"TRET_PREV_ASRA.UNIT_COMP, TRET_PREV_ASRA.TIMB_MAJ, " & _
"TRET_PREV_ASRA.USAG_MAJ" & vbCrLf & _
"FROM " & _
"""OPS$DEVLIB01""" & _
".TRET_PREV_ASRA TRET_PREV_ASRA" & vbCrLf & _
"ORDER BY TRET_PREV_ASRA.PROG_ASSU, TRET_PREV_ASRA.PROD_ASSU, " & _
"TRET_PREV_ASRA.AN_ASSU"
Set wbkConnection = ActiveWorkbook
Set shtConnection = wbkConnection.Sheets(1)
Set rngConnection = shtConnection.Range("A1")
strWbkPath = wbkConnection.Path
If Right(strWbkPath, 1) <> "\" Then
strWbkPath = strWbkPath & "\"
End If
strConnection = """TEXT;" & strWbkPath & cstrTextFile & """"
MsgBox strConnection
With shtConnection.QueryTables.Add( _
Connection:=strConnection, _
Destination:=rngConnection, _
Sql:=cstrCommandText)
.Name = "QueryTest"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFileTabDelimiter = True
.TextFileCommaDelimiter = True
.Refresh BackgroundQuery:=False
End With
Set rngConnection = Nothing
Set shtConnection = Nothing
Set wbkConnection = Nothing
End Sub
' ***********************************************************
The code bugs on
With shtConnection.QueryTables.Add( _
Connection:=strConnection, _
Destination:=rngConnection, _
Sql:=cstrCommandText)
I use Excel 2000 and Windows XP Pro.
Thanks
I want to place the information for the connection in a text file and use
that file in the code to create an ODBC query. When I put the connection
information directly in the code, the query runs fine but I can't get it to
work when it is in a text file.
Can someone point out the problem. Here is the code.
' ***********************************************************
Sub ConnectionViaTextFile()
Dim wbkConnection As Workbook
Dim shtConnection As Worksheet
Dim rngConnection As Range
Dim strWbkPath As String
Dim strConnection As String
Const cstrTextFile As String = "ConnectionText.txt"
Const cstrCommandText As String = _
"SELECT TRET_PREV_ASRA.PROG_ASSU, TRET_PREV_ASRA.PROD_ASSU, " & _
"TRET_PREV_ASRA.AN_ASSU, TRET_PREV_ASRA.D_DEB_ASSU, " & _
"TRET_PREV_ASRA.D_FIN_ASSU, TRET_PREV_ASRA.REVE_STAB, " & _
"TRET_PREV_ASRA.PRIX_MARC, TRET_PREV_ASRA.TAUX_COTI_UNIT, " & _
"TRET_PREV_ASRA.TAUX_COTI_PLAN_CONJ, TRET_PREV_ASRA.UNIT_COTI, " & _
"TRET_PREV_ASRA.UNIT_COMP, TRET_PREV_ASRA.TIMB_MAJ, " & _
"TRET_PREV_ASRA.USAG_MAJ" & vbCrLf & _
"FROM " & _
"""OPS$DEVLIB01""" & _
".TRET_PREV_ASRA TRET_PREV_ASRA" & vbCrLf & _
"ORDER BY TRET_PREV_ASRA.PROG_ASSU, TRET_PREV_ASRA.PROD_ASSU, " & _
"TRET_PREV_ASRA.AN_ASSU"
Set wbkConnection = ActiveWorkbook
Set shtConnection = wbkConnection.Sheets(1)
Set rngConnection = shtConnection.Range("A1")
strWbkPath = wbkConnection.Path
If Right(strWbkPath, 1) <> "\" Then
strWbkPath = strWbkPath & "\"
End If
strConnection = """TEXT;" & strWbkPath & cstrTextFile & """"
MsgBox strConnection
With shtConnection.QueryTables.Add( _
Connection:=strConnection, _
Destination:=rngConnection, _
Sql:=cstrCommandText)
.Name = "QueryTest"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFileTabDelimiter = True
.TextFileCommaDelimiter = True
.Refresh BackgroundQuery:=False
End With
Set rngConnection = Nothing
Set shtConnection = Nothing
Set wbkConnection = Nothing
End Sub
' ***********************************************************
The code bugs on
With shtConnection.QueryTables.Add( _
Connection:=strConnection, _
Destination:=rngConnection, _
Sql:=cstrCommandText)
I use Excel 2000 and Windows XP Pro.
Thanks