Hi, I'm Arcon.

I have a problem when saving records, it saves a record and when it saves another it overlaps the first record
when reading records with its id it does not read it to me correctly

thank you

Recommended Answers

All 27 Replies

Well, the immediate problem is you are referencing Text2 in the loop for every record you're trying to save:

MyRecord.Name = "MyName" & "  " & Text2.Text 

The other problem I see (or one that will cause you problems later) is you're using a ListBox and combining record properties into one item to simulate columns:

List1.AddItem myRecord.ID & "  " & myRecord.Name

Use a ListView control set to reportview and separate the ID and Name into listview columns:

    Dim itmx As ListItem
    Set itmx = ListView1.ListItems.Add()
    itmx.Text = Text1.Text
    itmx.SubItems(1) = Text2.Text

There's a long post of similar issues here: Click Here

thanks for answering

but i need it with list1

thank you

thanks for answering

but i need it with list1

thank you

Okay, but you still have problems with your loop in Command1_Click

    For x = 0 To List1.ListCount - 1
        myRecord.ID = Val(Text1.Text) ' Define ID.
        myRecord.Name = "MyName" & "  " & Text2.Text    ' Create a string.' Write record to file.
        Put #1, x + 1, myRecord       ' Show the record into the listbox
    Next x

You're using the List1.ListCount for the loop but assigning each record to Text1 and Text2

a possible solution as it would be to be able to save various data and read?
thank you

Try this.

ok it saves me well now the problem is in making the new button it doesn't add the id counter it stays at one.

thank you

The problem is that I make the introduction in the id=1
and I add several items in the list1 and I save and then in the new button it adds the items of the list1 id=5
I would have to go out id:2

id:1

list1

1 a a
1 b b
1 c c

save button

new button

id=5

thank you

Try this. I coded the Click event for the list, which populates your text boxes. The New button sets the ID to one greater than the List box contents. Why are you reading the file from the end to the beginning?

thanks for answering

the problem is that the id is adding the items of list1
and the id has to add one by one

id:1

list1

1 a a
1 b b
1 c c

save button

new button

id=4

I would have to go out id:2

thank you

I guess I don't understand what you're trying to do. Are you wanting records to be 1 aa, 1 bb, 1 cc, 2 aa, 2 bb, 2 cc?

yes but with different data
1 aa, 1 bb, 1 cc, 2 rr, 2 ff, 3 yy, 3 pp, 3 jj

So 3 records of each number but different data? Sounds like a case for modular arithmetic e.g If X Mod 3 = 0 then...

You didn't say why you're reading the file from the end to the beginning?

it was just a test to read

 List1.Clear
    Dim myRecord As empRecord
    Dim Position, totalRecords, bytesFile, bytesRecord ' Open sample file for random access.
    Open App.Path & "\TESTFILE.txt" For Random As #1 Len = Len(myRecord) 'find out how many records are there in file #1
        bytesFile = LOF(1)
        bytesRecord = Len(myRecord)
        totalRecords = bytesFile / bytesRecord ' read data (randomly)from last to first rec.
        For Position = 1 To totalRecords ' Read the sample file using the Get.
            Get #1, Position, myRecord  ' Read the record' Show the record into the listbox
            List1.AddItem myRecord.ID & " " & myRecord.Name
        Next '
    Close #1    ' Close the active file

thank you

a question the sep variable how is the variable declared?

The INSTR function returns a variant, so the return value can be declared as an integer or long.

Sep variable as declared

dim Sep as ?
thank you

Dim Sep as Long
-or-
Dim Sep as Integer

Either one will work. There's some debate as to which you should use, but either will work.

when I enter a record everything is fine, but
when I enter another record it puts it in the first record

It works right for me. I changed a few things and made comments in the file.

I can't make two id entries
id=1
name=a

id=1

name=b

he save it well
but when i enter the id=2

id=2
name=q

id=2
name=w

saves it but is overwritten in record id=1

It works for me. See attached.

To read the id=1, how would it be in the command3 button to read?
thank you

I mean this

Dim n As Integer
    Dim v As Integer
    List1.Clear
    n = Val(InputBox("Enter Staff ID:"))
    Open App.Path & "\TESTFILE.txt" For Random As #1 Len = Len(myRecord)
    For v = 1 To n
    Get #1, v, myRecord
    If v = n Then
    List1.AddItem myRecord.ID & " " & myRecord.Name
    End If
    Next v
    Close #1

but it doesn't work
thank you

Sorry, I've been out of town for the week and will be gone this weekend, so I can't be of much help. I see you changed the Text Box to an InputBox. I'll see how it behaves when I get back.

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.