J
Jaq23
I recorded a macro to pull in an external dbf file using a SQL query
in VBA (Excel 2003 on an XP machine).
Everything works great until I point it at the original file (was
pointing at a renamed copy) that I want to pull in. The files I need
to pull in will have the format <output filename>~<model 1>~<model
2>~<sub model>.dbf.
The SQL query doesn't like the strange characters (~'s) in the
filename and if I try a wildcard within the filename (i.e. <output
filename>*<sub model> or <output filename>%<sub model>) it doesn't
like that either.
I can't create copies of the output files every time I want to pull
one in (some of them are huge ~1.4GB) and I can't rename the original
file as they're used by other people and processes.
Any suggestions on how I can get this to work consistently without
renaming or copying files?
Original code:
Sub ImportCNP()
Dim OutputLoc As String
Dim SQLQuery As String
Dim SQLQuery2 As String
Dim temp
Dim ModelLoc As String
Dim FileName As String
OutputLoc = "cnp_start"
ModelLoc = Range("cnp_loc").Value
FileName = Range("cnp_source").Value
FileName = Replace(FileName, ".dbf", "")
Range(Range(OutputLoc), Range(OutputLoc).End
(xlDown)).EntireRow.ClearContents
SQLQuery = "SELECT *" & Chr(13) & "" & Chr(10) & "FROM " &
FileName & " " & FileName & "" & Chr(13) & "" & Chr(10) & "WHERE " &
FileName & ".time = ' 0'" & Chr(13) & "" & Chr(10) & "ORDER BY
" & FileName & ".product, " & FileName & ".purpose, " & FileName &
".group"
Sheets("cnp_test").Select
Range("cnp_start").Select
Call getDBF(SQLQuery, OutputLoc, ModelLoc)
End Sub
Sub getDBF(strQuery As String, OutLoc As String, ModelLoc As String)
With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
"ODBC;Driver={Microsoft FoxPro VFP Driver
(*.dbf)};UID=;;SourceDB=" & ModelLoc &
";SourceType=DBF;Exclusive=No;BackgroundFetch" _
), Array("=Yes;Collate=Machine;Null=Yes;Deleted=Yes;")),
Destination:=Range(OutLoc))
.CommandText = Array(strQuery)
.Name = "Query from Model_" & OutLoc
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub
in VBA (Excel 2003 on an XP machine).
Everything works great until I point it at the original file (was
pointing at a renamed copy) that I want to pull in. The files I need
to pull in will have the format <output filename>~<model 1>~<model
2>~<sub model>.dbf.
The SQL query doesn't like the strange characters (~'s) in the
filename and if I try a wildcard within the filename (i.e. <output
filename>*<sub model> or <output filename>%<sub model>) it doesn't
like that either.
I can't create copies of the output files every time I want to pull
one in (some of them are huge ~1.4GB) and I can't rename the original
file as they're used by other people and processes.
Any suggestions on how I can get this to work consistently without
renaming or copying files?
Original code:
Sub ImportCNP()
Dim OutputLoc As String
Dim SQLQuery As String
Dim SQLQuery2 As String
Dim temp
Dim ModelLoc As String
Dim FileName As String
OutputLoc = "cnp_start"
ModelLoc = Range("cnp_loc").Value
FileName = Range("cnp_source").Value
FileName = Replace(FileName, ".dbf", "")
Range(Range(OutputLoc), Range(OutputLoc).End
(xlDown)).EntireRow.ClearContents
SQLQuery = "SELECT *" & Chr(13) & "" & Chr(10) & "FROM " &
FileName & " " & FileName & "" & Chr(13) & "" & Chr(10) & "WHERE " &
FileName & ".time = ' 0'" & Chr(13) & "" & Chr(10) & "ORDER BY
" & FileName & ".product, " & FileName & ".purpose, " & FileName &
".group"
Sheets("cnp_test").Select
Range("cnp_start").Select
Call getDBF(SQLQuery, OutputLoc, ModelLoc)
End Sub
Sub getDBF(strQuery As String, OutLoc As String, ModelLoc As String)
With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
"ODBC;Driver={Microsoft FoxPro VFP Driver
(*.dbf)};UID=;;SourceDB=" & ModelLoc &
";SourceType=DBF;Exclusive=No;BackgroundFetch" _
), Array("=Yes;Collate=Machine;Null=Yes;Deleted=Yes;")),
Destination:=Range(OutLoc))
.CommandText = Array(strQuery)
.Name = "Query from Model_" & OutLoc
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub