Results 1 to 2 of 2

Thread: Assignment due tomorrow - error

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    47

    Angry Assignment due tomorrow-error "Subscript out of range"

    Dim fileName As String

    Private Sub cmdSelect_Click()
    Dim nom As String, phoneNum As String, x As String
    Dim prompt As String, title As String, q As String
    Open App.Path & "\Avail.txt" For Input As #1
    a = "Choose from the following files:" & Chr$(10) & "" & _
    Input(LOF(1), 1) & "or enter a new directory."
    q = InputBox(a, "Choose a directory")
    Close #1
    If UCase(q) = "WORK NUMBERS" Then
    x = App.Path & "\Aphone.txt"
    ElseIf UCase(q) = "HOME NUMBERS" Then
    x = App.Path & "\Bphone.txt"
    ElseIf UCase(q) <> "WORK NUMBERS" Or _
    UCase(q) <> "HOME NUMBERS" Then
    title = "Directory Request"
    prompt = "The directory you have requested does not " & _
    "exist. Do you want this to be your new directory?"
    response = MsgBox(prompt, vbYesNo, title)
    If response = vbYes Then
    x = App.Path & "\Cphone.txt"
    Else
    prompt = "You must choose a directory"
    c = MsgBox(prompt, vbOKOnly, "Error")
    End If
    End If

    Private Function NumberOfFiles(fileName As String) As Integer
    Dim nom As String, phoneNum As String
    Dim n As Integer
    n = 0
    Open fileName For Input As #1
    Do While Not EOF(1)
    Input #1, nom, phoneNum
    n = n + 1
    Loop
    Close #1
    NumberOfFiles = n
    End Function

    Private Sub cmdView_Click()
    Dim numFiles As Integer, index As Integer
    numFiles = NumberOfFiles(fileName)
    ReDim nom(index) As String
    ReDim phoneNum(index) As String
    picDisplay.Cls
    For index = 1 To numFiles
    picDisplay.Print nom(index), phoneNum(index)
    Next index
    End Sub


    VB6 learning edition
    MomOf3CollegeStudentTooMuchToDoNeverEnoughTime

    Using VB6 Working Model Edition

  2. #2
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    I think your error is due to the ReDim command in the cmdView_Click() event.

    AT this point you does not assign a value for the index,
    So, the program will automatic assign a zero "0" for it. Hence, when you Redim the nom() array and phoneNum() array will only have maximum of 1 element inside.

    It can be nom(0) or nom(1), all this depand on your Option Base decalration.
    option Base 0 will start will nom(0) and Option Base 1 will start with nom(1)

    If the numFiles return from the function NumberOfFiles is larger than 0, then you will hit the
    Subscript out of range error.

    Therefore, just get the Maximum array size for both nom and phoneNum before you ReDim it.

    Code:
    Private Sub cmdView_Click() 
    Dim numFiles As Integer, index As Integer 
    numFiles = NumberOfFiles(fileName) 
    'Assign a value for the index here first.
    ReDim nom(index) As String 
    ReDim phoneNum(index) As String 
    picDisplay.Cls 
    For index = 1 To numFiles 
    picDisplay.Print nom(index), phoneNum(index) 
    Next index 
    End Sub
    [Edited by Chris on 06-19-2000 at 01:53 AM]

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