Solved

Making My Own Procedure

Posted on 2007-03-18
8
151 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
Technology Partners: 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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

734 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