Link to home
Start Free TrialLog in
Avatar of Grant Fullen
Grant Fullen

asked on

Ado Query, List View, Display Help, Access edit

Hi all I have a list view I use a adoquery that is conected to a access database. database name = mail.mdb. Table= MailInfo.  The code below displays any records based on the text in a combobox. It work great.

var
  colName: string;
begin
  colName:= cmbListName.Text; //Assigns the list name to colName from a combobox
  With QueryManageListView do
  begin
      Close;
      Parameters.ParamByName('pList').Value := colName;
      Open;
      ListView2.Items.Clear;      // Empty the ListView.
      // Loop through the records returned by the query and add the
      // EMail values to the ListView.
     while not EOF do
     begin
       ListView2.Items.Add;
       ListView2.Items[ListView2.Items.Count-1].Caption := FieldByName('Salutation').AsString;
       ListView2.Items[ListView2.Items.Count-1].SubItems.Add(FieldByName('FirstName').AsString);
       ListView2.Items[ListView2.Items.Count-1].SubItems.Add(FieldByName('LastName').AsString);
       ListView2.Items[ListView2.Items.Count-1].SubItems.Add(FieldByName('Company').AsString);
       ListView2.Items[ListView2.Items.Count-1].SubItems.Add(FieldByName('Address1').AsString);
       ListView2.Items[ListView2.Items.Count-1].SubItems.Add(FieldByName('Address2').AsString);
       ListView2.Items[ListView2.Items.Count-1].SubItems.Add(FieldByName('City').AsString);
       ListView2.Items[ListView2.Items.Count-1].SubItems.Add(FieldByName('State').AsString);
       ListView2.Items[ListView2.Items.Count-1].SubItems.Add(FieldByName('Zip').AsVariant);
       ListView2.Items[ListView2.Items.Count-1].SubItems.Add(FieldByName('Country').AsString);
       ListView2.Items[ListView2.Items.Count-1].SubItems.Add(FieldByName('Phone').AsString);
       ListView2.Items[ListView2.Items.Count-1].SubItems.Add(FieldByName('Fax').AsString);
       ListView2.Items[ListView2.Items.Count-1].SubItems.Add(FieldByName('Email').AsString);

       Next;
     end; { while not EOF do }
     Close;
  end; { With ADOQuery1 do }
I need to be able to doubleclick on the listview2 and be able to edit the info in the Mail.mdb data base.
Thanks for looking or helping.
Avatar of wimmeyvaert
wimmeyvaert

Maybe you can look opn the site : www.torry.ru and do a search for a DBListView and DBTreeView there.

Like: http://www.torry.net/quicksearchd.php?String=dbtreeview&Title=Yes

Best regards,

The Mayor.
First thing I would do is slightly restructure your code

var
  colName: string;
  lsti: TListItem;
begin
  colName:= cmbListName.Text; //Assigns the list name to colName from a combobox
  With QueryManageListView do
  begin
      Close;
      Parameters.ParamByName('pList').Value := colName;
      Open;
      ListView2.Items.Clear;      // Empty the ListView.
      // Loop through the records returned by the query and add the
      // EMail values to the ListView.
     while not EOF do
     begin
       lsti := ListView2.Items.Add;
       with lsti do
       begin
         Caption := FieldByName('Salutation').AsString;
         SubItems.Add(FieldByName('FirstName').AsString);
         SubItems.Add(FieldByName('LastName').AsString);
         SubItems.Add(FieldByName('Company').AsString);
         SubItems.Add(FieldByName('Address1').AsString);
         SubItems.Add(FieldByName('Address2').AsString);
         SubItems.Add(FieldByName('City').AsString);
         SubItems.Add(FieldByName('State').AsString);
         SubItems.Add(FieldByName('Zip').AsVariant);
         SubItems.Add(FieldByName('Country').AsString);
         SubItems.Add(FieldByName('Phone').AsString);
         SubItems.Add(FieldByName('Fax').AsString);
         SubItems.Add(FieldByName('Email').AsString);
       end;
       Next;
     end; { while not EOF do }
     Close;
  end; { With ADOQuery1 do }


My next question is, what do you want a double click on the listview to actually do.
I know you want to edit the database, but what do you expect to happen.
Avatar of Grant Fullen

ASKER

Well i have a form named form2. It is a form where you can enter in or edit the record. SO when you click on the highlighted or selected row in the list view it will open form 2 with the current record you seleced in the listview.
Thanks
Grant
Create another TAdoQuery.

When adding items also add
SubItems.Add(FieldByName('ID').AsString);

Where that is the primary key (auto increment). That col doesn't have to display.

On the second AdoQuery have your sql like

SELECT * FROM MailInfo WHERE ID=:SID

Then when you select the item in the listview
set the Parameter of SID of the second TAdo with the  StrToInt(ListView2.items[listview2.selected.item].subitems[12])
then open the database
load all the data from the result of your query and show the form.

That help?
ASKER CERTIFIED SOLUTION
Avatar of wildzero
wildzero

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks alot that works great.......
No problem
also note that you should carry over the ID onto the second form (in a hidden label  / global variable) then when you edit the edit boxes of data you can call

UPDATE MailInfo SET {fields + data} WHERE ID=:SID
Where SID is a parameter set by say,  StrToInt(lbid.caption)

:D