BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SQL Query Help

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (ADO)
View previous topic :: View next topic  
Author Message
Grant
Guest





PostPosted: Sun Oct 08, 2006 9:30 pm    Post subject: SQL Query Help Reply with quote



Hi all I have a access database." mail.mdb".

I am trying to loop through all the records in a field. Field name is
"ListName".
This is were I am.
ShowMessage(Query1.fieldbyname('ListName').AsString);
My Sql is. select * from ListNames where ListName = :param1.
So I want to show message of all info in the field ListName.
Thanks in advance for helping.
Back to top
Bill Todd
Guest





PostPosted: Sun Oct 08, 2006 10:49 pm    Post subject: Re: SQL Query Help Reply with quote



Grant wrote:

Quote:
Hi all I have a access database." mail.mdb".

I am trying to loop through all the records in a field. Field name is
"ListName". This is were I am.
ShowMessage(Query1.fieldbyname('ListName').AsString);
My Sql is. select * from ListNames where ListName = :param1.
So I want to show message of all info in the field ListName.
Thanks in advance for helping.

Your message is not clear. Unless ListName is the primary key your
SELECT may return more than one row. ShowMessage displays a dialog box
that contains a single string. Either you have to loop through the rows
returned by the query and concatenate all of the returned ListNames
into a string or you need to display the returned data in a DBGrid or
DBListBox.

--
Bill Todd (TeamB)
Back to top
Grant
Guest





PostPosted: Sun Oct 08, 2006 10:52 pm    Post subject: Re: SQL Query Help Reply with quote



Yes I need to loop through the rows
returned by the query and concatenate all of the returned ListNames
into a string. Well i need to loop through all and populate a combobox.
Thanks
Grant
"Bill Todd" <no (AT) no (DOT) com> wrote in message
news:45293a39$1 (AT) newsgroups (DOT) borland.com...
Quote:
Grant wrote:

Hi all I have a access database." mail.mdb".

I am trying to loop through all the records in a field. Field name is
"ListName". This is were I am.
ShowMessage(Query1.fieldbyname('ListName').AsString);
My Sql is. select * from ListNames where ListName = :param1.
So I want to show message of all info in the field ListName.
Thanks in advance for helping.

Your message is not clear. Unless ListName is the primary key your
SELECT may return more than one row. ShowMessage displays a dialog box
that contains a single string. Either you have to loop through the rows
returned by the query and concatenate all of the returned ListNames
into a string or you need to display the returned data in a DBGrid or
DBListBox.

--
Bill Todd (TeamB)
Back to top
Bill Todd
Guest





PostPosted: Sun Oct 08, 2006 11:18 pm    Post subject: Re: SQL Query Help Reply with quote

Grant wrote:

Quote:
Yes I need to loop through the rows
returned by the query and concatenate all of the returned ListNames
into a string. Well i need to loop through all and populate a
combobox.

I still do not know what your question is. What do you not know how to
do? Do you need help with code to loop through the returned values and
concatenate them?

--
Bill Todd (TeamB)
Back to top
Grant
Guest





PostPosted: Mon Oct 09, 2006 1:49 am    Post subject: Re: SQL Query Help Reply with quote

Tlhanks for helping me. I do not no any SQL.
Lets say there is 20 records . The field ListName might have list1,
list2.list3, list4 and so on. I need to figure out the correct sql to loop
through all the records and return info to a combobox. I hope this willl
clear it up. Thanks for all comments.

"Bill Todd" <no (AT) no (DOT) com> wrote in message
news:452940ff (AT) newsgroups (DOT) borland.com...
Quote:
Grant wrote:

Yes I need to loop through the rows
returned by the query and concatenate all of the returned ListNames
into a string. Well i need to loop through all and populate a
combobox.

I still do not know what your question is. What do you not know how to
do? Do you need help with code to loop through the returned values and
concatenate them?

--
Bill Todd (TeamB)
Back to top
Bill Todd
Guest





PostPosted: Mon Oct 09, 2006 3:31 am    Post subject: Re: SQL Query Help Reply with quote

Ok. Since you posted in the ADO newsgroup I assume you are using and
ADODataSet to execute the SELECT statement. The code would be almost
the same if you use an ADOQuery.

// Assign a value to the parameter in the SELECT statement.
AdoDataSet1.Parameters.ParamByName('Param1').Value := TheListName'
// Execute the query.
AdoDataSet1.Open;
// Empty the combobox.
ComboBox1.Items.Clear;
// Loop through the records returned by the query and add the
// ListName value to the combobox.
while not AdoDataSet1.EOF do
begin
ComboBox1.Items.Add(AdoDataSet1.FieldByName('ListName').AsString);
AdoDataSet1.Next;
end;

--
Bill Todd (TeamB)
Back to top
Grant
Guest





PostPosted: Mon Oct 09, 2006 4:31 am    Post subject: Re: SQL Query Help Reply with quote

Thanks for you patience and help. I am useing an ado.query. look below.

// Assign a value to the parameter in the SELECT statement.
Query1.Parameters.ParamByName('Param1').Value := 'ListName';
// Execute the query.
Query1.Open;
// Empty the combobox.
ComboBox1.Items.Clear;
// Loop through the records returned by the query and add the
// ListName value to the combobox.
while not Query1.EOF do
begin
ComboBox1.Items.Add(Query1.FieldByName('ListName').AsString);
Query1.Next;
I enter in a,b,c,d and when i run the app it just load the letter d into the
combobox.. I wornder is my sql statement correct. here it is.
select * from ListNames where ListName = :param1 .

I wornder if i need to alter the statmemt. Again I do not know SQL .

Thanks

Grant

"Bill Todd" <no (AT) no (DOT) com> wrote in message
news:45297c3c (AT) newsgroups (DOT) borland.com...
Quote:
Ok. Since you posted in the ADO newsgroup I assume you are using and
ADODataSet to execute the SELECT statement. The code would be almost
the same if you use an ADOQuery.

// Assign a value to the parameter in the SELECT statement.
AdoDataSet1.Parameters.ParamByName('Param1').Value := TheListName'
// Execute the query.
AdoDataSet1.Open;
// Empty the combobox.
ComboBox1.Items.Clear;
// Loop through the records returned by the query and add the
// ListName value to the combobox.
while not AdoDataSet1.EOF do
begin
ComboBox1.Items.Add(AdoDataSet1.FieldByName('ListName').AsString);
AdoDataSet1.Next;
end;

--
Bill Todd (TeamB)
Back to top
Bill Todd
Guest





PostPosted: Mon Oct 09, 2006 5:10 am    Post subject: Re: SQL Query Help Reply with quote

Show us the actual code that you are using that does not work.

--
Bill Todd (TeamB)
Back to top
Grant
Guest





PostPosted: Mon Oct 09, 2006 5:18 am    Post subject: Re: SQL Query Help Reply with quote

Thanks for looking.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, DBCtrls, StdCtrls, Mask, DB, ADODB, Grids, DBGrids;

type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
ADOTable1: TADOTable;
DataSource1: TDataSource;
ADOTable2: TADOTable;
DataSource2: TDataSource;
ADOTable2ID: TAutoIncField;
ADOTable2ListName: TWideStringField;
DBGrid2: TDBGrid;
Label1: TLabel;
DBEdit1: TDBEdit;
Label2: TLabel;
DBEdit2: TDBEdit;
DBCheckBox1: TDBCheckBox;
Label3: TLabel;
DBEdit3: TDBEdit;
DBNavigator1: TDBNavigator;
Label4: TLabel;
DBEdit4: TDBEdit;
DBNavigator2: TDBNavigator;
Label5: TLabel;
ADOTable1ID: TAutoIncField;
ADOTable1Salutation: TWideStringField;
ADOTable1FirstName: TWideStringField;
ADOTable1Company: TWideStringField;
ADOTable1LastName: TWideStringField;
ADOTable1Address1: TWideStringField;
ADOTable1Address2: TWideStringField;
ADOTable1City: TWideStringField;
ADOTable1State: TWideStringField;
ADOTable1Zip: TIntegerField;
ADOTable1Country: TWideStringField;
ADOTable1Phone: TIntegerField;
ADOTable1Fax: TIntegerField;
ADOTable1DoNotSend: TBooleanField;
ADOTable1EMail: TWideStringField;
ADOTable1List: TWideStringField;
Label6: TLabel;
DBEdit5: TDBEdit;
DBComboBox1: TDBComboBox;
Query1: TADOQuery;
DataSource3: TDataSource;
Button1: TButton;
Edit1: TEdit;
Query1ID: TAutoIncField;
Query1ListName: TWideStringField;
Query1ListName2: TWideStringField;
Label7: TLabel;
DBEdit6: TDBEdit;
DBComboBox2: TDBComboBox;
Button2: TButton;
ComboBox1: TComboBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
Query1.Close; //A good thing to begin with closing the query
Query1.SQL.clear; //Clear the SQL

//Now we add the SQL statement (getting all the the info from
//the table InfoComputer that meet a specific os (param1)
Query1.sql.Add('select * from ListNames where ListName = :param1');

{Call ParseSQL to parse the SQL statement for parameters.

From Delphi HELP :
function ParseSQL(SQL: String; DoCreate: Boolean): String;

-SQL is a String containing the SQL statement to parse.
-DoCreate indicates whether to clear all existing parameter definitions
before parsing the SQL statement.
}
Query1.Parameters.ParseSQL(Query1.SQL.Text, True);

// Give a value to our parameter

Query1.Parameters.ParamByName('param1').value := Edit1.text;

//Open the query
Query1.open;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
// Assign a value to the parameter in the SELECT statement.
Query1.Parameters.ParamByName('Param1').Value := 'ListName';
// Execute the query.
Query1.Open;
// Empty the combobox.
ComboBox1.Items.Clear;
// Loop through the records returned by the query and add the
// ListName value to the combobox.
while not Query1.EOF do
begin
ComboBox1.Items.Add(Query1.FieldByName('ListName').AsString);
Query1.Next;


end;
end;
end.
"Grant" <grantfullen (AT) yahoo (DOT) com> wrote in message
news:45298a39 (AT) newsgroups (DOT) borland.com...
Quote:
Thanks for you patience and help. I am useing an ado.query. look below.

// Assign a value to the parameter in the SELECT statement.
Query1.Parameters.ParamByName('Param1').Value := 'ListName';
// Execute the query.
Query1.Open;
// Empty the combobox.
ComboBox1.Items.Clear;
// Loop through the records returned by the query and add the
// ListName value to the combobox.
while not Query1.EOF do
begin
ComboBox1.Items.Add(Query1.FieldByName('ListName').AsString);
Query1.Next;
I enter in a,b,c,d and when i run the app it just load the letter d into
the combobox.. I wornder is my sql statement correct. here it is.
select * from ListNames where ListName = :param1 .

I wornder if i need to alter the statmemt. Again I do not know SQL .

Thanks

Grant

"Bill Todd" <no (AT) no (DOT) com> wrote in message
news:45297c3c (AT) newsgroups (DOT) borland.com...
Ok. Since you posted in the ADO newsgroup I assume you are using and
ADODataSet to execute the SELECT statement. The code would be almost
the same if you use an ADOQuery.

// Assign a value to the parameter in the SELECT statement.
AdoDataSet1.Parameters.ParamByName('Param1').Value := TheListName'
// Execute the query.
AdoDataSet1.Open;
// Empty the combobox.
ComboBox1.Items.Clear;
// Loop through the records returned by the query and add the
// ListName value to the combobox.
while not AdoDataSet1.EOF do
begin
ComboBox1.Items.Add(AdoDataSet1.FieldByName('ListName').AsString);
AdoDataSet1.Next;
end;

--
Bill Todd (TeamB)

Back to top
Bill Todd
Guest





PostPosted: Mon Oct 09, 2006 6:20 am    Post subject: Re: SQL Query Help Reply with quote

What you posted is of no help at all. First, I do not need and did not
ask for the entire contents of the form's unit. I asked for the code
you are using that does not work. Instead, you posted two OnClick event
handlers. How am I supposed to know which one you are using? I will try
one last time. Please post:

1) Just the code you are executing that does not work.
2) The actual value you assign to the parameter in the query.
3) A clear description of what you want to happen and what actually
does (or does not) happen including the complete and exact text of any
error messages that are displayed.

--
Bill Todd (TeamB)
Back to top
Grant
Guest





PostPosted: Mon Oct 09, 2006 6:52 am    Post subject: Re: SQL Query Help Reply with quote

I'm using the code you gave me to populate the combobox with the ListName
fields in the table.

(1.) Here's the code:

// Assign a value to the parameter in the SELECT statement.
Query1.Parameters.ParamByName('Param1').Value := 'ListName';
// Execute the query.
Query1.Open;
// Empty the combobox.
ComboBox1.Items.Clear;
// Loop through the records returned by the query and add the
// ListName value to the combobox.
while not Query1.EOF do
begin
ComboBox1.Items.Add(Query1.FieldByName('ListName').AsString);
Query1.Next;
end;
end;

(2.) The parameter is select * from ListNames where ListName = :param1

(3.) Here's what I want to happen. Let's say I have 10 records, which will
be a,b,c,d,e,f,g,h,i,j. These will be in the ListNames field when you add a
record. When I click the button, I want to populate the combobox with all
the a,b,c, etc. records in the ListName field.

When I use the code you gave me, it only adds the last record in that field,
which would be j.

Thanks.
"Bill Todd" <no (AT) no (DOT) com> wrote in message
news:4529a3de$1 (AT) newsgroups (DOT) borland.com...
Quote:
What you posted is of no help at all. First, I do not need and did not
ask for the entire contents of the form's unit. I asked for the code
you are using that does not work. Instead, you posted two OnClick event
handlers. How am I supposed to know which one you are using? I will try
one last time. Please post:

1) Just the code you are executing that does not work.
2) The actual value you assign to the parameter in the query.
3) A clear description of what you want to happen and what actually
does (or does not) happen including the complete and exact text of any
error messages that are displayed.

--
Bill Todd (TeamB)
Back to top
Bill Todd
Guest





PostPosted: Mon Oct 09, 2006 5:48 pm    Post subject: Re: SQL Query Help Reply with quote

Grant wrote:

Quote:
Here's what I want to happen. Let's say I have 10 records, which
will be a,b,c,d,e,f,g,h,i,j. These will be in the ListNames field
when you add a record. When I click the button, I want to populate
the combobox with all the a,b,c, etc. records in the ListName field.

You cannot use a single parameter for a list of values. You could use

SELECT * FROM LISTNAMES WHERE LISTNAME IN ['A','B','C','D']

but you will have to build the SQL statement in code. You cannot use a
parameter for the list of values in the IN clause.

--
Bill Todd (TeamB)
Back to top
Grant
Guest





PostPosted: Tue Oct 10, 2006 2:27 am    Post subject: Re: SQL Query Help Reply with quote

Thanks very much for your help.
Grant
"Bill Todd" <no (AT) no (DOT) com> wrote in message
news:452a4502$1 (AT) newsgroups (DOT) borland.com...
Quote:
Grant wrote:

Here's what I want to happen. Let's say I have 10 records, which
will be a,b,c,d,e,f,g,h,i,j. These will be in the ListNames field
when you add a record. When I click the button, I want to populate
the combobox with all the a,b,c, etc. records in the ListName field.

You cannot use a single parameter for a list of values. You could use

SELECT * FROM LISTNAMES WHERE LISTNAME IN ['A','B','C','D']

but you will have to build the SQL statement in code. You cannot use a
parameter for the list of values in the IN clause.

--
Bill Todd (TeamB)
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (ADO) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.