Recent

Author Topic: Cross Unit DBF access  (Read 1122 times)

Petrus Vorster

  • Full Member
  • ***
  • Posts: 222
Cross Unit DBF access
« on: June 27, 2025, 11:44:12 am »
Greeting all.

I have a form (unit1) with some TDBF controls on it.
It uses datagrids or tstringgrids to display some data.

I created another form in which some data is updated and saved back into one of those databases.

I have found that one cannot refer to TDBF control from another unit like you can do with normal controls. Inserting to a database which has controls connected to it on another form appears to be ignored.

Should I deactivate the Database on form 1 before form2 opens or is there a way to refer to that control on the first form?

Regards - Peter

cdbc

  • Hero Member
  • *****
  • Posts: 2606
    • http://www.cdbc.dk
Re: Cross Unit DBF access
« Reply #1 on: June 27, 2025, 12:52:34 pm »
Hi
Take a look at my version 2 example in your other thread (post #8), the no-dbaware...
As you'll see, in that app, it would be _no_ problem to add another view and connect it to the 'Singleton' database-connection, but, alas, you keep toying with that ol' DBF-Shijt...  %)
(...and db-aware components)
Regards Benny
« Last Edit: June 27, 2025, 01:01:49 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Petrus Vorster

  • Full Member
  • ***
  • Posts: 222
Re: Cross Unit DBF access
« Reply #2 on: June 27, 2025, 01:07:10 pm »
Yes, that old DBF stuff might be old, but for my needs that is more than enough.
It is also simple to use.
I make desktop applications that helps where the workplace does not provide ad probably never will, and they will never allow me to make anything that will connect to a server or cloud databases.

I am way to close to retirement to bother with more complex stuff that I am never going to use.
I'm not in IT, and i'm a manager in a Medium size town with horrible infrastructure in a collapsing state. I am just holding everything together with zero funds and no interest from anywhere.  :D

You fellows here are WAY ahead of me. I honestly wish I joined here 15 years ago when I was still struggling with Freebasic/Powerbasic.

PS: I found the solution to my problem.
Do not activate the DBF connection on the unit two until the form is created.
Disable form1 connection, load form2, connect form2 DBF, Disconnect when close, Enable form1 DBF.

-thanks mate.
-Peter

wp

  • Hero Member
  • *****
  • Posts: 13351
Re: Cross Unit DBF access
« Reply #3 on: June 27, 2025, 01:49:46 pm »
The recommended way to have multiple forms access the same database is to put the database component (TDbf) on a TDataModule; this is something like a hidden form for all non-visual components. You only must make sure that the datamodule is created before it is used (if you auto-create it it must be listed before the main form in "Project options" > "Forms". And of course, you must add the datamodule to the uses clauses of every form which accesses the database.

In the attachment I am showing you a small sample dbf in which the records are listed in the DBGrid in the mainform and the recors are edited in a dedicated editor form. Both forms are accessing the same dbf file on the datamodule.
« Last Edit: June 27, 2025, 02:19:08 pm by wp »

Petrus Vorster

  • Full Member
  • ***
  • Posts: 222
Re: Cross Unit DBF access
« Reply #4 on: June 27, 2025, 03:56:43 pm »
That is something that might work!

Thanks a million.

-Peter

Petrus Vorster

  • Full Member
  • ***
  • Posts: 222
Re: Cross Unit DBF access
« Reply #5 on: June 30, 2025, 04:00:51 pm »
Thank you very much.

This was some great advice.
That does exactly what i need it to do.

-Peter

 

TinyPortal © 2005-2018