Solved

Making My Own Procedure

Posted on 2007-03-18
8
147 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
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…

746 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

10 Experts available now in Live!

Get 1:1 Help Now