Solved

Making My Own Procedure

Posted on 2007-03-18
8
152 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

691 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