José,
Not only can you have multiple databases open at the same time, but you can have multiple copies of the same database open. And you can build database objects that also contain other database objects. See this example:
http://forums.fivetechsupport.com/viewtopic.php?f=3&t=34310&sid=7105035b8916154ec3004198fe9c7663&start=15#p203884One of the advantages of database objects is that you don't have to deal with workspaces and aliases. These are handled automatically. Also you don't need scatter/gather routines--again handled automatically. And code is much easer to read since most of it is hidden.
- Code: Select all Expand view
oCustomers:= TCustomer():New()
oCustomers:seek( cCustNo )
MsgInfo( oCustomers:Company )
You may be worried about having multiple copies of the same database open at the same time. That was something that we needed to worry about a long time ago because the hardware was so limited. Now we have large amounts of memory and very fast CPUs, so it is not much of a problem.
How can I change the selected TDatabse object?
There is no selection required. You just reference the object. Simple!
- Code: Select all Expand view
oCustomer:company
oInvoice:company
oItem:description
The first thing I suggest you do is build a class for each database that you need to use. With coding we should always try to write each bit of code only once. So with database objects you should do the same. So each database class should open the database, it's indexes and set the primary key index to the default. Then instead of doing this each time you open a database:
- Code: Select all Expand view
oCustomers:= TDatabase():New(,"cust")
oCustomers:use()
oCustomers:setIndex("cust")
oCustomers:setOrder(1)
You build a class, that does that, so then you just do:
- Code: Select all Expand view
oCustomers:= TCustomers():new()
And you can open two copies at the same time like this:
- Code: Select all Expand view
oCustomers1:= TCustomers():new()
oCustomers2:= TCustomers():new()
Or, you can open two copies of the same database using exactly the same code if they are LOCALs in different functions or classes.
- Code: Select all Expand view
function one()
local oCustomers
oCustomers:= TCustomers():new()
...
Return nil
Function two()
local oCustomers
oCustomers:= TCustomers():new()
...
Return nil
An added advantage to isolating the name, source and indexes of the database in a database class will allow you, at a later time, to switch to a new database without changing any of your code except in the New() method of the class for that object. Not just a new DBF, but a new database type like SQL. Wow, encapsulation is very useful!
I think that I'll be able to use the class.
Not only will you be able to use it, database objects will open a whole new world for you.
James