Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7681111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:16:04+00:00 2026-05-31T18:16:04+00:00

I have never programmed in visual basic before and my boss just told me

  • 0

I have never programmed in visual basic before and my boss just told me to edit this visual basic program. This program read from read from a list of file and put the data in them(some numbers) into my boss sql database. He want me to edit this program so if the numbers in these files are out of range, the program will ignore these files. I’m trying to understand how the program does this:

Dim totalVolume1 As Integer, totalVolume2 As Integer, nLane1 As Integer, nLane2 As Integer
Dim totalVolSpeed1 As Double, totalVolSpeed2 As Double
Dim totalSpeed1 As Double, totalSpeed2 As Double, totalOccu1 As Double, totalOccu2 As Double
Dim nRunNum As Integer, NumberOfRunPerMinute As Integer, nWaitTime As Long, RetVal As Long
Dim nFileDir As Integer
Dim Sqlcommand1 As String, Sqlcommand2 As String
Dim FileSize() As Long
Dim FTP_DateTime As Date
NumberOfRunPerMinute = 3


'For nRunNum = 1 To NumberOfRunPerMinute
Do While True
    'OutputTxt "Running the " & nRunNum & " time(s) of this minute."

    nFileNum1 = FreeFile
    Open "c:\" & FileSQLRegular For Output As #nFileNum1
    nFileDir = FreeFile
    Open "c:\" & FileSQLDirection & "" For Output As #nFileDir
    OutputTxt "Start to look for files to import."
    cFileList = LookForFiles(cFileBaseFolder, "*.txt")
    OutputTxt "Finished looking for new files."
    Erase FileSize
    ReDim FileSize(LBound(cFileList) To UBound(cFileList))
    OutputTxt "Start to check file lengths."
    For i = 1 To UBound(cFileList)
        FileSize(i) = FileLen(MyDir(cDataFolder) & cFileList(i))
    Next
    OutputTxt "Done checking file lengths."


    For i = 1 To UBound(cFileList)
        Debug.Print i
        nFileNum2 = FreeFile
        OutputTxt "Start to process file '" & cFileList(i) & "'"
        Open cFileBaseFolder & cFileList(i) For Input As #nFileNum2
        FTP_DateTime = FileDateTime(cFileBaseFolder & cFileList(i))
        OutputTxt "DateTime of file '" & cFileList(i) & "' is '" & Format(FTP_DateTime, "yyyy-mm-dd hh:mm:ss") & "'"
        If Not EOF(nFileNum2) Then
            Line Input #nFileNum2, TmpString
            nDetID = Val(Right(TmpString, Len(TmpString) - 4))
            Do While Not EOF(nFileNum2)
                detData = RetrieveData(nFileNum2, nDetID)
                If Not detData.DayID = 0 Then
                    With detData
                        If .ValidattionID = 1 Then
                            OutputTxt "Retrieved 1 record!"

                            totalVolume1 = 0
                            totalVolume2 = 0
                            totalVolSpeed1 = 0
                            totalVolSpeed2 = 0
                            totalOccu1 = 0
                            totalOccu2 = 0
                            nLane1 = 0
                            nLane2 = 0
                            For j = 1 To nDetectorCount(.DetID)
                                TmpDataID = CStrN(.YearID, 4) & CStrN(.DayID, 3) & CStrN(.SecondTimeID, 5) & CStrN(.DetID, 4) & CStrN(j, 2)
                                SqlCommand = _
                                    "@""" & Format(.Date, "yyyy-mm-dd hh:mm:ss") & """,""" & .YearID & """,""" & .DayID & """,""" & .SecondTimeID & _
                                    """,""" & .DetID & """,""" & j & """,""" & .Speed(j) & """,""" & .Volume(j) & _
                                    """,""" & .Occupancy(j) & """,""" & TmpDataID & """,""" & Format(FTP_DateTime, "yyyy-mm-dd hh:mm:ss") & """;"
                                Print #nFileNum1, SqlCommand

                                If .Speed(j) >= 0 And .Speed(j) <= 90 Then
                                    If DetLaneDir(.DetID, j) = 1 Then
                                        totalVolume1 = totalVolume1 + .Volume(j)
                                        totalVolSpeed1 = totalVolSpeed1 + .Volume(j) * 1# * .Speed(j)
                                        totalOccu1 = totalOccu1 + .Occupancy(j)
                                        nLane1 = nLane1 + 1
                                    ElseIf DetLaneDir(.DetID, j) = 2 Then
                                        totalVolume2 = totalVolume2 + .Volume(j)
                                        totalVolSpeed2 = totalVolSpeed2 + .Volume(j) * 1# * .Speed(j)
                                        totalOccu2 = totalOccu2 + .Occupancy(j)
                                        nLane2 = nLane2 + 1
                                    End If
                                End If
                            Next

                            If totalVolume1 = 0 Then
                                totalOccu1 = 0
                                totalSpeed1 = 240
                            Else
                                totalOccu1 = totalOccu1 / nLane1
                                totalSpeed1 = totalVolSpeed1 / totalVolume1
                            End If

                            If totalVolume2 = 0 Then
                                totalOccu2 = 0
                                totalSpeed2 = 240
                            Else
                                totalOccu2 = totalOccu2 / nLane2
                                totalSpeed2 = totalVolSpeed2 / totalVolume2
                            End If

                            TmpDataID1 = CStrN(.YearID, 4) & CStrN(.DayID, 3) & CStrN(.SecondTimeID, 5) & CStrN(.DetID, 4) & CStrN(1, 2)
                            TmpDataID2 = CStrN(.YearID, 4) & CStrN(.DayID, 3) & CStrN(.SecondTimeID, 5) & CStrN(.DetID, 4) & CStrN(2, 2)
                            Sqlcommand1 = "@""" & Format(.Date, "yyyy-mm-dd hh:mm:ss") & """,""" & .YearID & """,""" & .DayID & """,""" & .SecondTimeID & _
                                    """,""" & .DetID & """,""" & 1 & """,""" & totalSpeed1 & """,""" & totalVolume1 & _
                                    """,""" & totalOccu1 & """,""" & TmpDataID1 & """,""d01"";"
                            Sqlcommand2 = "@""" & Format(.Date, "yyyy-mm-dd hh:mm:ss") & """,""" & .YearID & """,""" & .DayID & """,""" & .SecondTimeID & _
                                    """,""" & .DetID & """,""" & 2 & """,""" & totalSpeed2 & """,""" & totalVolume2 & _
                                    """,""" & totalOccu2 & """,""" & TmpDataID2 & """,""d01"";"
                            Print #nFileDir, Sqlcommand1
                            Print #nFileDir, Sqlcommand2
                        Else
                            'MsgBox "Not validated!"
                            Debug.Print "Not validated!"
                        End If
                    End With
                End If
            Loop
        End If
        Close (nFileNum2)
    Next
    Close (nFileNum1)
    Close (nFileDir)
    If FileLen("c:\" & FileSQLRegular & "") > 0 Then
        OutputTxt "Importing data to database real_time_data..."
        cnnMain_New.Execute ("LOAD DATA LOCAL INFILE 'c:\\" & FileSQLRegular & "' INTO TABLE real_time_data FIELDS TERMINATED BY ',' ENCLOSED BY '""' LINES STARTING BY '@' TERMINATED BY ';' (DATE_TIME, YEAR_ID, DAY_ID, SECOND_ID, DET_ID, LANE_ID, SPEED, VOLUME, OCCUPANCY, DATA_ID, PROCESSED_DATE_TIME, DATA_TYPE)")
        OutputTxt "Done importing data to database..."
        OutputTxt "Importing data to database real_time_data_two_day..."
        cnnMain_New.Execute ("LOAD DATA LOCAL INFILE 'c:\\" & FileSQLRegular & "' INTO TABLE real_time_data_two_day FIELDS TERMINATED BY ',' ENCLOSED BY '""' LINES STARTING BY '@' TERMINATED BY ';' (DATE_TIME, YEAR_ID, DAY_ID, SECOND_ID, DET_ID, LANE_ID, SPEED, VOLUME, OCCUPANCY, DATA_ID, PROCESSED_DATE_TIME, DATA_TYPE)")
        OutputTxt "Done importing data to database..."
    Else
        OutputTxt "No file found to process!"
    End If
    If FileLen("c:\" & FileSQLDirection & "") > 0 Then
        OutputTxt "Importing data to database real_time_data_direction..."
        cnnMain_New.Execute ("LOAD DATA LOCAL INFILE 'c:\\" & FileSQLDirection & "' INTO TABLE real_time_data_direction FIELDS TERMINATED BY ',' ENCLOSED BY '""' LINES STARTING BY '@' TERMINATED BY ';' (DATE_TIME, YEAR_ID, DAY_ID, SECOND_ID, DET_ID, Dir_ID, SPEED, VOLUME, OCCUPANCY, DATA_ID)")
        OutputTxt "Done importing data to database..."
        OutputTxt "Importing data to database real_time_data_direction_two_day..."
        cnnMain_New.Execute ("LOAD DATA LOCAL INFILE 'c:\\" & FileSQLDirection & "' INTO TABLE real_time_data_direction_two_day FIELDS TERMINATED BY ',' ENCLOSED BY '""' LINES STARTING BY '@' TERMINATED BY ';' (DATE_TIME, YEAR_ID, DAY_ID, SECOND_ID, DET_ID, Dir_ID, SPEED, VOLUME, OCCUPANCY, DATA_ID)")
        OutputTxt "Done importing data to database..."
    Else
        OutputTxt "No file found for directional data to process!"
    End If
    'cnnMain.Close
    Kill "c:\" & FileSQLRegular & ""
    Kill "c:\" & FileSQLDirection & ""
    MoveFiles cFileList, FileSize

' 
       If nRunNum < NumberOfRunPerMinute Then
'            nWaitTime = CLng((CSng(60) / NumberOfRunPerMinute - 2) * 1000)
'            OutputTxt "Start to wait for " & nWaitTime & " miliseconds."
'            RetVal = MsgWaitObj(nWaitTime)
'            OutputTxt "Finished waiting!"
'        End If
'    'Next
            nWaitTime = 2000
            OutputTxt "Start to wait for " & nWaitTime & " miliseconds."
            RetVal = MsgWaitObj(nWaitTime)
            OutputTxt "Finished waiting!"

Here is what I understand so far: in

For i = 1 To UBound(cFileList)

it goes through all the files. In

For j = 1 To nDetectorCount(.DetID)

it goes into each file and store the data in variables such as totalvolume1, totalspeed1… In

 If totalVolume1 = 0 Then
 totalOccu1 = 0
 totalSpeed1 = 240

it does some filter. Finally, in

 Sqlcommand1 = "@""" & Format(.Date, "yyyy-mm-dd hh:mm:ss") & """,""" & .YearID & """,""" & .DayID & """,""" & .SecondTimeID & _
                                        """,""" & .DetID & """,""" & 1 & """,""" & totalSpeed1 & """,""" & totalVolume1 & _ 

it put all the variables in two strings, Sqlcommand1 and Sqlcommand2, so they can be put into sql database.

Is my understanding of this program correct so far? Does this program then use

Print #nFileDir, Sqlcommand1

Print #nFileDir, Sqlcommand2

to put those two strings into the database?

Thanks for the help.

would this fix my problem:

  'if data out of range, then they are not written to text file and database
If totalVolume1 < 50 And totalVolume1 > -1 And totalVolume1 < 50 And totalVolume1 > -1 Then
       Print #nFileDir, Sqlcommand1
       Print #nFileDir, Sqlcommand2

 End If
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:16:06+00:00Added an answer on May 31, 2026 at 6:16 pm

    No. The Print #nFileDir statements are writing the data out to what appears to be a .csv (comma separated value) file. (Note this is really bad code, even for old VB6 written by a wanna-be programmer.)

    The pertinent statement for the two Print statements you’re asking about is this one:

    Open "c:\" & FileSQLDirection & "" For Output As #nFileDir
    

    This creates a file number (#nFileDir. which is declared as an Integer) to a text file (open for writing), which is used in the rest of the code to write to using the Print #nFileDir statements. The resulting text file is then imported to the database using the cnnNew.Execute statements that reference FileSQLDirection. (Note FileSQLDirection in the Open call I show above.) NOTE: The “file number” I mention is not a Win32 file handle, and isn’t compatible with Win32 API calls or other uses that need one. See Bob Riemersma’s comments below.

    To make the change your boss is asking about, you’ll need to fix the statements before these two Print statements, so that they only execute if the data is within the boundaries your boss wants for the data. You need to check the values before you get to the assignment of the text to SqlCommand1 and SQLCommand2 and the Print statements that writes them out to the text file, so the invalid values never get to the text file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've never programmed in java or eclipse before, although I have used c# and
I have never programmed anything in php before and haven't touched html in 10
I have programmed in C, Pascal, GWBasic, TCL, Perl, Lisp, but Visual Basic is
I have to ask this question because I've never programmed except in an intro
I am trying to port a Windows program to Linux. I have never programmed
I have never programmed a gadget for Vista or Seven, but I would like
I am amateur programmer learning how to program. I have never had any computer
I have never worked with web services and rails, and obviously this is something
I have never seen a way to do this nicely, i would be interested
I have never really found the design view in Visual Studio useful when developing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.