0

I'm lost, I try to fill arrays and receive a type mismatch

I'm trying to fill 4 arrays from one file

There is 500 lines in the text document each holding 4 different types of data separated by ","

The .txt file format example --->

pear, apple, grape, orange
apple, pear, orange, grape

ect...

Here is my code:

Private Sub CmdRun_Click()
Dim ticketid(500) As Variant
Dim theatreid(500) As Variant
Dim ticketamount(500) As Variant
Dim paymethod(500) As Variant

Open "C:\Users\Dylaan\Desktop\School Solution\tickets.txt" For Input As #1

Do While Not EOF(1)

Input #1, ticketid(), theatreid(), ticketamount(), paymethod()

lstticketid.AddItem ticketid()
lsttheatreid.AddItem theatreid()
lstticketamount.AddItem ticketamount()
lstmethod.AddItem paymethod()

Exit Do
Loop

Close #1

End Sub

Why?

Sigh
  • 133
  • 8

3 Answers3

4

take a look on this:

Private Sub CmdRun_Click()

    Dim ticketid(500) As Variant
    Dim theatreid(500) As Variant
    Dim ticketamount(500) As Variant
    Dim paymethod(500) As Variant

    dim ix as integer

    Open "C:\Users\Dylaan\Desktop\School Solution\tickets.txt" For Input As #1

    ix = 0
    Do While Not EOF(1)

        Input #1, ticketid(ix), theatreid(ix), ticketamount(ix), paymethod(ix)

        lstticketid.AddItem ticketid(ix)
        lsttheatreid.AddItem theatreid(ix)
        lstticketamount.AddItem ticketamount(ix)
        lstmethod.AddItem paymethod(ix)

        ix = ix + 1
    Loop

    Close #1

End Sub

And of course you should consider to use

freefile (to get a filehandle)

and also the possibility that there are MORE records than expected ...

Thomas Krojer
  • 1,018
  • 7
  • 9
0
Set fso = CreateObject("Scripting.FileSystemObject")
Set srcfile = fso.GetFile("c:\myfile.txt")
If err.number = 0 then Set TS = srcFile.OpenAsTextStream(1, 0)
Src=ts.readall
Arr1=Split(Src, vbcrlf)
For Each thing in Arr1
    Arr2=Split(thing, ",")
    For Each thing2 in Arr2
          msgbox thing2
    Next
Next

This is vbscript but will work in VB6. We are using the split command.

0

Some comments on your code:

  • You don't need to declare those arrays as variants, you can declare them as arrays of strings.
  • As the file is just 500 lines you can read it in at once.
  • Your real problem is the input command: It can't read in arrays at once.
  • The same applies to the listbox: You can't add an array at once.

With all that applied have a look at the following test project:

'1 form with:
'  1 command button   : name=Command1
'  4 listbox controls : name=List1 name=List2 name=List3 name=List4
Option Explicit

Private Sub Command1_Click()
  Dim strData As String
  strData = ReadFile("c:\temp\file.txt")
  ShowData strData
End Sub

Private Sub Form_Resize()
  Dim sngWidth As Single
  Dim sngCmdHeight As Single
  Dim sngLstWidth As Single, sngLstHeight As Single
  sngWidth = ScaleWidth
  sngCmdHeight = 315
  sngLstHeight = ScaleHeight - sngCmdHeight
  sngLstWidth = sngWidth / 4
  List1.Move 0, 0, sngLstWidth, sngLstHeight
  List2.Move sngLstWidth, 0, sngLstWidth, sngLstHeight
  List3.Move 2 * sngLstWidth, 0, sngLstWidth, sngLstHeight
  List4.Move 3 * sngLstWidth, 0, sngLstWidth, sngLstHeight
  Command1.Move 0, sngLstHeight, sngWidth, sngCmdHeight
End Sub

Private Function ReadFile(strFile As String) As String
  Dim intFile As Integer
  Dim strData As String
  intFile = FreeFile
  Open strFile For Input As #intFile
    strData = Input(LOF(intFile), #intFile)
  Close #intFile
  ReadFile = strData
End Function

Private Sub ShowData(strData As String)
  Dim lngLine As Long
  Dim strLine() As String
  Dim strPart() As String
  strLine = Split(strData, vbCrLf)
  For lngLine = 0 To UBound(strLine)
    strPart = Split(strLine(lngLine), ",")
    If UBound(strPart) = 3 Then
      List1.AddItem strPart(0)
      List2.AddItem strPart(1)
      List3.AddItem strPart(2)
      List4.AddItem strPart(3)
    Else
      'not the correct number of items
    End If
  Next lngLine
End Sub

When you click on Command1 it will read in the textfile from c:\temp\file.txt

After that it will split the data to form an array of lines, loop over all lines, split each line into part, and show the parts in the listboxes, if there are exactly 4 parts on a line.

Hrqls
  • 2,944
  • 4
  • 34
  • 54