Jump to content

PrimeMinister

Members
  • Content Count

    7
  • Joined

  • Last visited

Posts posted by PrimeMinister


  1. Hey guys, so for this program I'm writing I want to check a string grid for empty rows/rows with just spaces in them and then subsequently deleting them to clean the grids up a bit.

    I made a procedure that reads the fields from a specific table in a DB and into a corresponding string grid. While 13 of the 14 grids I have, read the records like they're supposed to from the DB, one single table doesn't read correctly, it reads the same amount of records again as empty rows when displayed on the grid.

    For example, let's say I have 3 records in the table in the DB and I read it into the grid, the records display properly but then they add 3 more rows with just spaces in them.

     

    My code for this so far:

    procedure TfrmPuntehou.IncreaseRowCount(grid: TStringGrid);
    begin
      //increases rows in grid
      grid.RowCount:= grid.RowCount+1;
    end;
    
    procedure TfrmPuntehou.WriteToList(tbl: TADOTable;grid:TStringGrid);
    var
      Row: Integer;
      i: Integer;
    begin
    
      tbl.Active:= True;
    
      Row := 1;
    
      // Populate cells
      Tbl.First;
      while not (Tbl.Eof) do
      begin
        grid.Cells[0,Row]:= IntToStr(Row);
        grid.Cells[1,Row]:= tbl.fields[0].AsString;
        grid.Cells[2,Row]:= tbl.fields[1].AsString;
        grid.Cells[3,Row]:= tbl.fields[2].AsString;
        Inc(Row);
        IncreaseRowCount(grid);
        Tbl.Next;
      end;
      tbl.Active:=false;
    
      //what I'm planning to do to alleviate this but I'm not sure to use in the conditions of the if
      // because if I say grid.Rows[i] = ''/grid.Rows[i] = ' ' then I get an error
    //  for i := 1 to grid.RowCount - 1 do
    //  begin
    //    if () OR () then
    //    begin
    //      grid.Rows[i].Delete;
    //    end;
    //  end;
    end;

    Illustration of what I'm talking about:

     

    What is supposed to happen:

    success.thumb.PNG.eb70ee95b49d239252fac34dbcc048ab.PNG

     

    What's happening (the records are inserted correctly, but there are unnecessary rows here for only 3 records. When exported to a CSV the empty rows are filled with spaces"):

    fail.thumb.PNG.3976edbbc8be86db9bc306d70323f38c.PNG

     

    Thanks in advance for any help on this!

     

    Kind Regards

    PrimeMinister

     


  2. 8 minutes ago, Anders Melander said:

    MyCmd isn't initialized so that's probably the cause of the AV. You need to create an instance of TADOCommand or reference an existing instance. Get rid of the local var declaration if you meant to be using the TADOCommand with the same name on the form

     

    Apart from that you haven't told us where in your code the problem occurs.

    It was on this line

    MyCmd.CommandText := 'Delete * from '+tablename;

    So I changed my code to this:
       

    //clears entire database
        with dmRacers do
        begin
    
          MyCmd := TADOCommand.Create(Self);
          try
          tbl1660.Active := true;
    
          MyCmd.CommandText := 'Delete * from tbl1660';
          MyCmd.Execute;
    
          tbl1660.Active := false;
            {EmptyTable(tbl1660,'tbl1660');
            EmptyTable(tblXKarts,'tblXKarts');
            EmptyTable(tblTwoPointOne,'tblTwoPointOnes');
            EmptyTable(tblMidB,'tblMidB');
            EmptyTable(tblMidA,'tblMidA');
            EmptyTable(tblLateModel,'tblLateModels');
            EmptyTable(tblSprints,'tblSprints');
            EmptyTable(tblV8,'tblV8');
            EmptyTable(tblHeavyMetals,'tblHeavyMetals');
            EmptyTable(tblHotrods,'tblHotrods');
            EmptyTable(tblPinkrods,'tblPinkrods');
            EmptyTable(tblStockrods,'tblStockrods');
            EmptyTable(tblMinis,'tblMinis');
            EmptyTable(tblDevelopment,'tblDevelopment');}
          finally
            MyCmd.Free;
          end;

    But now the error is "Missing Connection or ConnectionString".

     

    I don't know what to do anymore...


  3. Hey guys, so I'm having a problem with my ADO Tables. I have a button in my program that is meant to clear all tables in the database related to the program when clicked but whenever I click it, it gives errors. (no matter how many times I change my code) Could it be a problem with my Data Module or what? (I'm not sure cause my button which saves stuff to the database, works perfectly without a hitch) How could I alleviate this?

     

    Code of my data module:

       const
      scConnectionString = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%pathtomdb%Racers.mdb;Mode=ReadWrite;Persist Security Info=False;';
    var
      dmRacers: TdmRacers;
    
    implementation
    
    {%CLASSGROUP 'Vcl.Controls.TControl'}
    
    {$R *.dfm}
    
    procedure TdmRacers.DataModuleCreate(Sender: TObject);
    var
    path:string;
    begin
      path:=ExtractFilePath(ParamStr(0));
      conToDB.ConnectionString := StringReplace(scConnectionString, '%pathtomdb%', path, []);
      conToDB.Connected:=True;
      tbl1660.Active := True;
      tblXKarts.Active := True;
      tblTwoPointOne.Active := True;
      tblMidB.Active := True;
      tblMidA.Active := True;
      tblLateModel.Active := True;
      tblSprints.Active := True;
      tblV8.Active := True;
      tblHeavyMetals.Active := True;
      tblHotrods.Active := True;
      tblPinkrods.Active := True;
      tblStockrods.Active := True;
      tblMinis.Active := True;
      tblDevelopment.Active := True;
    end;

    Code from the form:

     public
        { Public declarations }
        MyCmd:TADOCommand;
        procedure EmptyTable(tbl:TADOTable;tablename:string);
        procedure ResetActiveProp(tbl:TADOTable);
      end;
    
    var
      frmEntry: TfrmEntry;
    
    implementation
    
    {$R *.dfm}
    
    procedure TfrmEntry.ResetActiveProp(tbl: TADOTable);
    begin
      tbl.Active := True;
    end;
    
    procedure TfrmEntry.EmptyTable(tbl: TADOTable; tablename: string);
    var
      MyCmd: TADOCommand;
    begin
        tbl.Active := true;
    
        MyCmd.CommandText := 'Delete * from '+tablename;
        MyCmd.Execute;
    
        tbl.Active := false;
    end;
    
    procedure TfrmEntry.bmbClearClick(Sender: TObject);
    var
      i:integer;
    begin
      cbxGridSelect.ItemIndex:=-1;
      cbxGridSelect.Text:='Select a class';
      edtRacerName.Clear;
      edtCarNumber.Clear;
      edtLicenseNum.Clear;
      i:=MessageDlg('Dialog cleared',mtInformation,[mbOK],0);
    end;
    
    procedure TfrmEntry.bmbClearDBClick(Sender: TObject);
    var
      i:integer;
    begin
      i:=MessageDlg('Are you sure you want to clear the Racers database? (all current data in the database will be lost.)',mtWarning,[mbOK,mbCancel],0);
      if i = mrOk then
      begin
        //clears entire database
        with dmRacers do
        begin
    
          EmptyTable(tbl1660,'tbl1660');
          EmptyTable(tblXKarts,'tblXKarts');
          EmptyTable(tblTwoPointOne,'tblTwoPointOnes');
          EmptyTable(tblMidB,'tblMidB');
          EmptyTable(tblMidA,'tblMidA');
          EmptyTable(tblLateModel,'tblLateModels');
          EmptyTable(tblSprints,'tblSprints');
          EmptyTable(tblV8,'tblV8');
          EmptyTable(tblHeavyMetals,'tblHeavyMetals');
          EmptyTable(tblHotrods,'tblHotrods');
          EmptyTable(tblPinkrods,'tblPinkrods');
          EmptyTable(tblStockrods,'tblStockrods');
          EmptyTable(tblMinis,'tblMinis');
          EmptyTable(tblDevelopment,'tblDevelopment');
    
          i:=MessageDlg('Database successfully cleared',mtInformation,[mbOk],0);
    
          //resets active property of tables
          ResetActiveProp(tbl1660);
          ResetActiveProp(tblXKarts);
          ResetActiveProp(tblTwoPointOne);
          ResetActiveProp(tblMidB);
          ResetActiveProp(tblMidA);
          ResetActiveProp(tblLateModel);
          ResetActiveProp(tblSprints);
          ResetActiveProp(tblV8);
          ResetActiveProp(tblHeavyMetals);
          ResetActiveProp(tblHotrods);
          ResetActiveProp(tblPinkrods);
          ResetActiveProp(tblStockrods);
          ResetActiveProp(tblMinis);
          ResetActiveProp(tblDevelopment);
        end;
      end
      else
      begin
        i:=MessageDlg('Clear aborted',mtInformation,[mbOk],0);
      end;
    end;

    Example of errors:

    error.JPG.c5cb123cd2765594842517df400a4356.JPG

    acccess.JPG.cf33c182ef64bb0f2a9664a1c9c63132.JPG

     

    Thanks in advance for the help!

    Kind Regards

    PrimeMinister

×