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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Varibale Name for two different tables 1

Status
Not open for further replies.

benny7

Programmer
Oct 3, 2004
31
GB
I know I can open tables with an alias name and I know I can open a table using a varible name, i.e.

use table1 alias tablename
or
tablename = "table1"
use (table1)

Basically I've got two tables with similar information. Circumatances will determine which table I want to use. Say I open a table using a variable name:

tablename = "table1"
use (tablename)

How to I refer to the fields. Normally I would just say something like ddd = table.fieldname. As "tablename" is a variable holding the name of an open table this does not work, ddd = tablename.fieldname shows the following error:

tablename is not an object

How can I refer to a field in a table, using a variable name for that table??? Hope you understand the question.

 
Here is an example. At the last line you will notice the two dots between the variable and the field name.

Code:
CREATE CURSOR myCursor (name c(20))
INSERT INTO myCursor (name) VALUES ("Mike")
?alias()
lcTable = ALIAS()
?&lcTable..name



Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Hi

Yep, I know about the alias clause. I was hoping there was a way I could do it without the alias as the table is refered to in hundreads of different places and I only wanted to refer to a different one in one or two parts of the code. For example I wanted something like:

If suspend
Table1 = "suspendtable"
Else
Table1 = "reallytable"
endif

Then refer to Table1 for fields, and also be able to refer to fields in both tables by using there really names.

i.e. test = table1.name
test = suspentable.name
test = reallytables.name

All the above should be able to be used.

I thought it may be

test = $table1.name
test = table1..name

This doesn't work







 
Benny7

As I suggested above try:

?&Table1..name


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Hi,

to add to that:

It's a difference to open a table with an aliasname or use a variable containing the real tablename. Because the one thing does change what ALIAS() will give back and the other does not.

works:
Code:
use table alias anothername
? anothername.field

won't work:
Code:
anothername = "table"
use (anothername)
? anothername.field

It won't work, because the alias of the table is still "table", not "anothertable". And anothertable is just a variable containig the string "table", and not a reference to the table itself or an object with properties.

There's one thing, that is dangerous about opening a table with a different alias name: You may have code somewhere else, where you use that table with it's original alias and get an error "table already in use". But because the table is in use with a different alias name, it's harder to find out, why the table is already in use, because it's not obvious. Even if you precheck with used("table"), if the table is already used, you get .F., although the table is in use! So I'd recommend to use alias as sparesely as possible, perhaps just only if you need a table open twice.

Using an aliasname is a solution, if you want to reuse code that was already written for that other alias with another table of the same structure, but some general code that does not depend on the real table name can be done, as Mike showed, that is called macro substitution.

works:
Code:
anothername = "table"
use (anothername)
? &anothername..field
One of those two periods is the sign for the end of macro substitution, the other one is the normal period between alias and field. You could also open the table using macro substitution:
Code:
anothername = "table"
use &anothername
? &anothername..field
This time that period as the end of the macro substitution is not needed. But macro substitution is slower than using name expressions (that is using the version with braces), but unfortunately something like this won't work:
Code:
anothername = "table"
use (anothername)
? (anothername).field
This has to be done with a whole name expression:
Code:
anothername = "table"
use (anothername)
afullfieldname = anothername+".field"
? (afullfieldname)
I'd say all in all this doesn't spare time, as you need to create the variable and set it to the full fieldname first.
That may spare time used in a loop on many records.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top