cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Register to attend Discovery Summit 2025 Online: Early Users Edition, Sept. 24-25.
  • New JMP features coming to desktops everywhere this September. Sign up to learn more at jmp.com/launch.
Choose Language Hide Translation Bar
tommst
Level I

How to determine if a data table is already opened?

This is how I used to do it before JMP 8:

tableIsOpen = Function( {tname},
	{dt, rv = 0},
	Try(
		dt = Data Table( tname );
		rv = 1;
	);
	rv;
);


This does not work in JMP verison 8 because DataTable function will open a file dialog if the table is not opened already. User can always press "cancel" but it's annoying.

Thanks for any help.
Tommy

4 REPLIES 4
XanGregg
Staff

Re: How to determine if a data table is already opened?

You can loop through all the open tables by passing a number to Data Table() and compare names.

tableIsOpen = Function( {tname},
	{rv = 0, i},
	For( i = 1, i < N Table() + 1 & rv == 0, i++,
		If( Lowercase( Data Table( i ) << Get Name ) == Lowercase( tname ),
			rv = 1
		)
	);
	rv;
);

edit: with a "<=" option

tableIsOpen = Function( {tname},
	{rv = 0, i},
	For( i = 1, i <= N Table() & rv == 0, i++,
		If( Lowercase( Data Table( i ) << Get Name ) == Lowercase( tname ),
			rv = 1
		)
	);
	rv;
);


(The forum software really doesn't like less than or equal, which is why I use plain less than and added 1.)

tommst
Level I

Re: How to determine if a data table is already opened?

Thanks for the code. I'm trying it out now.
I wish there's a more straight forward way of checking for opened data file.
Plotasaurus
Level II

Re: How to determine if a data table is already opened?

Came across this thread looking for something similar for closing lingering open data tables at the beginning of my script while testing code, instead of counting them. Used a combo of the code posted here and in another thread. Thought I'd post in case it's useful for anyone coming across this thread:

 

// list of table names that I want to make sure are closed before running the script
tname = { "TABLE1", "TABLE2", "TABLE3", "TABLE4" };

// loop to see if open tables match the name in the list above, then close them
// need to restart the i counter to i = 0 because it seems closing a table changes the 'i' number associated with the table
For( i = 1, i <= N Table(), i++,

	If( N Rows (Loc( tname, Data Table(i) << Get Name ) ) > 0, 
		Close(Data Table(i), no save);
		i = 0;
	)
);

 

txnelson
Super User

Re: How to determine if a data table is already opened?

I would simplify the closing by using the Try() function

 

Names Default To Here( 1 );

tname = {"TABLE1", "TABLE2", "TABLE3", "TABLE4"};

For Each( {dt}, tname,
	Try( Close( data table( dt ), nosave ) )
);

 

Jim

Recommended Articles