The best advise I can give to you is to separate the low level activities from the higher level.
You can better use local vars than the awful construction of strings and panel control updates.
Another problem is looking only at the status of an IO operation and ignoring the wealth of information that comes from the error cluster.
remove the while from open and close and see what happens.
and always display the error status in a cluster.
greetings from the Netherlands