简体   繁体   English

Visual Basic删除算法不起作用

[英]Visual Basic Delete Algorithm Doesn't work

This is a rehash of a previous question that went unanswered. 这是对先前未解决问题的重述。

I have a Record and a file with a collection of records in. I want to delete a record from that file. 我有一个记录和一个包含记录集合的文件。我想从该文件中删除一条记录。 This algorithm should take the record to be deleted from the file, mark the record for deletion, resave the record to the file and then copy all of the records APART from the record marked for deletion into a new temp file that becomes the main file. 此算法应从文件中删除要删除的记录,将记录标记为删除,将记录重新保存到文件,然后将所有记录APART从标记为删除的记录复制到新的临时文件中,该临时文件成为主文件。

What goes wrong: for some reason the algorithm still copies the record marked for deletion except without the correct fields. 出了什么问题:由于某种原因,该算法仍会复制标记为删除的记录,除非没有正确的字段。 It turns the fields blank but still sticks it in the new file for some reason. 它将字段变为空白,但由于某些原因仍将其粘贴在新文件中。

I will single-step through the code and watch the code only run the FilePut method three times when there are four records (showing that only three of four records are copied to the new file and one is left to be deleted as expected) but when the file is loaded into a report it still contains a blank record. 我将单步执行代码,并在有四个记录的情况下观看代码仅运行FilePut方法三次(显示四个记录中只有三个记录被复制到新文件,而一个记录则按预期保留被删除),但是当该文件已加载到报告中,但仍包含空白记录。 This makes no sense and nobody has been able to solve my problem! 这没有任何意义,而且没有人能够解决我的问题!

Dim n As Integer = 1
Dim TempDir As String = CurDir() & "\Temp.dat"
RecordNumber = Trim(Val(Mid(lstResultReport.Text, 1, 3)))
FileGet(4, ResultRecord, RecordNumber)

ResultRecord.TournamentName = "DELETE"
FilePut(4, ResultRecord, RecordNumber)
FileClose(4)
FileOpen(4, ResultsFilePath, OpenMode.Random, , , Len(ResultRecord))
FileOpen(5, TempDir, OpenMode.Random, , , Len(ResultRecord))

For n = 1 To LOF(4) / Len(ResultRecord)
    FileGet(4, ResultRecord, n)
    If Trim(ResultRecord.TournamentName) <> "DELETE" Then
        FilePut(5, ResultRecord, n)
    End If
Next
FileClose(4) 
FileClose(5)


Kill(ResultsFilePath)
FileCopy(TempDir, ResultsFilePath)
Kill(TempDir)
FileOpen(4, ResultsFilePath, OpenMode.Random, , , Len(ResultRecord))

UPDATE Interestingly, the algorithm works when I go to delete the last record in the file. 更新有趣的是,当我删除文件中的最后一条记录时,该算法就起作用了。

What I mean: 我的意思是:

dim newPosition as integer ' !!!
newPosition = 1            ' !!!
For n = 1 To LOF(4) / Len(ResultRecord)
    FileGet(4, ResultRecord, n)
    If Trim(ResultRecord.TournamentName) <> "DELETE" Then
        FilePut(5, ResultRecord, newPostion)
        newPostion = newPosition + 1 ' !!!
    End If
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM