Results 1 to 5 of 5

Thread: [RESOLVED] Input values from text file

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Resolved [RESOLVED] Input values from text file

    Hi,

    How would I achieve the some outcome here but with a text file instead of an ini file?

    This is what I have tried but the input into the program does not work.

    vb Code:
    1. Dim start As String
    2. Public Sub WData(settings As String)
    3.   ff = FreeFile
    4.   start = frmMain.chkStartup.Value
    5.   cap = frmMain.chkStartup.Caption
    6.   Open settings For Output As #ff
    7.   Print #ff, "[Load]" & vbCrLf & cap & " = " & start
    8.   Close #ff
    9. End Sub
    10.  
    11.     Public Sub RData(settings As String)
    12.        ff = FreeFile
    13.         Dim tmpString()  As String
    14.        Open settings For Input As #ff
    15.        Do Until EOF(ff)
    16.        Input #ff, entry
    17.        tmpString = Split(entry, " ")
    18.         If UBound(tmpString) = 0 Or UBound(tmpString) = 1 Then frmMain.chkStartup.Value = entry
    19.        Loop
    20.        Close #ff
    21.     End Sub

    Thanks,


    Nightwalker
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Input values from text file

    I assume you still want the format of the file to 'look' like a .ini file (?)

    If this is the case, something like this might do
    Code:
    Option Explicit
    
    Private Type Settings
        Section As String
        KeyData() As String
    End Type
    
    Private uSettings() As Settings
    
    Private Sub RData(strSettingsFile As String)
    Dim intFile As Integer
    Dim intSet As Integer
    Dim intKey As Integer
    Dim intI As Integer
    Dim intJ As Integer
    Dim intK As Integer
    Dim intPos As Integer
    Dim strData As String
    Dim strRecs() As String
    Dim strD() As String
    intSet = -1
    intFile = FreeFile
    Open strSettingsFile For Input As intFile
    strData = Input(LOF(intFile), intFile)
    Close intFile
    strRecs = Split(strData, vbNewLine)
    ReDim uSettings(UBound(strRecs))
    For intI = 0 To UBound(strRecs)
        If strRecs(intI) <> vbNullString Then
            If Mid$(strRecs(intI), 1, 1) = "[" Then
                If intKey > 0 Then ReDim Preserve uSettings(intSet).KeyData(intKey - 1)
                intKey = 0
                intSet = intSet + 1
                If intSet > UBound(uSettings) Then ReDim Preserve uSettings(UBound(uSettings) + 100)
                intPos = InStr(2, strRecs(intI), "]")
                uSettings(intSet).Section = Mid$(strRecs(intI), 2, intPos - 2)
                ReDim uSettings(intSet).KeyData(9)
            Else
                If intKey > UBound(uSettings(intSet).KeyData) Then ReDim Preserve uSettings(intI).KeyData(UBound(uSettings(intI).KeyData) + 100)
                uSettings(intSet).KeyData(intKey) = strRecs(intI)
                intKey = intKey + 1
            End If
        End If
    Next intI
    If intKey > 0 Then ReDim Preserve uSettings(intSet).KeyData(intKey - 1)
    ReDim Preserve uSettings(intSet)
    For intI = 0 To UBound(uSettings)
        Debug.Print "Section: "; uSettings(intI).Section
        For intJ = 0 To UBound(uSettings(intI).KeyData)
            strD = Split(uSettings(intI).KeyData(intJ), "=")
            Debug.Print "Key: '"; strD(0); "' Data: "; strD(1)
        Next intJ
    Next intI
    End Sub
    
    Private Sub Form_Load()
    Call RData("C:\Mobs.ini")
    End Sub
    The data file might look like this
    Code:
    [Section 1]
    Number= 1
    Value1= 1
    Value2= 2
    Value3= 3
    [Section 2]
    Number=2
    Value1= 10
    [Section 3]
    Number= 3
    Value5= 30
    Value20= 10
    Value44= 2.3
    and the ouptut like this
    Code:
    Section: Section 1
    Key: 'Number' Data:  1
    Key: 'Value1' Data:  1
    Key: 'Value2' Data:  2
    Key: 'Value3' Data:  3
    Section: Section 2
    Key: 'Number' Data: 2
    Key: 'Value1' Data:  10
    Section: Section 3
    Key: 'Number' Data:  3
    Key: 'Value5' Data:  30
    Key: 'Value20' Data:  10
    Key: 'Value44' Data:  2.3
    EDIT: Of course, you'll have to put some validation in to make sure that the sections end with a ']' and there's at least one key - data pair per section. (Just in case the user 'plays' with the file and messes it up !)
    Last edited by Doogle; Oct 12th, 2011 at 01:56 AM.

  3. #3

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Input values from text file

    Thanks! Although, when I run the code I receive an object required error on this line.

    vb Code:
    1. uSettings(intSet).Section = Mid$(strRecs(intI), 2, intPos - 2)

    What does that line refer to?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Input values from text file

    Wierd ! Did you try it with the Data I used ? Have you copied and pasted all the code (including that in the Declarations Section) correctly ? Do all your Sections end with a "]" ? Is there at least one Key - Data pair in each section ?

    EDIT: That line assigns the Section Name to the 'Section' value in the intSet'th element of the UDT Array. strRecs(inti) should hold something like "[Section 1]" (or if you're using your data "[Load]")
    Last edited by Doogle; Oct 12th, 2011 at 02:14 AM.

  5. #5

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Input values from text file

    Never mind! I moved this part of the code to the top of the module and it solved the problem.

    vb Code:
    1. Private uSettings() As Settings
    2.   Dim start As String
    3.      
    4.       Private Type Settings
    5.     Section As String
    6.     KeyData() As String
    7. End Type
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width