Reading ProjectList text file(contains name of the projects) n display those lines into combobox
my above code ....it is reading only first line of text file
help......

Private Sub Form_Load()

Open "c:\ProjectList.txt" For Random As #1 Len = Len(ReadData(1))

For intLoopIndex = 1 To LOF(1) / Len(ReadData(1))
    Get #1, , ReadData(intLoopIndex)
    cmbRead.AddItem (ReadData(intLoopIndex).NameofProject)   
Next intLoopIndex


Close #1


End Sub

Dani AI

Generated

Using Random mode on a plain text list is usually the root cause here: Random is meant for fixed-size records (a user-defined type with fixed-length strings), not ordinary line-delimited text. ’s suggestion to read the file sequentially is the right direction. Below is a compact, alternative approach that reads a plain text file line-by-line via the FileSystemObject; it avoids record-length math and works reliably for one-project-per-line text files.

Dim fso As Object
Dim ts  As Object
Dim sLine As String

Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile("C:\ProjectList.txt", 1, False)   ' 1 = ForReading
Do While Not ts.AtEndOfStream
    sLine = Trim$(ts.ReadLine)
    If Len(sLine) > 0 Then cmbRead.AddItem sLine
Loop
ts.Close
Set ts = Nothing
Set fso = Nothing

Troubleshooting checklist (common gaps seen in similar threads): confirm the exact file path and that the file actually contains multiple lines; check encoding — VB6 expects ANSI by default, so a UTF-8/Unicode file with a BOM can produce odd first-line behavior; confirm the form’s ComboBox name is cmbRead and that items aren’t being overwritten or cleared elsewhere; and ensure the file wasn’t created with fixed-size binary records. If Random mode must be used, the UDT must use fixed-length strings (for example String * N) so Len() yields the correct record size and LOF/recordSize math is valid.

This complements ’s sequential approach and addresses the likely mismatches in record format that ’s Random-based code would encounter. As noted, showing the UDT declaration or the way the file was written will quickly determine whether Random access is appropriate.

Recommended Answers

All 3 Replies

To begin with, this is not a code snippet! It is a question!

Option Explicit
'...
Private Sub ReadFileUpdateCombo()
Dim FName As String, FNumb As Integer
Dim LineContents As String

FName = "PathToFileandName"
FNumb = FreeFile

Open FName For Input As #FNumb
Do While Not EOF(FNumb)
  Line Input #FNumb, LineContents
  Combo1.AddItem LineContents
Loop

Close #FNumb
End Sub

Good Luck

To begin with, this is not a code snippet! It is a question!

snipped

Did you just hand the answer to them? After over 1000 posts you should know better than that. We help here. We are not a coding service.

Walt, search my handle and Open and you will see more often than not my suggest is to read in help about the following

FreeFile Function
Open Statement
Input Function or Line Input Function
Print Statement 'for writing out
Close Statement

But some days when I'm feeling charitable or when the OP has put forth the effort like this one has, you will see an answer like the above...


Have a nice day

commented: I can live with that :o) +9
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.