Add ADO record via code

Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 20
Reputation: bob on whidbey is an unknown quantity at this point 
Solved Threads: 0
bob on whidbey bob on whidbey is offline Offline
Newbie Poster

Add ADO record via code

 
0
  #1
Jun 25th, 2008
I'm using D2007 and trying to add a new, blank record to my Access database with code. The DB has a 'day' field and a 'event' field. All works fine when a record exists for the given day. I need to learn how to add a record to the file with day=NewKey when that record doesn't exist in the file.

Here's what I've got so far:

Pascal and Delphi Syntax (Toggle Plain Text)
  1. // e.g. NewKey = '"2008-06-18"';
  2.  
  3. SQL := 'SELECT Event FROM Events ' + ' WHERE Day = ' + NewKey;
  4.  
  5. rs := CoRecordset.Create;
  6. rs.CursorLocation := adUseClient;
  7. rs.Open(SQL, cnn, adOpenDynamic, adLockPessimistic, adCmdTableDirect);
  8.  
  9. if not rs.EOF then
  10. DayEvent := VarToStr(rs.Fields.Item['Event'].Value)
  11. else begin
  12. ??????
  13.  
  14. end;
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 9
Reputation: filippo.toso is an unknown quantity at this point 
Solved Threads: 0
filippo.toso filippo.toso is offline Offline
Newbie Poster

Re: Add ADO record via code

 
0
  #2
Jun 26th, 2008
You can use an INSERT statement and the cnn.Execute() method.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 7
Reputation: Clive29 is an unknown quantity at this point 
Solved Threads: 0
Clive29 Clive29 is offline Offline
Newbie Poster

Re: Add ADO record via code

 
0
  #3
Jun 28th, 2008
hi
if you want to be able to add a new record, i suggest that you draw a DBGrid on your form. since DBGrid has delete, add, assign, update, edit and goto. so you dont need a code for that, all you have to do is to link your DBGrid with your ADO thingy.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1
Reputation: waynera is an unknown quantity at this point 
Solved Threads: 0
waynera's Avatar
waynera waynera is offline Offline
Newbie Poster

Re: Add ADO record via code

 
0
  #4
Jul 13th, 2008
I use the following code to do this:
This is the SQL TStrings fotr my ADQuery.

Pascal and Delphi Syntax (Toggle Plain Text)
  1.  
  2. INSERT INTO Users (UserName,[Password],[First Name],Surname,eMail,Country,Program,Joined,[First Ip],Updates,Premium,Http,TimesViewed,AveLoginTime,AveConnectTime,TimesOptomized) VALUES ( :xUserName,:xPassword,:xFirstName,:xSurname,:xEmail,:xCountry,:xProgram,:xJoined,:xIP,:xUpdates,:xPremium,:xHttp,:xTimesViewed,:xAveLoginTime,:xAveConnectTime,:xTimesOptomized);

To run the Query
Pascal and Delphi Syntax (Toggle Plain Text)
  1. with ADOQuery2 do
  2. begin
  3. Close;
  4. // Make sure the Query is Closed before trying to set the Parameters.
  5. // Add the Data to the Parameter List.
  6.  
  7. Parameters.ParamByName('xUserName').Value := Peer[CurPeer].UserName;
  8. Parameters.ParamByName('xPassword').Value := Peer[CurPeer].Password;
  9. Parameters.ParamByName('xEmail').Value := Peer[CurPeer].Email;
  10. Parameters.ParamByName('xCountry').Value := Peer[CurPeer].Country;
  11. Parameters.ParamByName('xIp').Value := Peer[CurPeer].IP;
  12. Parameters.ParamByName('xJoined').Value := DateTimeToStr(Now);
  13. if(Peer[CurPeer].Premium)then
  14. begin
  15. Parameters.ParamByName('xPremium').Value := 'Yes';
  16. end
  17. else
  18. begin
  19. Parameters.ParamByName('xPremium').Value := 'No';
  20. end;
  21. .........etc
  22.  
  23. ExecSQL;
  24. // This is very important and often overlooked. You need to use ExecSQL not Open for an update , Insert etc.
Please see how to declare the variables ....... use :VarName;
Also use ExecSQL not Open. Open is for SELECT Queries only.

Also I save the DateTime to a Text var. and convert back when I extract the data.
It works all the time. It is somewhat buggy trying to attach the DateTime formats for differing Databases.

Hope this helps.

Waynera.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Pascal and Delphi Forum
Thread Tools Search this Thread



Tag cloud for Pascal and Delphi
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC