Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Problem with CSV web query

Hello all,

I've come across a problem that has stumped me and now I'm ready to
pull my hair out.

I query a website for a file each day (or various dates). I have no
problem getting the CSV file off the website and importing it into
Excel. One cell is set to a date - this date is then converted to a
string and put into the URL.

If I try to pull the file and the file does not happen to exist, Excel
gets the error 1004 - file does not exist. That is expected.

But, if I change the date to a file that I know does exist, Excel will
not attempt to refetch the page. Instead it gives me the 1004 error,
despite the fact that it is refering to the new URL that does
exist.

Here is the code:

Sub import()
'
' import Macro
'

'
d = Range("tomorrow").Value

Dim dateString As String
Dim qt As QueryTable
Dim conn As String
Dim dest As Range

dateString = ""

' year is always 4 digits, so we don't need to modify it
dateString = dateString & Year(d)

Dim tempInt As Integer

tempInt = Month(d)

' add a 0 in front of the month number if it's less than 10
If tempInt < 10 Then
dateString = dateString & "0" & tempInt
Else
dateString = dateString & tempInt
End If

' add a 0 in front of the day number if it's less than 10
tempInt = Day(d)

If tempInt < 10 Then
dateString = dateString & "0" & tempInt
Else
dateString = dateString & tempInt
End If

' now dateString is of the form yyyymmdd

On Error GoTo err_file

conn = "TEXT;http://www.nepool.com/histRpts/da-lmp/lmp_da_" &
dateString & ".csv"
Set dest = Worksheets("DA Pull").Range("A34")

If Worksheets("DA Pull").QueryTables.Count 0 Then
Set qt = Worksheets("DA Pull").QueryTables(1)
qt.Connection = conn
qt.Destination = dest
Else
Set qt = Worksheets("DA
Pull").QueryTables.Add(Connection:=conn, Destination:=dest)
End If


With qt
.Name = "PUB_GenPlan"
On Error GoTo err_file
.RobustConnect = xlNever
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = False
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.AdjustColumnWidth = False
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=True
.SaveData = True
End With

Exit Sub

err_file:
qt.Delete
Set qt = Nothing
Exit Sub

End Sub

This is run on XP and Excel 2003

Thanks!!

-Zeqe

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 40
Default Problem with CSV web query

I am not sure if it will work, but try adding the statement "On Error
Resume Next" after the "End With". It worked for me. Good Luck.
-pb


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Problem with CSV web query

What if you change this line
..Refresh BackgroundQuery:=True
to =False

NickHK

wrote in message
ups.com...
Hello all,

I've come across a problem that has stumped me and now I'm ready to
pull my hair out.

I query a website for a file each day (or various dates). I have no
problem getting the CSV file off the website and importing it into
Excel. One cell is set to a date - this date is then converted to a
string and put into the URL.

If I try to pull the file and the file does not happen to exist, Excel
gets the error 1004 - file does not exist. That is expected.

But, if I change the date to a file that I know does exist, Excel will
not attempt to refetch the page. Instead it gives me the 1004 error,
despite the fact that it is refering to the new URL that does
exist.

Here is the code:

Sub import()
'
' import Macro
'

'
d = Range("tomorrow").Value

Dim dateString As String
Dim qt As QueryTable
Dim conn As String
Dim dest As Range

dateString = ""

' year is always 4 digits, so we don't need to modify it
dateString = dateString & Year(d)

Dim tempInt As Integer

tempInt = Month(d)

' add a 0 in front of the month number if it's less than 10
If tempInt < 10 Then
dateString = dateString & "0" & tempInt
Else
dateString = dateString & tempInt
End If

' add a 0 in front of the day number if it's less than 10
tempInt = Day(d)

If tempInt < 10 Then
dateString = dateString & "0" & tempInt
Else
dateString = dateString & tempInt
End If

' now dateString is of the form yyyymmdd

On Error GoTo err_file

conn = "TEXT;http://www.nepool.com/histRpts/da-lmp/lmp_da_" &
dateString & ".csv"
Set dest = Worksheets("DA Pull").Range("A34")

If Worksheets("DA Pull").QueryTables.Count 0 Then
Set qt = Worksheets("DA Pull").QueryTables(1)
qt.Connection = conn
qt.Destination = dest
Else
Set qt = Worksheets("DA
Pull").QueryTables.Add(Connection:=conn, Destination:=dest)
End If


With qt
.Name = "PUB_GenPlan"
On Error GoTo err_file
.RobustConnect = xlNever
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = False
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.AdjustColumnWidth = False
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=True
.SaveData = True
End With

Exit Sub

err_file:
qt.Delete
Set qt = Nothing
Exit Sub

End Sub

This is run on XP and Excel 2003

Thanks!!

-Zeqe



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Query problem Keith H[_2_] Excel Discussion (Misc queries) 2 December 16th 09 04:30 PM
Problem with MS Query - can't edit query jarems Excel Discussion (Misc queries) 2 December 12th 05 09:42 AM
Problem with Query John Links and Linking in Excel 4 December 7th 04 02:49 PM
Problem in SQL Query Shilps Excel Programming 2 April 23rd 04 10:29 AM
Problem with .Background Query option of ODBC Query Shilps Excel Programming 0 April 19th 04 06:41 AM


All times are GMT +1. The time now is 11:31 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"