Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to use AddNew() ?

Status
Not open for further replies.

kaya17

Programmer
Feb 9, 2004
78
SG
hi, anyone can tell me how to use AddNew()?
i have a CRecordset object, and i use the statement below to open it:

recset.open(CRecordset::snapshot, tablename, CRecordset::appendOnly)


recset.AddNew()
recset.IP = ip;
recset.HostName = hostname;

IP and HostName are the field names in my table with "tablename",

but the compiler said IP and HostName are not the member of CRecordset.

then, what should i do to assign the values to each field of the table?

thanks a lot for your help!

regards,
kaya
 
A record set needs to have an associated and opened CDatabase object before it will work. Below is some sample code to give you an idea of how CDatabase and Crecordset objects work. I suggest using the class wizard to generate a new class for the table you are working with. Then add a function to add new rows. The database can be opened from this class but typically this is done elsewhere. Since most programs manipulate multiple tables a single instance of the database object is created and passed to each recordset class.

You also specified a snapshot Snapshots do not allow updates.

CString Connect, Table;
CDatabase dbData;
CRecordset rsTable;

Connect = "DSN=DsnName;UID=UserId;PWD=Password"; // Specify DSN, user and password
Table = "TableName"; // Specify table name

dbData.OpenEx(Connect); // Open database object

rsTable.m_pDatabase = dbData; // Associate database object.
rsTable.Open(CRecordset::dynaset, Table); // Now open recordset

rsTable.AddNew(); // Initiate Add transaction

rsTable.m_strName = "Kaya17"; // Member variables in DoFieldExchange
rsTable.m_wAge = "17"; // added using class wizard

rsTable.Update(); // Update completes write transaction

rsTable.Close(); // Close recordset
dbData.Close(); // Close database

Below is an example of the DoFieldExchange() function which will be generated by the wizard.

void CCustSet::DoFieldExchange(CFieldExchange* pFX)
{
//{{AFX_FIELD_MAP(CCustSet)
pFX->SetFieldType(CFieldExchange::eek:utputColumn);
RFX_Text(pFX, "Name", m_strName);
RFX_Int (pFX, "Age", m_wAge);
//}}AFX_FIELD_MAP
}

Note this has not been compiled so it may contain syntax errors.

Good luck. I will check back for any replies if you have an additional questions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top