Results 1 to 3 of 3

Thread: loading

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Wink

    Hi,
    I need to be able to fill TWO comboboxes with values from a textfile. The textfile consists of 2 columns. The text file will have variable length (I'll be adding data to those two columns).

    "First", "Last"
    "Tom", "Johns"
    "Joe", "Burns"...

    I want to load first column in the first ComboBox and second column in the second ComboBox.

    Thanks
    Tomexx (VB6 Pro)

  2. #2
    Lively Member
    Join Date
    Jun 2000
    Location
    Belgium
    Posts
    77

    Smile

    You can maeke something like this :

    Code:
     Dim Str As String
     Dim FileName As String
     Dim Elem1 as string
     Dim Elem2 as string
     Dim i As Integer
     FileName = "MyFileName.Ext"
     Open FileName For Input As 1
      While not eof(1)
       Input #1, Str
       ' The SPLIT function make an array with the string. Each elem of the array is separate by the caractère "," in the string
       ' ex: Elem1=Split("One, Two, 2",",") 
       '     after the code, 
       '       Elem1(0)="One "
       '       Elem1(2)="Two "
       '       Elem1(0)="2"
       '
       ' the trim function erase blank before and after then string
       '  ex: trim("   one ") = "one"
       Elem1=Trim(split(str,",")(0))
       Elem2=Trim(split(str,",")(1))
       ' if your file contained ", you must add this line of code for delete it
       ' Elem1=mid(elem1,2,len(elem1)-3)
       ' Elem2=mid(elem2,2,len(elem2)-3)
    
       Combo1.AddItem Elem1
       Combo2.AddItem Elem2 
      Wend
     Close 1
    KWell

  3. #3
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Read the file in to a variable. Then split the string up on cariage returns, then loop for how many lines where in the text file, and load the column data into the combo boxes.

    Code:
    Private Sub Command1_Click()
        Dim bTemp() As Byte
        Dim strData As String
        Dim strLine() As String
        Dim strFields() As String
        Dim i As Integer
        
        Open "test.txt" For Binary As #1
        
        'size the array to the size of the file
        ReDim bTemp(LOF(1)) As Byte
        
        'get the data
        Get #1, , bTemp
        
        Close #1
        
        
        'convert data into a string
        strData = StrConv(bTemp, vbUnicode)
        'split data on carriage returns
        strLine = Split(strData, vbCrLf)
        
        'for the number of lines found
        For i = LBound(strLine) To UBound(strLine)
          'split on commas
          strFields = Split(strLine(i), ",")
          
          'if two fileds then
          If UBound(strFields) >= 1 Then
            Combo1.AddItem strFields(0)
            Combo2.AddItem strFields(1)
          Else
          'only one filed on this line
            Combo1.AddItem strFields(0)
          End If
        Next i
        
    End Sub
    Iain, thats with an i by the way!

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