#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Access violation with master/detail


    Hello people

    I need some help, hope someone will be able to tell me what am I doing wrong.

    My *.mdb database have several tables. I have dbGrid that is populated from table tblUnits using ADO Query. I made Add record, Update, Delete options and everything was working fine at this point.

    Later I turned that dbGrid to be master control. I put another dataset component and ADO table that is linked with tblRenter table from database. Some controls are placed to show Renter informations. Master/detail relation is set, ADO table MasterFields is RenterName. Master/detail showing data works excellent.

    So what is problem? Delete record option working fine, but when I try to add new record or update existing in tblUnits table (using ADO query), I get access violation error.

    Here is my Update record code:

    Code:
    procedure TfrmUpdateUnits.btnUpdateClick(Sender: TObject); 
    var 
       MyPoint: TBookmarkStr;
    begin 
       MyPoint:= frmMain.queADO.Bookmark;   
       frmMain.tblRenter.DisableControls; 
       frmMain.queADO.DisableControls; 
       frmMain.queADO.SQL.Clear; 
       frmMain.queADO.SQL.Text:= 'SELECT * FROM tblUnits ORDER BY ComplexName'; 
       frmMain.queADO.Open;
       frmMain.queADO.Edit; 
       frmMain.queADO.FieldByName('NumOfBeds').Value := Trim(edtBeds.Text); 
       frmMain.queADO.FieldByName('Price').Value := Trim(edtPrice.Text); 
       frmMain.queADO.FieldByName('AirConditioner').Value := chkAir.Checked; 
       frmMain.queADO.FieldByName('CableTV').Value := chkCableTV.Checked; 
       ...
       frmMain.queADO.Post; 
       frmMain.queADO.EnableControls; 
       frmMain.stbStatus.Panels[0].Text:= 'Number of records matching selected criteria is: ' + IntToStr(frmmain.queADO.RecordCount); 
       frmMain.queADO.Bookmark:= MyPoint; 
       frmMain.tblRenter.EnableControls;
       Close; 
    end;
    I would really appreciate if someone can help me with this.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    151
    Rep Power
    2
    Which line gives you the access violation?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    Thanks for reply.

    I tested it again. It apperas that line:

    Code:
    frmMain.queADO.EnableControls;
    making problem. I can't understand why.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    151
    Rep Power
    2
    In that case, your problem was related with db-aware controls. Try to disconnect all db-aware controls and connect them one by one when testing.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    344
    Rep Power
    6
    You are attempting to get a bookmark on a query that is (presumably) not open yet.
    Code:
       MyPoint:= frmMain.queADO.Bookmark;   
       frmMain.tblRenter.DisableControls; 
       frmMain.queADO.DisableControls; 
       frmMain.queADO.SQL.Clear; 
       frmMain.queADO.SQL.Text:= 'SELECT * FROM tblUnits ORDER BY ComplexName'; 
       frmMain.queADO.Open;
    You start by defining MyPoint, then 4 lines later you define the query, and then open it.

    A Bookmark can only be set on an open query/table since its purpose is to set a pointer to the currently selected record. If the query isn't open, then there aren't any records, let alone a selected one.

    Also

    Bookmarks can cause access violations if the reference to where they point to is no longer valid.

    When I want to use bookmarks, I always code it as:
    Code:
    var
       MyPoint: TBookMark;
       //TBookmarkStr is deprecated in newer Delphi's
    begin
       frmMain.queADO.SQL.Clear; 
       frmMain.queADO.SQL.Text:= 'SELECT * FROM tblUnits ORDER BY ComplexName'; 
       frmMain.queADO.Open;
       MyPoint := frmMain.queADO.GetBookMark;
       try
          frmMain.tblRenter.DisableControls;
          frmMain.queADO.DisableControls;
          try
             frmMain.queADO.Edit; 
             frmMain.queADO.FieldByName('NumOfBeds').Value := Trim(edtBeds.Text); 
             frmMain.queADO.FieldByName('Price').Value := Trim(edtPrice.Text); 
             frmMain.queADO.FieldByName('AirConditioner').Value := chkAir.Checked; 
             frmMain.queADO.FieldByName('CableTV').Value := chkCableTV.Checked; 
             ...
             frmMain.queADO.Post; 
             try
                frmMain.queADO.GotoBookMark(MyPoint);
             except
                //catch GotoBookMark error here.
             end;
          finally
             frmMain.tblRenter.EnabledControls;
             frmMain.queADO.EnabledControls;//should be in its own try..finally..
          end;
       finally
          frmMain.queADO.FreeBookMark(MyPoint);
       end;
    end;
    Last edited by majlumbo; October 16th, 2012 at 10:55 AM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    Thanks for replies. I replaced query from main form with ado table and works perfectly now. I will use query on other forms when needed.

    Thanks again for your effort.

IMN logo majestic logo threadwatch logo seochat tools logo