Link to home
Start Free TrialLog in
Avatar of Samooramad
Samooramad

asked on

VB and crystal reports

Hi,

I'm very very new to VB and need someone to please explain this code.
Thanks


'Const Record_number = 5000+
Public cn As Connection
Public mySystem
Public ConnectString, login, myDataBase
Public cntString, fName, secFname, seqFname, myBuf, logfname, reccnt
Public printername


'Public Function GetConfigSys(inParm)
'    On Error GoTo getErr
'    fni = "c:\services\general.cnf"
'    Open fni For Input As #1
'    Line Input #1, buf
'    While (Not EOF(1)) And (Left(buf, Len(inParm)) <> inParm)
'        Line Input #1, buf
'    Wend
'    If EOF(1) Then
'        GetConfigSys = "-1"
'        Else
'        GetConfigSys = Mid(buf, Len(inParm) + 1, 999)
'    End If
'    Close #1
'    Exit Function
'getErr:
'        GetConfigSys = "-1"
'End Function
Private Sub Main()
   
    myBuf = LoadFile(Command())
    constr = GetPartFromReq(myBuf, "constring" & vbTab, vbCrLf)
    seqFname = GetPartFromReq(myBuf, "lastseqfn" & vbTab, vbCrLf)
    logfname = GetPartFromReq(myBuf, "logfile" & vbTab, vbCrLf)
    stRep = GetPartFromReq(myBuf, "repname" & vbTab, vbCrLf)
    Dest = GetPartFromReq(myBuf, "Printer" & vbTab, vbCrLf)
    printername = GetPartFromReq(myBuf, "Printername" & vbTab, vbCrLf)
   
    Open logfname For Append As #2
    Print #2, Format(Now(), "dd/mm/yyyy hh:mm:ss") + UCase(stRep) + " report : Started "
    Print #2, "Destination " + Dest
    On Error GoTo errLogin
    cntr = cntr + 1
    Set cn = New Connection
    cn.CursorLocation = adUseClient
    'ConnectString = LoadFile("c:\services\SARIE\" & "repuser.sec")
    ConnectString = GetPartFromReq(myBuf, "constring" & vbTab, vbCrLf)
    'Record_number = LoadFile("c:\services\SARIE\" & "LastSeq.Seq")
    Record_number = LoadFile(seqFname)
    cn.Open ConnectString
    login = True
   
    Dim rr As Recordset
    Set rr = New Recordset
    tsql = "select business_date ,path_03 ,path_04  from config where company_code = '0000000000' "
    rr.Open tsql, cn, adOpenForwardOnly, adLockReadOnly
    repSave = rr.Fields(1).Value
    repPath = rr.Fields(2).Value
    sDATE = rr.Fields(0).Value
    'commented and changed by shaik ismail to change the date format to yyyymmdd
    'sDate = Right(sDate, 2) & Mid(sDate, 5, 2) & Left(sDate, 4)
    If UCase(stRep) = "TRANSARI" Then
       sDATE = Right(sDATE, 2) & Mid(sDATE, 5, 2) & Left(sDATE, 4)
    End If
       
    rr.Close
    'stRep = "TRANSARI"
'-----------------------------------
    scr = LoadFile(repPath & stRep & ".QUR")
    scs = LoadFile(repPath & stRep & ".QUS")
    scr = Replace(scr, "[Date]", sDATE)
    scr = Replace(scr, "[Record_Number]", Record_number)
    scs = Replace(scs, "[Date]", sDATE)
    scs = Replace(scs, "[Record_Number]", Record_number)
    'rr.Open Replace(scr, "SELECT", "SELECT RECORD_NUMBER , "), cn, adOpenForwardOnly, adLockReadOnly
    rr.Open scs, cn, adOpenForwardOnly, adLockReadOnly
   
    If rr.RecordCount <= 0 Then
        rr.Close
        Print #2, stRep + " No Entry / No Report"
        Print #2, Format(Now(), "dd/mm/yyyy hh:mm:ss") + UCase(stRep); " report : Ended successfully."
        Print #2, "################################################"
        Close #2
        MsgBox "No Entry / No Report "
        Exit Sub
    End If
    rr.MoveLast
    last_rec = rr.Fields(0).Value
   
    Open seqFname For Output As #1
    Print #1, Format(last_rec, "000000")
    Close #1
   
    Print #2, "Records to be printed : " + Str(rr.RecordCount)
    Print #2, "Last printed sequence : " + Str(last_rec)
       
    rr.Close
'---------------------------------------
    'Dest = LoadFile("c:\services\SARIE\DEST.cnf")
    GenReport repPath & stRep, Dest, scr, repSave & stRep
    If Not IsNull(Trim(printername)) And printername <> "-1" Then
       Print #2, "Printer name : " + printername
    Else
       Print #2, "Printer name : Default printer"
    End If
       
    Print #2, Format(Now(), "dd/mm/yyyy hh:mm:ss") + UCase(stRep); " report : Ended successfully."
    Print #2, "################################################"
    Close #2
    MsgBox "Report Has been generated "
    Unload frmSARI
    Exit Sub
errLogin:
    Print #2, stRep + " report cannot be generated due to the below reason"
    Print #2, "Error" + Err.Description
    Print #2, " Contact the administrator ... "
    Print #2, Format(Now(), "dd/mm/yyyy hh:mm:ss") + UCase(stRep); " report : Ended with errors."
    Print #2, "################################################"
    Close #2
    MsgBox " Contact the administrator ... "
End Sub


Public Sub GenReport(inRep, inDest, inFormula, sSave)  '

On Error GoTo genErr
    'repdir = "d:\b2b\rpt\def\"
    'repDown = "d:\b2b\rpt\Reports" & inRep
    dst = crptToWindow
    Select Case inDest
        Case "File"
            dst = crptToFile
            frmMain.repGen.PrintFileType = crptRTF
            frmMain.repGen.PrintFileName = sSave & ".rtf"
        Case "Printer"
            dst = crptToPrinter
        Case "Window"
            dst = crptToWindow
    End Select

frmSARI.repGen.Connect = ConnectString
frmSARI.repGen.ReportFileName = inRep & ".rpt"
frmSARI.repGen.SQLQuery = inFormula
frmSARI.repGen.Destination = dst

If Not IsNull(Trim(printername)) And printername <> "-1" Then
   frmSARI.repGen.printername = printername
End If
frmSARI.repGen.Action = 1

Exit Sub
genErr:
MsgBox "[" & Err.Number & "]" & Err.Description
End Sub

Public Function LoadFile(FilePath)
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Set objFileSys = CreateObject("Scripting.FileSystemObject")
    Set fs = objFileSys.GetFile(FilePath)
    Set filestream = objFileSys.OpenTextFile(FilePath, ForReading)
    LoadFile = filestream.read(fs.Size)
    filestream.Close
    Set filestream = Nothing
    Set fs = Nothing
    Set objFileSys = Nothing
End Function

Function GetPartFromReq(sDocument, sToken, eToken)
        Dim pos1, pos2
        Dim result
        result = ""
         
         
        pos1 = InStr(sDocument, sToken)
        pos2 = InStr(pos1 + 1, sDocument, eToken)
       
        If pos1 = 0 Or pos2 = 0 Then
            GetPartFromReq = -1
            Exit Function
        End If
        If pos1 > 0 Then
            If Len(eToken) <> 0 Then
                result = Mid(sDocument, pos1 + Len(sToken), pos2 - (pos1 + Len(sToken)))
            Else
                result = Mid(sDocument, pos1 + Len(sToken))
            End If
        End If
      GetPartFromReq = result
End Function

Avatar of Mike McCracken
Mike McCracken

Is there a problem with the code or do you just need an explanation.

There is a fair amount of code doing several things.  Is there a particular area or just the code ingeneral.

mlmcc
I think it would be better if you split this in to say 4 or 5 questions and award each question with say 100 points that way it will be easier because you can split it by each function and such like :) then that way you get all of the code explained for more or less the same amount of points and experts would be more willing to do it that way, unless there is someone going through all of it now commenting it
Avatar of Samooramad

ASKER

just need a main Idea..I know what its doing but just not in detail...so say a few comments on each part..then if I need more details I will ask new questions..ok?
mlmcc,
no tere is no problem with the code
I mean could someone just say which function is doing what...
please :)
ASKER CERTIFIED SOLUTION
Avatar of Shane Russell
Shane Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the grade and points !! I wasnt expecting that lol. If you would like to post parts you dont understand of the code then go from there ?