When I knocked on the student information management system to modify the class information form today, when I knocked on the code for deleting the record, I suddenly understood the concept of "function block" very deeply.
The following code is used to explain the problem:
If str2 $ = vbokthen
MRC. movenext: Find the next entry to be displayed after deletion.
If MRC. EOF then
MRC. movefirst
Mybookmark = MRC. bookmark
MRC. movelast '??
If MRC. recordcount = 1 then
MRC. Delete
Unload me
Exit sub
Else
MRC. Delete
MRC. Bookmark = mybookmark
Call viewdata 'display deleted records
End if
Else
Mybookmark = MRC. bookmark
MRC. moveprevious 'record set pointer, pointing to the record to be deleted
MRC. Bookmark = mybookmark' record set pointer pointing to the next deleted
Call viewdata: The next deleted record is displayed.
End if
Else
MRC. Bookmark = mybookmarkf
Call viewdata
End if
This is the code for deleting records. What does this code mean?
'Bookmark indicates the location of the current DBGrid. The function here is to record the location of the current DBGrid when the point is deleted,
'If you are sure you want to delete the file, check whether it is the last one,
'If yes, return to the first line, delete, and leave the location in the first line,
'If it is not the last one, the data will be deleted and the bookmark will be in the current position;
'If the deletion is not performed, the bookmark remains unchanged at the current position.
So we can certainly understand it, but how can we understand the code ?? Now let's take a look.
This code has three more if statements, that is, three functional blocks.
First, let's look at one of the most:
If MRC. recordcount = 1 then' this code records only one special case
MRC. Delete
Unload me
Exit sub
End if
Let's look at the second one:
If MRC. EOF then 'delete record at the last position
MRC. movefirst
Mybookmark = MRC. bookmark
MRC. movellast
MRC. Delete
MRC. Bookmark = mybookmark
Call viewdata
Else' the deleted record is not in the last one
Mybookmark = MRC. bookmark
MRC. moveprevious
MRC. Delete
MRC. Bookmark = mybookmark
Call viewdata
End if
It is much easier to look at the peripheral situations.
If str2 $ = vbokthen
MRC. movenext
.......
Else
MRC. Bookmark = mybookmarkf
Call viewdata
End if
I think the main function of the function block is ① To connect the code of this function after reading or writing it at one time, which is very helpful for understanding and will not look at the code of this function for a while, later I will look at the code section of that function, leading to confusion in the Code understanding.
② When writing code, no sentence will be lost.