CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2013
    Posts
    11

    [RESOLVED] Visual basic 6 - Next without for

    Hey, this is my first post. Currently completing A2 ICT and we have a brief to code a program to allow a school worker the ability to monitor the loaning of equipment. Got an array so i can add data etc...however i need to be able to add/delete. Add works fine, the delete option keepings coming up with "next without for"?

    Heres the code,

    Code:
     Private Sub cmddelete_Click()
    Dim index As Integer
    Dim numberofrecords As Integer
    Dim recordnumbertodelete As Integer
    Dim newfilename As String
    Dim product As OneProduct
    If lstdisplayfile.Text <> "" Then
    recordnumbertodelete = lstdisplayfile.ListIndex + 1
    End If
    newfilename = App.Path & "\products.text"
    Open Filename For Random As #1 Len = Len(product)
    Open newfilename For Random As #2 Len = Len(product)
    numberofrecords = LOF(1) / Len(product)
    
        Get #1, , product
        If index <> recordnumbetodelete Then
        
        Put #2, , product
        End If
        
    Next index
    
    Close #1
    Close #2
    Kill products
    Name newfilename As products
    Call displayfile
    Else
    MsgBox ("You must select a product first")
    End If
    
    
    End Sub
    Cheers,

    Jordie

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Visual basic 6 - Next without for

    That looks more like QB code. Here's a sample that's been posted a few times...
    Code:
    Option Explicit
    
    ' Skin a Cat!
    
    Private Sub Form_Load()
      Dim x As Integer, st As String, y As Integer
      Dim ff As Integer
      Dim strBuff As String
      Dim str() As String
      ff = FreeFile
    On Error GoTo trapit
      Open App.Path & "\to do3.txt" For Input As #ff
        strBuff = Input(LOF(ff), ff)
      Close #ff
      ' ----------------- two ways to skin a cat --------------
      MsgBox "Lines = " & Len(strBuff) - Len(Replace(strBuff, vbCrLf, "x")) + 1
      ' -------------------------------------------------------
      str() = Split(strBuff, vbCrLf)
      MsgBox "There are " & UBound(str) + 1 & " lines in the file"
      Dim words() As String
      For x = 0 To UBound(str)
        words = Split(str(x)) ' Split into WORDS
        For y = 0 To UBound(words)
          st = st & str(x) & vbCrLf & vbCrLf ' one line for each word
        Next y
      Next x
      MsgBox st
      Exit Sub
    trapit:
      If Err.Number = 53 Then
       MsgBox ("Didn't work")
      End If
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Visual basic 6 - Next without for

    Just like the error says there is a Next without the For statement which is invalid.

    this line
    Code:
    Next index
    Implies that somewhere before it is a line like
    Code:
    For Index=1 to numberofrecords ' or whatever
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Apr 2013
    Posts
    11

    Re: Visual basic 6 - Next without for

    Hi, thanks for the replies! I had indeed missed that line of code, however now the program deletes the orginal file but doesnt create the new one for the data to be put into? It should copy all the rcords bar the one im deleting and copy them, but it isnt?

    Code:
     Private Sub cmddelete_Click()
    Dim index As Integer
    Dim NumberOfRecords As Integer
    Dim recordnumbertodelete As Integer
    Dim newfilename As String
    Dim product As OneProduct
    If lstdisplayfile.Text <> "" Then
    recordnumbertodelete = lstdisplayfile.ListIndex + 1
    newfilename = App.Path & "\products.text"
    Open Filename For Random As #1 Len = Len(product)
    Open newfilename For Random As #2 Len = Len(product)
    NumberOfRecords = LOF(1) / Len(product)
    For index = 1 To NumberOfRecords
    
        Get #1, , product
        If index <> recordnumbetodelete Then
        
        Put #2, , product
        End If
        
    Next index
    
    Close #1
    Close #2
    Kill App.Path & "\products.text"
    Name newfilename As products
    Call lstdisplayfile1
    'Else
    'MsgBox ("You must select a product first")
    End If
    
    
    End Sub
    Any help?

    Cheers

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Visual basic 6 - Next without for

    Code:
    Name newfilename As products
    You are renaming the file here to the content of the variable products which is not defined nor assigned in the current scope so you may very well be renaming the file to 0 or blank in the current directory rather than where you want it and under the name you expect. Based on the code you have I would expect that it should be

    Code:
    Name newfilename As Filename
    Always use [code][/code] tags when posting code.

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

Featured