Solved

Dataset not in edit or insert mode

Posted on 2010-08-18
7
1,681 Views
Last Modified: 2012-06-27
Hello,

I have this procedure:

procedure TMainForm.utfSchedMgrPostAppt(Sender: TObject; Appt: TJvTFAppt);
var
  I : Integer;
begin
  With GetApptQuery do
    Begin
      FieldByName('ApptID').AsString := Appt.ID;
      Open;                   <==========================
      If RecordCount > 0 Then
        Edit
      Else
        Begin
          Insert;
          FieldByName('ApptID').AsString := Appt.ID;
        End;
      FieldByName('StartDate').AsDateTime := Appt.StartDate;
      FieldByName('StartTime').AsDateTime := Appt.StartTime;
      FieldByName('EndDate').AsDateTime := Appt.EndDate;
      FieldByName('EndTime').AsDateTime := Appt.EndTime;
      FieldByName('Description').AsString := Appt.Description;
      FieldByName('AlarmEnabled').AsBoolean := Appt.AlarmEnabled;
      FieldByName('AlarmAdvance').AsInteger := Appt.AlarmAdvance;
      Post;
      Close;
    End;
  With DeleteApptLinkQuery do
    Begin
      FieldByName('ApptID').AsString := Appt.ID;
      ExecSQL;
    End;
  With ApptSchedulesQuery do
    Begin
      FieldbyName('ApptID').AsString := Appt.ID;
      Open;
      For I := 0 to Appt.ScheduleCount - 1 do
        Begin
          Insert;
          FieldByName('ApptID').AsString := Appt.ID;
          FieldByName('SchedName').AsString := Appt.Schedules[I];
          Post;
        End;
      Close;
    End;
end;

Open in new window


I get an error: Dataset not in edit or insert mode
at the line where I put the arrow.

It's a procedure that calls for 3 ADOQuery's: GetApptQuery, DeleteApptLinkQuery  and
ApptSchedulesQuery. But at the line where I put the arrow I get the error-message.
Just in case I have put the whole unit in the code-section.

Who knows the answer and is willing to help me?

Greetings,

Peter Kiers
type

  TMainForm = class(TForm)

    ADOConnection: TADOConnection;

    ADOTable1: TADOTable;

    ADOTable2: TADOTable;

    ADOTable3: TADOTable;

    DataSource1: TDataSource;

    DataSource2: TDataSource;

    DataSource3: TDataSource;

    NeedApptsQuery: TADOQuery;

    ApptSchedulesQuery: TADOQuery;

    GetApptQuery: TADOQuery;

    DeleteApptLinkQuery: TADOQuery;

    DeleteApptQuery: TADOQuery;

    SchedulesQuery: TADOQuery;

    TFDays: TJvTFDays;

    utfSchedMgr: TJvTFScheduleManager;

    tbEdtAppt: TButton;

    tbNewAppt: TButton;

    Button3: TButton;

    GetApptQueryApptID: TWideStringField;

    procedure tbEdtApptClick(Sender: TObject);

    procedure tbNewApptClick(Sender: TObject);

    procedure TFDaysDblClick(Sender: TObject);

    procedure FormCreate(Sender: TObject);

    procedure utfSchedMgrRefreshAppt(Sender: TObject; Appt: TJvTFAppt);

    procedure utfSchedMgrPostAppt(Sender: TObject; Appt: TJvTFAppt);

    procedure utfSchedMgrLoadBatch(Sender: TObject; BatchName: string;

      BatchStartDate, BatchEndDate: TDate);

    procedure utfSchedMgrDeleteAppt(Sender: TObject; Appt: TJvTFAppt);

  private

    { Private declarations }

    DBPath: string;

    DBName: string;

  public

    { Public declarations }

  end;



var

  MainForm: TMainForm;



implementation



uses ApptEdit;



{$R *.dfm}



procedure TMainForm.FormCreate(Sender: TObject);

var

  conn_str:string;

begin

  DBPath := IncludeTrailingPathDelimiter(ExtractFilePath(Application.Exename));

  DBName := 'DBRoot.mdb';

  conn_str :=

    Format('provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=%s%s;',

    [IncludeTrailingPathDelimiter(DBPath), DBName]);

   ADOConnection.Connected := False;

   ADOConnection.ConnectionString := conn_str;

   ADOConnection.Connected := True;

   ADOTable1.Open;

   ADOTable2.Open;

   ADOTable3.Open;

end;



procedure TMainForm.TFDaysDblClick(Sender: TObject);

begin

  With TFDays do

    If ValidSelection Then

      If Assigned(SelAppt) Then

        tbEdtApptClick(nil)

      Else

        tbNewApptClick(nil);

end;



procedure TMainForm.tbEdtApptClick(Sender: TObject);

begin

  If Assigned(TFDays.SelAppt) Then

    Begin

      frmApptEdit.Appt := TFDays.SelAppt;

      frmApptEdit.ShowModal;

    End

  Else

    MessageDlg('Please select an appointment to edit.', mtInformation,

               [mbOK], 0);

end;



procedure TMainForm.tbNewApptClick(Sender: TObject);

begin

  frmApptEdit.ShowModal;

end;



procedure TMainForm.utfSchedMgrDeleteAppt(Sender: TObject; Appt: TJvTFAppt);

begin

  With DeleteApptQuery do

    Begin

      FieldByName('ApptID').AsString := Appt.ID;

      ExecSQL;

    End;

  With DeleteApptLinkQuery do

    Begin

      FieldByName('ApptID').AsString := Appt.ID;

      ExecSQL;

    End;

end;



procedure TMainForm.utfSchedMgrLoadBatch(Sender: TObject; BatchName: string;

  BatchStartDate, BatchEndDate: TDate);

var

  Appt : TJvTFAppt;

  NewAppt : Boolean;

begin

  With NeedApptsQuery do

    Begin

      FieldByName('D1').AsDateTime := BatchStartDate;

      FieldByName('D2').AsDateTime := BatchEndDate;

      FieldByName('SchedName').AsString := BatchName;

      Open;

      First;

      While not EOF do

        Begin

          utfSchedMgr.RequestAppt(FieldByName('ApptID').AsString,

            Appt, NewAppt);

          If NewAppt Then

            Begin

              Appt.SetStartEnd(FieldByName('StartDate').AsDateTime,

                               FieldByName('StartTime').AsDateTime,

                               FieldByName('EndDate').AsDateTime,

                               FieldByName('EndTime').AsDateTime);

              Appt.Description := FieldByName('Description').AsString;

              Appt.AlarmEnabled := FieldByName('AlarmEnabled').AsBoolean;

              Appt.AlarmAdvance := FieldByName('AlarmAdvance').AsInteger;

              With ApptSchedulesQuery do

                Begin

                  FieldByName('ApptID').AsString := Appt.ID;

                  Open;

                  First;

                  While not EOF do

                    Begin

                      Appt.AddSchedule(FieldByName('SchedName').AsString);

                      Next;

                    End;

                  Close;

                End;

            End;

          Next;

        End;

      Close;

    End;

end;



procedure TMainForm.utfSchedMgrPostAppt(Sender: TObject; Appt: TJvTFAppt);

var

  I : Integer;

begin

  With GetApptQuery do

    Begin

      FieldByName('ApptID').AsString := Appt.ID;

      Open;

      If RecordCount > 0 Then

        Edit

      Else

        Begin

          Insert;

          FieldByName('ApptID').AsString := Appt.ID;

        End;

      FieldByName('StartDate').AsDateTime := Appt.StartDate;

      FieldByName('StartTime').AsDateTime := Appt.StartTime;

      FieldByName('EndDate').AsDateTime := Appt.EndDate;

      FieldByName('EndTime').AsDateTime := Appt.EndTime;

      FieldByName('Description').AsString := Appt.Description;

      FieldByName('AlarmEnabled').AsBoolean := Appt.AlarmEnabled;

      FieldByName('AlarmAdvance').AsInteger := Appt.AlarmAdvance;

      Post;

      Close;

    End;

  With DeleteApptLinkQuery do

    Begin

      FieldByName('ApptID').AsString := Appt.ID;

      ExecSQL;

    End;

  With ApptSchedulesQuery do

    Begin

      FieldbyName('ApptID').AsString := Appt.ID;

      Open;

      For I := 0 to Appt.ScheduleCount - 1 do

        Begin

          Insert;

          FieldByName('ApptID').AsString := Appt.ID;

          FieldByName('SchedName').AsString := Appt.Schedules[I];

          Post;

        End;

      Close;

    End;

end;



procedure TMainForm.utfSchedMgrRefreshAppt(Sender: TObject; Appt: TJvTFAppt);

begin

  With GetApptQuery do

    Begin

      FieldByName('ApptID').AsString := Appt.ID;

      Open;

      If RecordCount = 1 Then

        Begin

          Appt.SetStartEnd(FieldByName('StartDate').AsDateTime,

                           FieldByName('StartTime').AsDateTime,

                           FieldByName('EndDate').AsDateTime,

                           FieldByName('EndTime').AsDateTime);

          Appt.Description := FieldByName('Description').AsString;

          Appt.AlarmEnabled := FieldByName('AlarmEnabled').AsBoolean;

          Appt.AlarmAdvance := FieldByName('AlarmAdvance').AsInteger;

        End;

      Close;

    End;

  Appt.ClearSchedules;

  With ApptSchedulesQuery do

    Begin

      FieldByName('ApptID').AsString := Appt.ID;

      Open;

      First;

      While not EOF do

        Begin

          Appt.AddSchedule(FieldByName('SchedName').AsString);

          Next;

        End;

      Close;

    End;

end;



end.

Open in new window

0
Comment
Question by:peterkiers
  • 4
  • 3
7 Comments
 
LVL 13

Expert Comment

by:aflarin
ID: 33462996
Hi Peter,

Maybe it's a typo? ParamByName instead of FieldByName?

procedure TMainForm.utfSchedMgrPostAppt(Sender: TObject; Appt: TJvTFAppt);
var
    I : Integer;
begin
   With GetApptQuery do
     Begin
        ParamByName('ApptID').AsString := Appt.ID;
        Open;                   <==========================
    ...


0
 
LVL 13

Expert Comment

by:aflarin
ID: 33463010
The same:

With DeleteApptLinkQuery do
Begin
       ParamByName('ApptID').AsString := Appt.ID;
       ExecSQL;
    End;
  With ApptSchedulesQuery do
    Begin
       ParamByName('ApptID').AsString := Appt.ID;
       Open;



0
 
LVL 1

Author Comment

by:peterkiers
ID: 33463011
The code came from a TQuery that I have changed to TADOQuery
The only thing I had to change is the ParamByName to FieldByName
cause ParamByName is not recognized by TADOQuery.

Peter
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Accepted Solution

by:
aflarin earned 500 total points
ID: 33463033
Use Parameters.ParamByName:

procedure TMainForm.utfSchedMgrPostAppt(Sender: TObject; Appt: TJvTFAppt);
var
  I : Integer;
begin
  With GetApptQuery do
    Begin
      Parameters.ParamByName('ApptID').AsString := Appt.ID;
      Open;                   <==========================
      If RecordCount > 0 Then
        Edit
      Else
        Begin
          Insert;
          FieldByName('ApptID').AsString := Appt.ID;
        End;
      FieldByName('StartDate').AsDateTime := Appt.StartDate;
      FieldByName('StartTime').AsDateTime := Appt.StartTime;
      FieldByName('EndDate').AsDateTime := Appt.EndDate;
      FieldByName('EndTime').AsDateTime := Appt.EndTime;
      FieldByName('Description').AsString := Appt.Description;
      FieldByName('AlarmEnabled').AsBoolean := Appt.AlarmEnabled;
      FieldByName('AlarmAdvance').AsInteger := Appt.AlarmAdvance;
      Post;
      Close;
    End;
  With DeleteApptLinkQuery do
    Begin
      Parameters.ParamByName('ApptID').AsString := Appt.ID;
      ExecSQL;
    End;
  With ApptSchedulesQuery do
    Begin
      Parameters.ParamByName('ApptID').AsString := Appt.ID;
      Open;
      For I := 0 to Appt.ScheduleCount - 1 do
        Begin
          Insert;
          FieldByName('ApptID').AsString := Appt.ID;
          FieldByName('SchedName').AsString := Appt.Schedules[I];
          Post;
        End;
      Close;
    End;
end;
0
 
LVL 13

Expert Comment

by:aflarin
ID: 33463053
and Value instead of AsString:

procedure TMainForm.utfSchedMgrPostAppt(Sender: TObject; Appt: TJvTFAppt);
var
  I : Integer;
begin
  With GetApptQuery do
    Begin
      Parameters.ParamByName('ApptID').Value := Appt.ID;
      Open;                  
      If RecordCount > 0 Then
        Edit
      Else
        Begin
          Insert;
          FieldByName('ApptID').AsString := Appt.ID;
        End;
      FieldByName('StartDate').AsDateTime := Appt.StartDate;
      FieldByName('StartTime').AsDateTime := Appt.StartTime;
      FieldByName('EndDate').AsDateTime := Appt.EndDate;
      FieldByName('EndTime').AsDateTime := Appt.EndTime;
      FieldByName('Description').AsString := Appt.Description;
      FieldByName('AlarmEnabled').AsBoolean := Appt.AlarmEnabled;
      FieldByName('AlarmAdvance').AsInteger := Appt.AlarmAdvance;
      Post;
      Close;
    End;
  With DeleteApptLinkQuery do
    Begin
      Parameters.ParamByName('ApptID').Value := Appt.ID;
      ExecSQL;
    End;
  With ApptSchedulesQuery do
    Begin
      Parameters.ParamByName('ApptID').Value := Appt.ID;
      Open;
      For I := 0 to Appt.ScheduleCount - 1 do
        Begin
          Insert;
          FieldByName('ApptID').AsString := Appt.ID;
          FieldByName('SchedName').AsString := Appt.Schedules[I];
          Post;
        End;
      Close;
    End;
end;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33463187
Oke, it continues but stops at:
FieldByName('StartDate').AsDateTime := Appt.StartDate;

I quess Fieldbyname has to be Parameters.ParamByName but
wath about .asdatetime ???

P.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33463209
I got it. Thanks for the info.
500 points are comming to you...

PK
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Thread safe  opinion 7 126
error 1.1 400 Bad request idhttp delphi 18 52
Create Database on Android via Delphi dbExpress 3 78
Delphi selector screen 2 69
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now