vb - read input from text file

Posted on 2006-05-03
Last Modified: 2010-05-01
I use vb 6.0 to run a sql to which I pass parameters like statrt date and end date from
vb to sql.. and then print results to a file.
Private Function Build_Query(sFileName As String,  customer As String, sFolderLoc As String)

    Dim sInput As String
    Dim sOutput As String
    Open sFileName For Input As #1

    Line Input #1, sInput   ' Get a line of input from the file(sFileName) and store it in the sInput
                            '  string
    sOutput = sInput
    Do While Not EOF(1)
        Line Input #1, sInput

        sOutput = sOutput & Chr(10) & sInput
    sOutput = Replace(sOutput, "&BEGDATE", "'" & strBegDate & "'")
    sOutput = Replace(sOutput, "&ENDDATE", "'" & strEndDate & "'")
    sOutput = Replace(sOutput, "&customer_LIST", svcbr)
    Close #1
    Build_Query = sOutput
     Open sFolderLoc & "\List_" & Format(strEndDate, "mmm") & "_" & Format(runDate, "mm""-""dd""-""yyyy") & ".txt" For Append As #5
    Print #5, "This Report was run was for  " & customer
    Close #5
End Function

and then I make a call to the function build_query like this:
customer = "(1,7)"
    sSQL = Build_Query(SQL_Home & "\sqlFileName.sql", customer, sFolderLoc)

If you notice I am hard coding the cutomer list like this:customer = "(1,7)"
Instead , I want to create a text file(using notepad) and store the customer values there in a folder and then use
that in my code,, so that every time the user changes the customer list, I dont have to change my code, instead i can just
change the text file contents.
How do I do this? Please add changes to my code to be specific.
Question by:Sara_j_11
    LVL 44

    Accepted Solution

        Dim strCustData As String
        Open "C:\Folder\Config.txt" For Input As #1
        Input #1, strCustData
        Close #1

    Author Comment

    Thanks expert , i used a text file
    when I get to this line of code such as :
     If rstRecordSet.RecordCount > 0 in the foll. module , I get the error - operation not allowed when object is closed.
    what is that error and how do i correct it?
    Private Sub WriteOutReport1(ByVal sFileName As String)
     'remove_file (sFileName)
         If rstRecordSet.RecordCount > 0 Then 'put data into file which will be read
         Open sFileName For Output As #1
            Write #1, rstRecordSet.Fields(0).Name, rstRecordSet.Fields(1).Name, _
                            rstRecordSet.Fields(2).Name, rstRecordSet.Fields(3).Name, _
                            rstRecordSet.Fields(4).Name, rstRecordSet.Fields(5).Name, _
                            rstRecordSet.Fields(6).Name, rstRecordSet.Fields(7).Name, _
                            rstRecordSet.Fields(8).Name, rstRecordSet.Fields(9).Name

            Do While Not rstRecordSet.EOF
                        Write #1, rstRecordSet.Fields(0), rstRecordSet.Fields(1), _
                            rstRecordSet.Fields(2), rstRecordSet.Fields(3), _
                            rstRecordSet.Fields(4), rstRecordSet.Fields(5), _
                            rstRecordSet.Fields(6), rstRecordSet.Fields(7), _
                            rstRecordSet.Fields(8), rstRecordSet.Fields(9)


            Close #1

        End If
    End Sub
    LVL 44

    Expert Comment

    Award me the points since I answered the question.

    Sara_j_11 introduced an unrelated second question on 5/19.  


    This is the first time I'm seeing this due to PC problems I'm having.  Is this second problem still a problem for you or have you resolved it?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now