-
April 16th, 2013, 03:39 PM
#1
[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
-
April 16th, 2013, 07:30 PM
#2
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
-
April 16th, 2013, 07:50 PM
#3
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
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.
-
April 17th, 2013, 07:48 AM
#4
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
-
April 17th, 2013, 03:46 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|