| | |
Add ADO record via code
![]() |
•
•
Join Date: Oct 2007
Posts: 20
Reputation:
Solved Threads: 0
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:
Here's what I've got so far:
Pascal and Delphi Syntax (Toggle Plain Text)
// e.g. NewKey = '"2008-06-18"'; SQL := 'SELECT Event FROM Events ' + ' WHERE Day = ' + NewKey; rs := CoRecordset.Create; rs.CursorLocation := adUseClient; rs.Open(SQL, cnn, adOpenDynamic, adLockPessimistic, adCmdTableDirect); if not rs.EOF then DayEvent := VarToStr(rs.Fields.Item['Event'].Value) else begin ?????? end;
•
•
Join Date: Jun 2008
Posts: 9
Reputation:
Solved Threads: 0
You can use an INSERT statement and the cnn.Execute() method.
I use the following code to do this:
This is the SQL TStrings fotr my ADQuery.
To run the Query
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.
This is the SQL TStrings fotr my ADQuery.
Pascal and Delphi Syntax (Toggle Plain Text)
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)
with ADOQuery2 do begin Close; // Make sure the Query is Closed before trying to set the Parameters. // Add the Data to the Parameter List. Parameters.ParamByName('xUserName').Value := Peer[CurPeer].UserName; Parameters.ParamByName('xPassword').Value := Peer[CurPeer].Password; Parameters.ParamByName('xEmail').Value := Peer[CurPeer].Email; Parameters.ParamByName('xCountry').Value := Peer[CurPeer].Country; Parameters.ParamByName('xIp').Value := Peer[CurPeer].IP; Parameters.ParamByName('xJoined').Value := DateTimeToStr(Now); if(Peer[CurPeer].Premium)then begin Parameters.ParamByName('xPremium').Value := 'Yes'; end else begin Parameters.ParamByName('xPremium').Value := 'No'; end; .........etc ExecSQL; // This is very important and often overlooked. You need to use ExecSQL not Open for an update , Insert etc.
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.
![]() |
Similar Threads
- trojans...now nothing opens and I get a paint can't open error (Viruses, Spyware and other Nasties)
- Master:Detail deletions (C#)
- Generate Primary Key in VB / Delete Record Status (Visual Basic 4 / 5 / 6)
- Beginner: how to add new record into database??? (VB.NET)
- Read Me:Access to any type of DB's possible (Visual Basic 4 / 5 / 6)
- Delete related record (VB.NET)
- Problem with Listbox (Visual Basic 4 / 5 / 6)
- ADO.NET question modification. (VB.NET)
- ADO data control problem (Visual Basic 4 / 5 / 6)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: Need examples of manipulating 2d arrays
- Next Thread: Help with cursor!!!!
| Thread Tools | Search this Thread |





