Reputation: 3418
BACKGROUND
I am making a simple program to log data and do a few calculations with a .MDB database. accessed via Jet 4.
AIM
I wish to be able to get the value out of / into any field from a sql query on a primary key(one result)
CURRENT
I have currently connected to the database and I am able to view edit the data via "data controls"
as shown below:
QUESTION
How do I access the data in the database such that I may put the values into a suitable variable and then write them back?
Upvotes: 1
Views: 10629
Reputation: 12084
There are many ways to do what you are asking.
The most straight-forward way is to use a descendant of TDataset. The most common ones in Delphi for accessing Microsoft Jet would probably be dbGo (ADO Express when it was introduced in Delphi 5). If you've already hooked up the UI you're likely already using it. The same tables and queries you used to hook to your UI can be accessed directly in code. TADOQuery and TADOTable both support stepping through the dataset (using Next
), searching for specific records (using Locate
or Seek
) and accessing fields (using FieldbyNumber
or FieldbyName
)
For small and medium sized applications this is the easiest way to persist your data. Alternatively you can access the same data using the UI controls you've already hooked up to the data source. All the Delphi standard db aware controls have a Field
property and many have a Text
property.
For larger applications with complex business rules a domain layer with an ORM(Object/Relational Mapper) as the persistence layer is recommended.
Of course, small projects have a way of turning into big projects over time. So it would benefit you to plan accordingly.
Upvotes: 0
Reputation: 22236
Jørn Angeltveit's answer sounds like direct way to do at least part of what you want. He gives you way to access value in field. But realize that merely changing the value of that field will not update the record in the database; to do that you would also need to call the dataset's Post method. Your question is a very basic one and if you're building an app with a database backend you would probably be well-served to read up on how to write database apps with Delphi. If you try to go ahead without learning the basics of Delphi TDataset components then you're likely to end up with messy approach and extra work.
EDIT: Here's link to CodeGear site with some helpful docs on TDatasets. It is probably very similar to the documentation you'd find in the Delphi 7 help system (though finding your way through the built-in help can sometimes be confusing): http://docs.codegear.com/products/rad_studio/radstudio2007/RS2007_helpupdates/HUpdate4/EN/html/devwin32/fhxr30623_xml.html
Here's a good online series that actually focuses on accessing .mdb's using Delphi's ADO database components: Beginner's Guide to Databases with Delphi
Also, Marco Cantu has good sections on it in his book Mastering Delphi. You can pick up a used copy on Amazon for not much money. I used Mastering Delphi 6 when learning Delphi 7 and it was great resource. Mastering Delphi 7 is available, but I would save a few bucks and get used copy of Mastering Delphi 6: http://www.amazon.com/Mastering-Delphi-6-Marco-Cantu/dp/0782128742/ref=sr_1_6?ie=UTF8&qid=1296705281&sr=8-6
Upvotes: 7
Reputation: 3079
Is MyDataSet.FieldByName('TheFieldName').AsString := myString;
(and vice versa) what you are looking for?
Upvotes: 2