Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using 1 table in 2 different forms

Filip Brnic

Programmer
Dec 25, 2023
58
RS
Hello, i have an issue

First of all let me just give you a picture of the form layout

FORM 1
Tables: Robak, Roba
Has a grid connected to robak
A button that opens a form that needs to load Robak

FORM 2
Tables: Robak

So i tried the following

In form 1 command button
do form form2

in form 2 init property i have the following
use robak shared
select robak
(some code that works below not connected to this)
i also have data session set to 2 in this form

When i try to use the button in form 1, with data session set to 2 in form 2 i get the error this file is used by another user
When i try to use the button in form 1, with data session set to 1 in form 2 i sucessfully open the form2 but the issue now is that my grid just becomes blank, dissapears. I'm currently reading the help guidance provided by fox, i stumbled upon set releation to procedure, but im still trying to figure out what to do.

And to mention i don't have anything in the data enviroment in form 2 just incase anyone was wondering because i know that can mess things up sometimes.
 
"File is used by another user" sounds to me like you have used a file exclusively, or you used an FLOCK() to lock the whole file.

So, before looking into the data session issues, check to be sure you have SET EXCLUSIVE OFF and that you haven't opened any tables with EXCLUSIVE or called an FLOCK() somewhere, which lock the entire file.
 
Nope, no flock and set exclusive is off in both forms load property...
 
Nope, no flock and set exclusive is off in both forms load property...
Then the problem is related to the data sessions.

I never use data sessions, so I don't have any experience troubleshooting them, but I know enough to know that there are configuration options in data sessions that you can explore such as private vs default datasets (private isolates the session and keeps record positions, index orders, etc. independent from the default), and there are multiple buffering options that may play a role.

Since you said one form has a data session, and the other does not, this should be the first thing you explore.
 
the issue now is that my grid just becomes blank,

This is a separate issue - not related to the error you are seeing or your DataSession settings.

The problem occurs when you set the grid's RecordSource, but then later close the underlying table (the one that the RecordSource point to), and then re-open the table. I don't know the exact sequence of events in your case, but I suspect it is something along those lines.

The solution is to remove the RecordSource (that is, set it to an empty string), then re-open the table, then set the RecordSource back to pointing to the table.

Also, I don't think SET RELATION will help in this case. Whatever caused the "File in use by another" error won't be solved by SET RELATION.

Mike
 
Hi,

The DATASESSION property of each form has to be set to 2 (Private). Then you have full control of the underlying table(s).

From Hacker's Guide to VFP 7

Set an object's DataSession to 2 to keep it private. This allows multiple instances of the same object to be running at the same time without interfering with each other. Each form has its own DataSessionID, its own set of work areas, its own record pointers and its own set of aliases. This last is really cool. With a private data session, you can use the same alias every time, something you can't do with USE AGAIN.

You may also want to have a look at "Programming for Shared Access" in the help file.
hth

MarK
 
Just open the DBF like
Code:
MyGRID.addproperty("SrcAlias", "D_"+ThisForm.name)
USE (sourcefilename) alias (MyGRID.SrcAlias) in select(MyGRID.SrcAlias) SHARED [NOUPDATE] AGAIN
MyGRID.RecordSource = MyGRID.SrcAlias
in both forms.
Here You have 2 different Aliases
 
Hey
Hello, i have an issue

First of all let me just give you a picture of the form layout

FORM 1
Tables: Robak, Roba
Has a grid connected to robak
A button that opens a form that needs to load Robak

FORM 2
Tables: Robak

So i tried the following

In form 1 command button
do form form2

in form 2 init property i have the following
use robak shared
select robak
(some code that works below not connected to this)
i also have data session set to 2 in this form

When i try to use the button in form 1, with data session set to 2 in form 2 i get the error this file is used by another user
When i try to use the button in form 1, with data session set to 1 in form 2 i sucessfully open the form2 but the issue now is that my grid just becomes blank, dissapears. I'm currently reading the help guidance provided by fox, i stumbled upon set releation to procedure, but im still trying to figure out what to do.

And to mention i don't have anything in the data enviroment in form 2 just incase anyone was wondering because i know that can mess things up sometimes.
Hi Filip,

as mjcmkrsr explained, setting DATASESSION property to 2 (PRIVATE) should resolve your issue. Also, better to add AGAIN clause in your USE statement, this will avoid any error of "ALREADY IN USE"
 
Ive tried setting the datasesion property to 2 in both forms, still the same error, tried use robak again and use robak shared and both give the same error regardless. also tried use robak again alias robak and still the same error, the forms work individually but opening the other one with the first one open causes errors, you might find something from this video, i showed every code that could be related to this.
 
You write that Set Exclusive is Off, I say that you're wrong! Scan your code with GoFish, and search for the word Exclusive.
 
You write that Set Exclusive is Off, I say that you're wrong! Scan your code with GoFish, and search for the word Exclusive.
Hello, now i get the error file is in use, coming from the form 2 where i have the code
Use robak shared
I also tried
Use robak alias robak again
I also tried
Use robak in 1 and use robak in 0 and still the same error
 
When you get the error, select Suspend. Then type ?Set('exclusive') in the command window, and see the result.

If that doesn't help, keep the debugger open when you run the program. Before running the program, select the debugger, and add a breakpoint (ctrl-B), Type =Break when expression has changed. In the Expression field, type Set('exclusive'). Then press Add, and run the program.

Something very basic is wrong in your setup. Could it be that you have Exclusive = On in your config.fpw?
 
When you get the error, select Suspend. Then type ?Set('exclusive') in the command window, and see the result.

If that doesn't help, keep the debugger open when you run the program. Before running the program, select the debugger, and add a breakpoint (ctrl-B), Type =Break when expression has changed. In the Expression field, type Set('exclusive'). Then press Add, and run the program.

Something very basic is wrong in your setup. Could it be that yo1735221073972.pngu have Exclusive = On in your config.fpw?
ON THE SECOND LINE OF CODE GOT THIS.
 
Of course, and that line is Set Exclusive On!!! Set it to Off, and your problem is solved.
 
maybe the "USE"-Line missing SHARED
Frm_397.jpg
Code:
USE robak {ALIAS (aliasname) IN SELECT(aliasname) } SHARED AGAIN

the SHARED-Clause overwites the EXCLUSIVE-Setting

optional give the used "robak.dbf" an unique aliasname, as i've shown few comments above to avoid duplicate aliases

Remember, if you open a table multiple times within the same datasession, all additional USE SHARED will have the same properties.

Frm_398.jpg
 

Attachments

  • 1735223103927.png
    1735223103927.png
    49.3 KB · Views: 3
The line failing is
use robak shared
isn't it?
I agree with Tore, the form1 opens the table exclusive, or it was already open exclusive before even running, for example due to being open within the IDE when testing within the IDE.

Notice, Filip, that for the IDE the dafault setting of EXCLUSIVE is ON, as the developer you likely often need DBF opened excluive. But when you want to test shared usage of DBFs, that's hindering that. a Database included in the project also keeps a database open (exclusive). So for testing change the IDE settings.

Also notice, that some settings, like EXCLUSIVE and others are settings per datasession. That's meaning you have to set them for every form, not only for form1. to be consistently the same in all datasessions. That's described in the help topic about commands that scope to a session. Which means they only influence the current datasession, not all of VFP and all forms within other datasessions:

That this exists and how it's chosen actually makes all these things better scopable, if that word exists, if you would start a transaction throughout all datasessions, transactions wouild become useless. For some settings, like Exclusive, it's discussable whether they could also be global, but nothing is in your way of turning that off for all sessions by putting SET EXCLUSIVE OFF into every form load.

That excursion to session scoped settings aside, you'd still ask yourself why it failed to make form2 part of the same datasession? Well, your still do USE ROBAK SHARED. Although it is already open. Perhaps in another workarea than the current one.

Look into the USE commmand once more and rea about the IN clause, to understand why it's usually always the best to make any USE... IN 0, which means in a new unused workarea. And in this case, when you don't use a privatre datasession robak is already open from form1, why do you use it at all? It's already used by form1. You confirm my judgement. I also wonder why you don't get told the obvious by others. Anyway, there you are.

Putting SET EXCLUSIVE OFF in both forms load procedures would make it possible for both forms to USE robak shared. It would also work wwith EXCLUSIVE ON, if you always USE robak shared. But you'd open a DBC exclusive, and that can then only be opened by one form. So you better work shared, and have something in your IDE that ends the IDE mode before starting tests, that IDEshutdown.prg should close all tables and databases, close all sessions, clear all objects and class definitions from memory and then set exclusive off and make any other general settings necessary for testting and then start your projects main.prg to run the app as if it was started by windows without any previous VFP runtime and datasession etc. usage.
 
By the way, the reason Tore even noticed you have to have exclusive on in his 8:22 AM post, is that there is no other reason you get this error. When an alias is used already and you try to USE in another workarea you get the error 3 - File is in use, not error, 108 - File is in use by another user. They differ.

But telling us exclulsive is off when it's on, you need glasses, it seems.
 
Thanks Chris, ill look into what you told me, i did have exclusive off before ive sent you the video, but i still got the error file in use by another user.
What im curious about is, is this something simple that i just made difficult, like for example
Some of you here would be able to make this work in like 5 minutes?
Regardless, I do understand the logic and I did have in mind alot of solutions for this but its just the lack of experience in fox it seems.
But there are alot of answers here, so which ones do i implement because i guess there are multiple approaches?
Should i try what @EinTerraner suggested?
 
Can someone just do this and send me the project file or something, I'm at my limit, im wasting time trying to figure out how to fix the most basic shitty error possible.

I have so much more i wanna do but can't because im stuck on something i have no fucking clue how to solve. Can someone just show me and ill remember for the rest of my life.
 

Part and Inventory Search

Sponsor

Back
Top