Computing.Net > Forums > Programming > Amending a record in a .dat file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Amending a record in a .dat file

Reply to Message Icon

Name: vb6user
Date: April 16, 2009 at 15:15:31 Pacific
OS: Windows XP
CPU/Ram: Dual Core 512MB RAM+
Subcategory: General
Comment:

Using VB6

I have a list of records saved in a .dat file, which are displayed in a list box on a form called ‘frm_Existing_Student’. Using a separate form called ‘frm_Amend_Student’, I want to be able to make changes to any of the fields within a record.

I am currently able to select the record which I want to edit, by clicking on it in the list box in ‘frm_Existing_Student’, and clicking an ‘amend’ button; this opens the second form, which displays all the fields of the record in separate textboxes. (One for student name, address, postcode and so on.)

I have tried the same code as I used on a completely different form, which allows me to edit one or more fields of a record. However, when I tried it with the ‘frm_Amend_Student’ save button, I simply get a duplicate of the record instead of amending the existing record.

One complication, involves the fact that I have defined all my variables in a module rather than an array, as the variables need to be used on more than one form.

The following code is from the example which works; the top two Dim statements are defined in the General Declarations, and the rest of the code is in the click event of the ‘save’ button called ‘cmd_Enter’.

Dim EditEvent As Boolean
Dim en As Integer


Cmd_New.Enabled = True
If EditEvent = True Then
        With Student_Event(en)
                .Event_ID = txt_Event_ID.Text
                .Event_Name = txt_Event_Name.Text
                .Location = txt_Location.Text
                .Ticket_Price = txt_Price.Text
                .Event_Date = txt_Date.Text
                .Start_Time = txt_Time.Text
                .Number_of_Tickets = txt_No_Tickets.Text
        End With
        Call saveStudent_Event(en)
    Else
        If txt_Event_ID = "" Or txt_Location = "" Or txt_Event_Name = "" Or txt_Date = "" Or txt_Price = "" Or txt_No_Tickets = "" Or txt_Time = "" Then
           MsgBox "Please complete all fields"
           txt_Event_ID.SetFocus
        Else
           Student_EventCount = Student_EventCount + 1
            With Student_Event(Student_EventCount)
                .Event_ID = txt_Event_ID.Text
                .Event_Name = txt_Event_Name.Text
                .Location = txt_Location.Text
                .Ticket_Price = txt_Price.Text
                .Event_Date = txt_Date.Text
                .Start_Time = txt_Time.Text
                .Number_of_Tickets = txt_No_Tickets.Text
        End With
            Call saveStudent_Event(Student_EventCount)
        End If
End If
End Sub

This section of code is from the save button on the form which does not work; it simply creates a duplicate of the record.

Dim EditStudent As Boolean
Dim st As Integer

Private Sub cmd_Save_Click()

If EditStudent = True Then
        With Student(st)
            
                .Student_ID = txt_ID.Text
                .Student_FirstName = txt_Name
                .Student_Surname = txt_Surname
                .Address_1 = txt_Address1
                .Address_2 = txt_Address2
                .Town = txt_Town
                .Post_Code = txt_Post
                .Telephone = txt_Phone
                .Mobile = txt_Mobile
        End With
        Call saveStudent(st)

        Else
StudentCount = StudentCount + 1
            With Student(StudentCount)
                .Student_ID = txt_ID.Text
                .Student_FirstName = txt_Name
                .Student_Surname = txt_Surname
                .Address_1 = txt_Address1
                .Address_2 = txt_Address2
                .Town = txt_Town
                .Post_Code = txt_Post
                .Telephone = txt_Phone
                .Mobile = txt_Mobile
                
        End With
            Call saveStudent(StudentCount)
End If
End Sub

I would be very grateful for any suggestions; I have wasted hours trying to get this thing to work!

(I would also like to know how I could delete a record from the same file.)

Thanks a lot!



Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


VBS script with changing ... Batch Script Help



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Amending a record in a .dat file

decompiling a .dat file www.computing.net/answers/programming/decompiling-a-dat-file/3660.html

editing a .dat file www.computing.net/answers/programming/editing-a-dat-file/19570.html

Txt doc and Dat files using VB www.computing.net/answers/programming/txt-doc-and-dat-files-using-vb/16089.html