Board index » cppbuilder » accesing records in TDBGrid

accesing records in TDBGrid


2004-07-30 12:33:28 AM
cppbuilder35
I'm trying to access individual cells in a TDBGrid, i'm using
AnsiString test = (GPC_GGT_DBGrid->Columns->Items[3]->Field
->Value);
but this returns only the first record in the specified column
(in this case column 3) how do i go down the grid to access the
data in different records?
 
 

Re:accesing records in TDBGrid

When working with any type of grid, I always get the data from the
underlying database and never from the grid. For example:
AnsiString test = tblCustomers->FieldByName("FirstName")->AsString;
 

Re:accesing records in TDBGrid

But what if I want to find a specific field value of a specific record?
i.e. I have a table of product details where I want to find the value of the
field "Price" of a record with a field "Product_Code" that have the value of
"ST-002"?
Thanks in advance.
Regards.
FernDog < XXXX@XXXXX.COM >wrote in message
Quote
When working with any type of grid, I always get the data from the
underlying database and never from the grid. For example:

AnsiString test = tblCustomers->FieldByName("FirstName")->AsString;


 

{smallsort}

Re:accesing records in TDBGrid

"newsreader" < XXXX@XXXXX.COM >wrote in message
Quote
But what if I want to find a specific field value of a specific record?
i.e. I have a table of product details where I want to find the value of
the
field "Price" of a record with a field "Product_Code" that have the value
of
"ST-002"?

Thanks in advance.
Regards.
You must search the database for that value you need to find. Please note
that more than likely, I use a completely different database than you do
which means that your syntax may likely be significantly different. It will
give you a general idea of what you need to do though.
AnsiString sProductID = "ST-002";
AnsiString sProdDesc;
if (tblProducts->AdsSeek(sProductID, stHARD)) {
// the record was found
sProdDesc = tblProducts->FieldByName("Description")->AsString;
}
else {
// the record was not found
ShowMessage("Product code was not found.");
}
Also, you should look at the examples that are included with Builder. I
briefly looked at one that demonstrates something similar to what you need:
C:\Program Files\Borland\CBuilder6\Examples\DBTasks\Find
 

Re:accesing records in TDBGrid

Thanks for the reply, after reading the help file you recommanded, and some
other referance materials, this is what I managed to construct: (Oh yes, I
am working with TurboDB, thus the Tdb prefix)
AnsiString searchKey = "UserId";
AnsiString resultKey = "Password";
Variant returned;
Variant searchValue(LoginUserName->Text);
TdbDatabase1->Connected = true;
returned.Clear();
returned = TdbTable1->Lookup(searchKey, &searchValue, resultKey);
if(returned.Type() == varNull)
{
MessageBox(Handle, "User not found", "Error", MB_OK | MB_ICONERROR);
TdbDatabase1->Connected = false;
return;
}
if(returned != LoginPassword->Text)
{
MessageBox(Handle, "Password error", "Error", MB_OK | MB_ICONERROR);
TdbDatabase1->Connected = false;
return;
}
It works, but I would like to know is there any better way of doing this?
The usage of Variants is giving me a hard time to understand. It's pure luck
that I managed to "hit the spot" in countless retry ro figure out how to use
variant (actually I just copied it from the examples, still haven't figured
out how to use a variant). Is there any other method that do not require the
usage of variant?
Also, I could not find any AdsSeek() referance in the help files...may I
know what object does it came from?
Regards.
FernDog < XXXX@XXXXX.COM >wrote in message
Quote

"newsreader" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...
You must search the database for that value you need to find. Please note
that more than likely, I use a completely different database than you do
which means that your syntax may likely be significantly different. It
will
give you a general idea of what you need to do though.


AnsiString sProductID = "ST-002";
AnsiString sProdDesc;

if (tblProducts->AdsSeek(sProductID, stHARD)) {
// the record was found
sProdDesc = tblProducts->FieldByName("Description")->AsString;
}
else {
// the record was not found
ShowMessage("Product code was not found.");
}


Also, you should look at the examples that are included with Builder. I
briefly looked at one that demonstrates something similar to what you
need:

C:\Program Files\Borland\CBuilder6\Examples\DBTasks\Find


 

Re:accesing records in TDBGrid

I've never used TurboDB myself. As for your question regarding AdsSeek,
that is one way to search for a record within Advantage Database by Extended
Systems. I personally am used to the navigational approach, hense
"AdsSeek", but other people prefer searching Advantage Database using SQL
querries instead.
It looks like you've got your search ability going on below. Is TdbTable1
connected to your grid? If this is the case and you don't want to lose your
current position when doing some calculation, you can open a second instance
to do that calculation.
For example, let's say that your grid is positioned on a particular record
and let's just say you some button that runs code to do some kind of
calculation, like obtaining the sum of all your products. Instead of using
TdbTable1 and screwing up your grids positioning, you can open up a new
instance of the table, TdbTable2 and do you calcs on that table.
"newsreader" < XXXX@XXXXX.COM >wrote in message
Quote
Thanks for the reply, after reading the help file you recommanded, and
some
other referance materials, this is what I managed to construct: (Oh yes, I
am working with TurboDB, thus the Tdb prefix)

AnsiString searchKey = "UserId";
AnsiString resultKey = "Password";
Variant returned;
Variant searchValue(LoginUserName->Text);

TdbDatabase1->Connected = true;
returned.Clear();
returned = TdbTable1->Lookup(searchKey, &searchValue, resultKey);
if(returned.Type() == varNull)
{
MessageBox(Handle, "User not found", "Error", MB_OK |
MB_ICONERROR);
TdbDatabase1->Connected = false;
return;
}

if(returned != LoginPassword->Text)
{
MessageBox(Handle, "Password error", "Error", MB_OK |
MB_ICONERROR);
TdbDatabase1->Connected = false;
return;
}

It works, but I would like to know is there any better way of doing this?
The usage of Variants is giving me a hard time to understand. It's pure
luck
that I managed to "hit the spot" in countless retry ro figure out how to
use
variant (actually I just copied it from the examples, still haven't
figured
out how to use a variant). Is there any other method that do not require
the
usage of variant?

Also, I could not find any AdsSeek() referance in the help files...may I
know what object does it came from?

Regards.
 

Re:accesing records in TDBGrid

I see. Thanks for the tips. By the way, TdbTable is functionally same as
TTable.
Regards.
FernDog < XXXX@XXXXX.COM >wrote in message
Quote
I've never used TurboDB myself. As for your question regarding AdsSeek,
that is one way to search for a record within Advantage Database by
Extended
Systems. I personally am used to the navigational approach, hense
"AdsSeek", but other people prefer searching Advantage Database using SQL
querries instead.

It looks like you've got your search ability going on below. Is TdbTable1
connected to your grid? If this is the case and you don't want to lose
your
current position when doing some calculation, you can open a second
instance
to do that calculation.

For example, let's say that your grid is positioned on a particular record
and let's just say you some button that runs code to do some kind of
calculation, like obtaining the sum of all your products. Instead of
using
TdbTable1 and screwing up your grids positioning, you can open up a new
instance of the table, TdbTable2 and do you calcs on that table.