Solved

Making My Own Procedure

Posted on 2007-03-18
8
150 Views
Last Modified: 2010-05-18
I'm making a procedure that inserts records to a .mdb access database table.  It looks something like:

procedure AddInfo(sTable, sField, sValue: String);
var
  CustomerInfo: TAdoQuery;
begin
  CustomerInfo := TAdoQuery.create(nil);
  CustomerInfo.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=clean.mdb;Persist Security Info=False';
  CustomerInfo.Sql.Text := 'INSERT INTO ' + sTable + '(' + sField + ') VALUES (:s)';
  CustomerInfo.RecNo:= CustomerInfo.RecordCount
  ShowMessage(CustomerInfo.SQL.Text);
  CustomerInfo.Parameters.ParamByName('s').Value:= sValue;
  CustomerInfo.ExecSQL;
end;

This works fine but it only adds one value to one field per record.  I need to be able to have more than one parameter for the field and value.  I can't just say sValue1, sValue2, etc. : string because I won't know how many values I'm adding.
0
Comment
Question by:Grant Fullen
  • 4
  • 4
8 Comments
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 18745725
const
  expectedFieldDelimiter = #28; // FS 28 = Field Separator, so is a natural choice


procedure AddInfo(sTable: string; sFields, sValues: String);
var
  sFieldsSL, sValuesSL: TStringList;
  CustomerInfo: TAdoQuery;
  i: integer;
  sqltext: string;
begin
  sFieldsSL := TStringList.Create;
  sValuesSL := TStringList.Create;
  try
    sFieldsSL.Text := StringReplace(sFields, expectedFieldDelimiter, #13#10, [rfReplaceAll]);
    sValuesSL.Text := StringReplace(sValues, expectedFieldDelimiter, #13#10, [rfReplaceAll]);

    CustomerInfo := TAdoQuery.create(nil);
    CustomerInfo.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=clean.mdb;Persist Security Info=False';
    sqltext := 'INSERT INTO ' + sTable + '(';

    for i := 0 to pred(sFieldsSL.Count) do sqltext := sqltext + sFieldsSL[i] + ',';
    delete(sqltext, length(sqltext), 1); // remove final ','

    sqltext := sqltext + '') VALUES (';

    for i := 0 to pred(sFieldsSL.Count) do sqltext := sqltext + ':' + sFieldsSL[i] + ',';
    delete(sqltext, length(sqltext), 1); // remove final ','

    CustomerInfo.Sql.Text := sqltext;

    CustomerInfo.RecNo:= CustomerInfo.RecordCount
    ShowMessage(CustomerInfo.SQL.Text);

    for i := 0 to pred(sFieldsSL.Count) do CustomerInfo.Parameters.ParamByName(sFieldsSL[i]).Value := sValuesSL[i];

    CustomerInfo.ExecSQL;

  finally
    sFieldsSL.Free;
    sValuesSL.Free;
  end;
end;  

call it like
  AddInfo('Table1',
          'Field1' + #28 + 'Field2' + #28 + 'Field3',
          'Value1' + #28 + 'Value2' + #28 + 'Value3');

0
 

Author Comment

by:Grant Fullen
ID: 18745841
This is perfect.  Only other thing I would like is to be able to choose whether the value you're inserting is boolean, string, date, etc.

I'm upping the points to 500 if you could just do that.

Thanks.
0
 

Author Comment

by:Grant Fullen
ID: 18746010
If I changed sValues : string to sValues : Variant would that work?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 17

Accepted Solution

by:
TheRealLoki earned 500 total points
ID: 18746024
well... this isn't the best way to do it really, but i'll expand on what i've posted so you get an idea

const
  expectedFieldDelimiter = #28; // FS 28 = Field Separator, so is a natural choice

  mft_String  = 'S';
  mft_Bool    = 'B';
  mft_Date    = 'D';
  mft_Time    = 'T';
  mft_DateTime= '@';

procedure AddInfo(sTable: string; sFields, sValues, sDataTypes: String);
var
  sFieldsSL, sValuesSL, sDataTypesSL: TStringList;
  CustomerInfo: TAdoQuery;
  i: integer;
  sqltext: string;
  s: string;
begin
  sFieldsSL := TStringList.Create;
  sValuesSL := TStringList.Create;
  sDataTypesSL := TStringList.Create;
  try
    sFieldsSL.Text := StringReplace(sFields, expectedFieldDelimiter, #13#10, [rfReplaceAll]);
    sValuesSL.Text := StringReplace(sValues, expectedFieldDelimiter, #13#10, [rfReplaceAll]);
    sDataTypesSL.Text := StringReplace(sDataTypes, expectedFieldDelimiter, #13#10, [rfReplaceAll]);
    CustomerInfo := TAdoQuery.create(nil);
    CustomerInfo.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=clean.mdb;Persist Security Info=False';
    sqltext := 'INSERT INTO ' + sTable + '(';

    for i := 0 to pred(sFieldsSL.Count) do sqltext := sqltext + sFieldsSL[i] + ',';
    delete(sqltext, length(sqltext), 1); // remove final ','

    sqltext := sqltext + ') VALUES (';

    for i := 0 to pred(sFieldsSL.Count) do sqltext := sqltext + ':' + sFieldsSL[i] + ',';
    delete(sqltext, length(sqltext), 1); // remove final ','

    sqltext := sqltext + ')';

    CustomerInfo.Sql.Text := sqltext;

    CustomerInfo.RecNo:= CustomerInfo.RecordCount

    ShowMessage(CustomerInfo.SQL.Text);

    for i := 0 to pred(sFieldsSL.Count) do
    begin
      s := sValuesSL[i];
      if sDataTypes[i] = mft_Bool then
      begin
        CustomerInfo.Parameters.ParamByName(sFieldsSL[i]).DataType := ftBoolean;
        CustomerInfo.Parameters.ParamByName(sFieldsSL[i]).Value := ( (s <> '') and ( (s[1] = 't') or (s[1] = 'T') or (s[1] = '1') or (s[1] = 'y') or (s[1] = 'Y') ) );
      end
      else if sDataTypes[i] = mft_Date then
      begin // expected date format (to be generic is always dd-mmm-yyyy, change to whatever you like)
        CustomerInfo.Parameters.ParamByName(sFieldsSL[i]).DataType := ftDateTime;
        CustomerInfo.Parameters.ParamByName(sFieldsSL[i]).Value := s;
      end
      else if sDataTypes[i] = mft_Time then
      begin
        CustomerInfo.Parameters.ParamByName(sFieldsSL[i]).DataType := ftDateTime;
        CustomerInfo.Parameters.ParamByName(sFieldsSL[i]).Value := s;
      end
      else if sDataTypes[i] = mft_DateTime then
      begin // expected time format (to be generic is always dd-mmm-yyyy hh:nn:ss.zzz, change to whatever you like)
        CustomerInfo.Parameters.ParamByName(sFieldsSL[i]).DataType := ftDateTime;
        CustomerInfo.Parameters.ParamByName(sFieldsSL[i]).Value := s;
      end
      else
        CustomerInfo.Parameters.ParamByName(sFieldsSL[i]).Value := s;
    end;

    CustomerInfo.ExecSQL;

  finally
    sFieldsSL.Free;
    sValuesSL.Free;
    sDataTypesSL.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  AddInfo('table1',
          'field1'#28'field2'#28'field3',
          'value1'#28'value2'#28'value3',
          mft_String + #28 + mft_String + #28 + mft_String);

{ or

  AddInfo('table1',
          'booleanfield1'#28'datefield2'#28'stringfield3',
          'true'#28'01-Dec-2007'#28'some text',
          mft_Bool + #28 + mft_Date + #28 + mft_String);

or
  AddInfo('table1',
          'field1'#28'field2'#28'field3',
          'value1'#28'value2'#28'value3',
          'S'#28'S'#28'S');
}

end;
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 18746031
can you show me where you are getting the values from, because if it's from another table/query, there is a much better way of doing this, which will also work for NULLs btw
0
 

Author Comment

by:Grant Fullen
ID: 18746069
The values are coming from DBEdits.  You type in the information in the edits and then hit the "Insert Info" button, which is where I'm putting this code.  The new record should then be put in.
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 18746174
If they are DBEdits, then shouldn't this all be happening automatically?
Are you familiar with using DB Controls?
0
 

Author Comment

by:Grant Fullen
ID: 18746228
Works just fine.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

830 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