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 Chris Miller 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.
 
Honestly, are you blind?

You showed us the line which says Set Exclusive On, and I told you to change it into Set Exclusive Off.

That will fix your problem, why don't you simply do as told????
 
Then repeat the procedure I showed you!!!!!!

There's no other explanation to your problem than Set Exclusive On or a misplaced Exclusive in a Use statement. Or Exclusive=ON in your config.fpw.

That means you should also run GoFish and search for the word Exclusive. Remember that the word Exclusive can also be shorter, like Excl.

I consider most of the other advices given here as irrelevant, and will only confuse you.

I have seen it too many times, "I have done this and checked that", only to read later "oops, I overlooked that...."

If your car engine suddenly stops, it's either no more gas, or the engine is defective. Checking the wheels or emptying the glove compartment is a waste of time...

And as a last resort, have you restarted your computer?

Update: I never use Data Environment, never have, never saw any advantage.

However.... in the Data Environment, when you select a table, right click and select Properties, what do you see for the property Exclusive? Could it be True by any chance? If so, set it to False. And in the future, have Set Exclusive Off all the time, and never ever change it!
 
Last edited:
Filip,

if you're now having SET EXCLUSIVE OFF in both forms, but still have form2.DataSession=1 (default datasession), then the problem you have is with your USE ROBAK as I explained above, reread that:
...in this case, when you don't use a private datasession robak is already open from form1, why do you use it at all? It's already used by form1.
It's not only unnecessary to USE ROBAK SHARED, it's harmful. Because it doesn't only open ROBAK, at the same time it closes whatever is open in the current workarea. And there is something open, because otherwise you wouldn't have a problem. And that's why I recommend you read the USE help chapter about the IN clause. USE is always opening AND closing in one workarea. Without an IN clause USE works in the current workarea. So you do open robak but also close another table that's open in the current workarea. And that is why something else stops working, even though you don't get any error now.

If you SET EXCLUSIVE OFF in BOTH forms, then go back to make form2 have a private datasession. Then it's not harmful to USE ROBAK SHARED, it's even necassary, so just Datasession being default or private decides whether you need USE ROBAK or not. It's even a better idea to let all forms have a private datasession and let each form open the tables it needs and only let forms run with a default datasession when you know what this means and know you want that to happen.
 
Last edited:
I assume that your object for this table is a GRID. Then add the following properties and the corresponding code to this object. You can define the properties during the design.

Code:
GridName.DisRobak

** Grid.Init
with This
** Unique Aliasname
  .DisRobak = "DisRobak_"+.Name+"_"+ttoc(datetime(),1)
....
ENDWITH

** Grid.Destroy
with This
** close eventually here used aliases
  use in select(.DisRobak)
endwith

Grid.WhereverYouUseIt
with This
 TRY
  use ROBAK alias (.DisRobak) in selec(.DisRobak) SHARED AGAIN
CATCH To Lo_Error
 SET STEP ON
** Check ASESSIONS and AUSED and/or your DATASESSION-Window
ENDTRY

In case the GRID in both forms have the same name, just replace the ".Name" with "ThisForm.name" oder "ThisForm.name+'_'+.name" in the INIT-Procedure. The UnderScore i use make it better readable
 
Assuming both forms have private datasessions:
If You use the Table in the first form as EXCLUSIVE and try to reuse it in the second form (other datasession) you might not use it again. Within the other datasession You dont see the already the used one and therefore the error "Used by another" will fire.
 
Hi Filip,

Please find below the demo code for one or more forms accessing the same table

Code:
PUBLIC oForm1, oForm2

LOCAL i

CREATE TABLE  dbfTemp (f1 I, f2 I, f3 I)
    FOR i = 1 TO 50
        INSERT INTO dbfTemp VALUES ( Int(Rand()*1001), Int(rand()*1000), Int(Rand()*999))
    ENDFOR
    
USE
    
oform1=NEWOBJECT("form1")
oform1.Show

oform2=NEWOBJECT("form2")
oform2.Show

Read Events

Close all
Clear All

RETURN


**************************************************
DEFINE CLASS form1 AS form
    Caption = "Grid with calculated columns"
    Left = 24
    Top = 48
    MinHeight = This.Height
    MinWidth = This.Width
    MaxWidht = This.Width
    MinButton = .F.
    MaxButton = .F.
    DataSession = 2
 
    ADD OBJECT grid1 AS grid WITH ;
        ColumnCount = -1, ;
        Left = 10, ;
        Top = 30, ;
        Width = ThisForm.Width - 20, ;
        Height = ThisForm.Height - 40, ;
        RecordSource = "dbfTemp", ;
        Anchor = 15
 
        PROCEDURE grid1.Init
             WITH This.Column1
              .ControlSource = "F1"
              .Header1.Caption = "F1"
             ENDWITH

             WITH This.Column2
              .ControlSource = "F2"
              .Header1.Caption = "F2"
             ENDWITH

             WITH This.Column3
              .ControlSource = "F3"
              .Header1.Caption = "F3"
             ENDWITH
         ENDPROC
 

PROCEDURE Destroy
    Thisform.Release()
    CLOSE ALL
    Clear Events
ENDPROC

PROCEDURE Load
    USE dbfTemp IN 0
ENDPROC

ENDDEFINE
*********************************************
DEFINE CLASS form2 AS form
    Left = 480
    Top = 72
    Caption = "Grid with calculated columns"
    MinHeight = This.Height
    MinWidth = This.Width
    MaxWidht = This.Width
    MinButton = .F.
    MaxButton = .F.
    DataSession = 2
 
    ADD OBJECT grid1 AS grid WITH ;
        ColumnCount = -1, ;
        Left = 10, ;
        Top = 30, ;
        Width = ThisForm.Width - 20, ;
        Height = ThisForm.Height - 40, ;
        RecordSource = "dbfTemp", ;
        Anchor = 15
 
        PROCEDURE grid1.Init
             WITH This.Column1
              .ControlSource = "F1"
              .Header1.Caption = "F1"
             ENDWITH

             WITH This.Column2
              .ControlSource = "F2"
              .Header1.Caption = "F2"
             ENDWITH

             WITH This.Column3
              .ControlSource = "F3"
              .Header1.Caption = "F3"
             ENDWITH
         ENDPROC

PROCEDURE Destroy
    Thisform.Release()
    CLOSE ALL
    Clear Events
ENDPROC

PROCEDURE Load
    USE dbfTemp IN 0
ENDPROC

ENDDEFINE
*********************************************

hth

MarK
 
...
and a fancier example showing the versatility of DATASESSION = 2 - three forms using one table with different index tags and creating their own sets of cursors all named identically (see attachment).

hth

MarK
 

Attachments

  • OneTableMoreForms.txt
    10.5 KB · Views: 6
Last edited:

Part and Inventory Search

Sponsor

Back
Top