Results 1 to 4 of 4

Thread: Deleting a Record

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    10

    Deleting a Record

    Hi. I'm struggling to work out how to delete a record from a simple database I've been working on. The code is as follows:
    Code:
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
    If MessageBox.Show("Are you sure you want to permanently" & vbNewLine & _
    "delete record number " & intCurrentRec & " ?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
    Exit Sub
    End If
    If intNumberRecs > 0 Then
    DeleteRec()
    
    intNumberRecs = intNumberRecs - 1
    
    If intNumberRecs = 0 Then
    NewBoxes()
    
    Else
    If intCurrentRec = intNumberRecs + 1 Then
    intCurrentRec = intNumberRecs
    
    End If
    GetRec(intCurrentRec)
    
    DisplayRec()
    
    End If
    End If
    End Sub
    Private Sub DeleteRec()
    Dim intFileNum1 As Int16 = FreeFile()
    FileOpen(intFileNum1, strCustFName, OpenMode.Random, OpenAccess.ReadWrite, , intRecLength)
    
    Dim intFileNum2 As Int16 = FreeFile()
    FileOpen(intFileNum2,
    
    "TEMP.DAT", OpenMode.Random, OpenAccess.ReadWrite, , intRecLength)
    Dim intRecord As Int16
    For intRecord = 1 To intNumberRecs
    If intRecord <> intCurrentRec Then
    FileGet(intFileNum2, aCustomer, )
    
    End If
    Next
    FileClose(intFileNum1)
    
    FileClose(intFileNum2)
    
    Kill(strCustFName)
    
    Rename(
    
    "TEMP.DAT", strCustFName)
    End Sub
    I'd appreciate it if anyone could help me out.

    Thanks

    P.S. The NewBoxes() sub just deletes the text inside the text boxes. intNumberRecs is the number of records and intCurrentRec is the current record, both are int16

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Deleting a Record

    Not so much a database as a text file, by the looks of it, which suggests a different solution. However, the key question is this: How many records are there in the file?

    If the number is small, any program that works with the records would be well advised to read the whole thing into memory at the start (search for ReadAllLines to get some example code). This would give you an array with one slot for each string. Deleting from an array is considerably easier than deleting from a file (deleting from a List (of T) is even easier). Then you would just write the file again when you were done with it.

    You are doing something pretty similar to that already, except that you are making a temporary file along the way, then swapping them around. That would also work, but might not be as fast. However, if your files have large numbers of records, then doing it the way you are currently doing it might make more sense.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    10

    Re: Deleting a Record

    Yeah. It's meant to be a large database which updates very regularly. I've gotten everything else working, just not this, and I don't really want to rewire the whole of the code.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Deleting a Record

    First up, if you are going to work with files in VB.NET then I suggest that you stop using that outdated VB6 mode of file I/O and start using VB.NET the way it was intended to be used. To read a structured text file you could use a StreamReader but even better would be a TextFieldParser. To write a text file you should use a StreamWriter.

    That said, if this is "meant to be a large database which updates very regularly" then a text file is a very bad option. It would be far better to use an actual database, e.g. Access, SQL Server CE or SQL Server Express. That will give you far better performance and be much easier to work with than a text file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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