* * *

Author Topic: [Solved] Why cannot type to DBEdit  (Read 301 times)

drschwarcz

  • New member
  • *
  • Posts: 17
[Solved] Why cannot type to DBEdit
« on: March 03, 2017, 10:19:22 am »
Hi there!

I just started to learn to Database handling and I try to use "Data Controls" items.

Where I'm now: Can create a Database and can build connection. Data writing (from Code) and reading are successful (be published in Controls).

And we arrived to my trouble: Why cannot type anything to Controls, for example, to DBEdit?

My easy program in attached (without embed Firebird).

Please, be lenient with me, I'm a beginner.  :)  O:-)

I tried look for the answer in Forum, but couldn't find. THX for your help in advance.
« Last Edit: March 03, 2017, 04:10:02 pm by drschwarcz »
Lazarus 1.6.4 @ Windows 10 x64

Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe.

Einstein, Albert :) ;)

rvk

  • Hero Member
  • *****
  • Posts: 2407
Re: Why cannot type to DBEdit
« Reply #1 on: March 03, 2017, 10:50:38 am »
Ok, to help you on your way a bit.... you can do the following.

1) You've connected a datasource to your SQLQuery. You should only do that if you want to do a master/detail form. In this case you connect the same datasource which is already connected to the SQLQuery itself. That can't work and you'll get an exception. So remove the datasource from SQLQuery in the Object inspector. (you'll see that SQLQuery is kept in "DataSource")

2) Add the following lines to open the Query when activating the form.
Code: Pascal  [Select]
  1.     SQLQuery.SQL.Text := 'select * from Adatok';
  2.     SQLQuery.Open;

3) And last... it would help you a lot to know if your dataset is in Insert/Edit/Browse mode. You can drop a TDBNavigator on the form (first component in the Data Controls-tab). Set the Datasource to "DataSource" and you get a navigator with which you can add and insert records.

After that, you're still not done yet because you have some required fields you don't set on insert, but at least you're on your way and can do the edit.

wp

  • Hero Member
  • *****
  • Posts: 3352
Re: Why cannot type to DBEdit
« Reply #2 on: March 03, 2017, 10:54:24 am »
First of all: A question in a forum should also be helpful to other users if they have the same problem. If you post code along with your question you should not post it in your private cloud or some throw-away site, but you shoud add the code as an attachment: Pack all required files to compile the demo, usually the .pas, .lfm, .lpi, .lpr files, into a common zip which you can upload here. The upload button is hidden underneath, in the "Attachments and other options" box. This way your question will be complete as long as this forum exists, whatever you erase in your private cloud, or whenever throw-away sites expire.

Your issue: You want to edit some field of the database, but you don't specify which field in which table. You have a TDBEdit (correct), connected to a TDatasource (correct), connected to a TSQLQuery (correct). But you don't specifiy the SQL needed to define the fields used. Since I don't know the tables and fields in your database I cannot give you a precise example. Suppose the table is named "employees" and contains fields "Name" and "Address" then you should type
Code: [Select]
  SELECT Name, Address FROM Employees in the SQL property field of the query. Now that the field "Name" is defined you can select this field in the property DataField of the DBEdit. For this to work in the ObjectInspector you must set the IBConnection to Connected := true, and you must open the Query (SQLQuery.Active := true). Now you should see the Name in the DBEdit.

To edit the displayed Name you must put the dataset (query) into Edit mode. You can do this in code by calling SQLQuery.Edit. Or, in ObjectInspector, add a TDBNavigator which you link the the same Datasource as the DBEdit. Then, when the program runs, you click on the pen icon to make the query editable.

To insert a new record you click on the "+" icon of the navigator at runtime. Or, in code, call SQLQuery.Insert or SQLQuery.Append.

After having made your changes you must save the edited/new record. In code, you call SQLQuery.Post (or SQLQuery.Cancel if you want to abort this edit), In the navigator at runtime you click on the checkmark icon to save (or the cancel icon to cancel). Or simply navigate to another record.

After having set up everything you should disconnect the IBConnection from the database (IBConnection.Connected := false) and close the query (SQLQuery.Active := false), and you should set these properties to true in your code. This avoids crashes when loading your project and the database file is not found any more (for example, because you moved it to a different folder).
Lazarus trunk / fpc 3.0.0 / Win32

drschwarcz

  • New member
  • *
  • Posts: 17
Re: Why cannot type to DBEdit
« Reply #3 on: March 03, 2017, 04:09:17 pm »
Thanks both of you!  :) Your answers were quite useful for me, thus I can move to the next step in my program.


@wp

Yes, I saw the possibility of attaching files.  :-[ Attached the .zip to Forum now, but my original was 12MB (max size in Forum is 250KB*4). So, Firebird (embed) has been removed.
Lazarus 1.6.4 @ Windows 10 x64

Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe.

Einstein, Albert :) ;)

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus