Solved

Making My Own Procedure

Posted on 2007-03-18
8
148 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Strange code, can use it, but i cant figure out what it does. 3 54
Newbie Thread Programming 1 139
Delphi selector screen 2 69
find a node in VST 2 63
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

21 Experts available now in Live!

Get 1:1 Help Now