Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > what is wrong with this code


Reply
 
Thread Tools Display Modes
  #1  
Old 08-07-2012, 10:02 PM
rohitb rohitb is offline
Freshman
 
Join Date: Jun 2006
Posts: 26
Default what is wrong with this code


attached file contains entire project...
the code downloads zip file from website and extracts it... then it has to read the extracted file and output selected columns from the file to another file. its not doing that

Code:
Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" _
(ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long

Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
Alias "DeleteUrlCacheEntryA" _
(ByVal lpszUrlName As String) As Long

Private Const ERROR_SUCCESS As Long = 0
Private Const BINDF_GETNEWESTVERSION As Long = &H10
Private Const INTERNET_FLAG_RELOAD As Long = &H80000000

Private Sub Command1_Click()
On Error GoTo Err
Const q As String = "/"
Dim tmp As String, fname As String, pos As Long, fPath As String
Dim first As Date, last As Date, spath As String, d As Date
Dim oUnZip As CGUnzipFiles

cap = Me.Caption
If Dir(App.Path & "\NSE EQUITY\", vbDirectory) = "" Then
MkDir App.Path & "\NSE EQUITY\"
End If
spath = App.Path & "\NSE EQUITY\" ' folder to save files : note trailing \
first = MonthView1
last = MonthView2
'http://www.nseindia.com/content/historical/EQUITIES/2011/JAN/cm03JAN2011bhav.csv.zip
strurl = "http://www.nseindia.com/content/historical/EQUITIES/"
For d = first To last
sSourceURL = strurl & Year(d) & q & UCase(Format(d, "mmm")) & q & "cm" & UCase(Format(d, "ddmmmyyyy")) & "bhav.csv.zip"
Debug.Print sSourceURL
fname = Format(d, "dd-mm-yyyy") & ".zip"
slocalfile = spath & fname
Me.Caption = "Downloading " & fname
Call DeleteUrlCacheEntry(sSourceURL)
If URLDownloadToFile(0&, sSourceURL, slocalfile, BINDF_GETNEWESTVERSION, 0&) = S_OK Then
    
    Set oUnZip = New CGUnzipFiles
    
    With oUnZip
        .ZipFileName = spath & fname
        .ExtractDir = spath
        .HonorDirectories = False
        If .Unzip <> 0 Then
            MsgBox .GetLastMessage
        End If
    End With
  
Set oUnZip = Nothing
   ' MsgBox fName & "Extracted Successfully to " & .ExtractDir
    FileCopy spath & "cm" & UCase(Format(d, "ddmmmyyyy")) & "bhav.csv", spath & Format(d, "dd-mm-yyyy") & ".csv"
    FileName = spath & Format(d, "dd-mm-yyyy") & ".csv"
    Kill slocalfile
    Kill spath & "cm" & UCase(Format(d, "ddmmmyyyy")) & "bhav.csv"
        Debug.Print FileName


    '  sLastSymbol = "zzz"  'Set to something Symbol cannot possibly be initially.
    Open App.Path & "\temp.csv" For Output As #2
    Open App.Path & "\NSE EQUITY\" & FileName For Input As #1
    Do While Not EOF(1)
        Line Input #1, sLineIn
        sCols = Split(sLineIn, ",")
        sSymbol = sCols(0)
        sDate = sCols(10)
        sOpen = sCols(2)
        sHigh = sCols(3)
        sLow = sCols(4)
        sClose = sCols(5)
        sVolume = sCols(8)
        Debug.Print sLineIn
        sLineOut = sSymbol & "," & sDate & "," & sOpen & "," & sHigh & "," & sLow & "," & sClose & "," & sVolume
        Print #2, sLineOut
    Loop
    Close #1
    Close #2

    ' Delete file 1 and rename file 2 as the original file 1 name.
    ' // Delete the original file
    'Kill slocalfile
    ' // Rename the temp file to the original name
   ' Name App.Path & "\temp.csv" As slocalfile
End If
Next
Me.Caption = cap

'YOU CAN TAKE THIS BELOW OUT IF U DONT WANT IT
MsgBox "Saved to " & spath, vbInformation + vbOKOnly, "Success!"
Exit Sub
Err: MsgBox "Error", vbCritical + vbOKOnly, "Market Was Closed"
End Sub
could someone guide me
Attached Files
File Type: zip nse downloader.zip (83.8 KB, 2 views)
Reply With Quote
  #2  
Old 08-08-2012, 10:52 AM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,722
Default

I would suggest putting a breakpoint on one of the lines of code and then step through the process so you can see at each line if the code is doing what you expect.
VB has such easy and thorough debugging capability it makes it about the easiest language in the world to debug.
Debugging Tutorial in case you're not familiar with legacy VB debugging capabilities.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->