I have created several points that need to be stored in my file. However I am getting an error that states "File blah blah cannot be accessed because it is being used by another process". This said to me that the file was still open when I tried to open it again. However, I have made sure the file was closed as the following code will show. Here is the code leading up to the problem:
Code:
Sub compassrule(dnorthstart,deaststart, totaldist, sfirst, spend) erlat = main.closenorth - dblNorth 'total error in latitude erdep = main.closeeast - dblEast 'total error in departure FileClose(c) FileOpen(c,main.strfilename,cRead) lineoftext=FileRead(c) DoUntil lineoftext=EOF coord()=StrSplit(lineoftext,",") If coord(0)=main.strfirst Then dblNorth1 = coord(1) dblEast1 = coord(2) Exit EndIf lineoftext=FileRead(c) Loop FileClose(c)
For i = main.strpointno-(main.intcounter) To main.strpointno-1 FileClose(c) FileOpen(c,main.strfilename,cRead) lineoftext=FileRead(c) DoUntil lineoftext=EOF coord()=StrSplit(lineoftext,",") If coord(0)=i Then dblNorth2 = coord(1) dblEast2 = coord(2) dblNdiff = Abs(dblNorth1 - dblNorth2) dblEdiff = Abs(dblEast1 - dblEast2) dist = Sqrt(dblNdiff ^ 2 + dblEdiff ^ 2) lat = lat + (dist * erlat / totaldist) dep = dep + (dist * erdep / totaldist) dblNorth2 = dblNorth2 + lat dblEast2 = dblEast2 + dep coord(1) = dblNorth2 coord(2) = dblEast2 coord(0) = i dblnorth=dblnorth2 dbleast=dbleast2 main.strpointno=coord(0) FileClose(c) CallSub("storecoord") 'problem after this sub is called dblNorth1 = dblNorth2 dblEast1 = dblEast2 Exit EndIf lineoftext=FileRead(c) Loop FileClose(c) Next FileClose(c) End Sub
Then here is the code for the storecoord sub:
Code:
Sub storecoord() coord(0) = main.strpointno coord(1) = dblNorth coord(2) = dblEast FileClose(c) FileOpen(c,main.strfilename,cWrite,cAppend) '<font color="red"> Where error is triggered </font> FileWrite(c, coord(0) & "," & coord(1) & "," & coord(2) & "," & coord(3)) FileClose(c) End Sub
All of the numbers work leading up to the problem. I have successfully stored points using this sub with other routines. Could the error message be intimating something else? I suspect it might be bad logic within the For...Next with Do...until embedded but can't pinpoint it. I appreciate all the past support I have gotten here. Thanks.
Well guys, apparently you can't come up with a solution, either. Since my post, I have done everything I can think of, but without success. There is a work around I can use but it is really hokey and will require my explaining in the instruction manual how to do it, including exiting and returning to the program. Maybe b4ppc doesn't have the tools for this one. I have determined that a file opened for read, then closed then opened for write within the same routine can't be done without firing the error I keep getting. It doesn't make sense that it cannot be done because I have done it with other software but my skill level simply is not high enough yet with b4ppc. Hopefully one of you gurus will see the solution right away.
I must first say that I don't know what the problem is.
But it seems strange to start a code sequence with closing the file before opening it. That hints that you think that other regions of code are/may be not clsong the file when "finished with". (I don't know if the FileClose() fails if the file is not open; if so that means you are indeed leaving the file open.) Why not keep the opening and closing in the same Sub?
The Help about files mentions the "connection name" but does not say what sort of variable this is. It does not seem to need to be declared or "Dim"ed, so there is no opportunity to specify whether it is local to a module (I deduce you are using modules since you use "Main.") or public. Are you using the same connection name in other modules?
Another approach might be to use ErrorLabel(), but I'm not sure what you would do in the error handling routine!
I had also a look at your code and don't see why you get the error.
As already suggested by mjcoon, I would also remove all unnecessary FileClose(c) lines.
Another suggestion is to change the 2nd routine that way:
Thank you for your responses. I suppose it is a VB habit of mine to always close a file prior to opening just in case I did not close it from a prior routine. Mike's suggestion that maybe B4PPC doesn't handle that well is a good one and I will check that out. Klaus, your suggestion to change the connection name in the 'Storecoord' sub doesn't work because the file name (strfilename)is still the same. If I change the file name, all is OK.
Here is what I am trying to do:
I have a text file of values, separated with commas (strfilename) that I open at the beginning of the application. Then other routines make calculations, the results of which are added to that file via the 'storecoord' sub. No problems so far. Now I have a routine (a traverse) that creates new points w/coordinates intended to close upon itself or an existing point. These new points are also stored in the file. Still no problems to here. Since the traverse does not exactly close upon the 'closing point', I need to adjust the distances/coordinates to make it precise. This is where the 'compassrule' sub comes to play. The code in this sub is supposed to adjust the coordinates and put them back into the file. The math works fine but putting the points back into the file is where the error occurs. Looking at my code, you will see where I have tried to do this by closing the file, then opening it for writing (storecoord). I realize I will have 2 sets of the same points in the file (unadjusted and adjusted) but I will address that later. My work-around is to create a second file to contain the adjusted points, have the user exit the program and copy and paste from the second file to overwrite the points in the main file (strfilename). I could probably do it with code without leaving the program but that is a lot of additional code if not necessary. Hopefully this explanation will help your understanding of the issue.
I guess what I am really asking is whether or not my logic should work and if it is feasible to do it with the method I am using. Thanks Mike and Klaus for your help.
Hi Jim,
Couldn't you store the content of your file in a matrix variable, make all your changes and addings in that matrix and when leaving the program store the matrix back to the file. I find that this would be more efficient than working in the file or even with two files.
1) With a matrix variable:
Dim Pnt(1000,4)
where
Pnt(i,0)=coord(0)
Pnt(i,1)=coord(1)
Pnt(i,2)=coord(2)
Pnt(i,3)=coord(3)
i is the index of the point you are looking at
2) or with a structure variable
Dim Type (X, North, East, K) Pnt(1000)
where
Pnt(i).X=coord(0)
Pnt(i).North=coord(1)
Pnt(i).East=coord(2)
Pnt(i).K=coord(3)
You should adapt X and K to the meaning of your variables, as I don't know what coord(0) and coord(3) do mean.
3) or with single array variables:
Dim PntX(1000)
Dim PntNorth(1000)
Dim PntEast(1000)
Dim PntK(1000)
where
PntX(i)=coord(0)
PntNorth(i)=coord(1)
PntEast(i)=coord(2)
PntK(i)=coord(3)
4) Use a Table. This has the advantages (should you need them) that you can extend it dynamically merely by adding new rows (just like a file) and also filter to discover rows with given content without doing a search.
A table is just like a matrix but is declared like a Control, not a variable. This is because it can be visible to the user but does not have to be. The columns in the table are equivalent to the named elements in Klaus's "2) or with a structure variable".
Thank both of you very much for the alternatives. I will explore and try to implement something like that in my code. It may prove to be a valuable tool.
Klaus, coord(0) is the point number and coord(3) is the descriptor of the point.