943,747 Members | Top Members by Rank

Ad:
Jun 25th, 2008
0

Add ADO record via code

Expand Post »
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;
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
bob on whidbey is offline Offline
32 posts
since Oct 2007
Jun 26th, 2008
0

Re: Add ADO record via code

You can use an INSERT statement and the cnn.Execute() method.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
filippo.toso is offline Offline
9 posts
since Jun 2008
Jun 28th, 2008
0

Re: Add ADO record via code

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Clive29 is offline Offline
7 posts
since May 2008
Jul 13th, 2008
0

Re: Add ADO record via code

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
waynera is offline Offline
1 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: Need examples of manipulating 2d arrays
Next Thread in Pascal and Delphi Forum Timeline: Help with cursor!!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC