Click to See Complete Forum and Search --> : DaoRecordset.SetFieldValue("FName", CString) Part2?


June 25th, 1999, 05:16 PM
I am having a problem. When I try to addNew to my .mdb in all of the CStrings that I try to add it only adds the first character of the CString.
If I do this:

// Doesn't work - only writes "M"
CString Name = "MICAH";
userRecordset.SetFieldValue( "FName", (COleVariant)Name );

It will only put the "M" in the FName. I have my .mdb FName field set to a lenght of 50. If I do this:

// Works
userRecordset.SetFieldValue( "FName", "MICAH" );

It works when I hard code the value in? Why in the would this be happening?

Here is my code:

CUserTable::CUserTable(int iUID, CString sFName, CString sLName, CString sAddress, CString sCity, CString sState, int iZip, CString sCountry, CString sEmail)
{
try
{
CDaoDatabase mydb; // database
CDaoTableDef userTable( &mydb ); // table def with a pointer to the database
CDaoRecordset userRecordset; // recordset

mydb.Open( "C:\\RTC.MDB", FALSE, FALSE, _T("") );

userTable.Open( "Users" );

userRecordset.Open( &userTable, dbOpenDynaset, 0 );

userRecordset.AddNew();

userRecordset.SetFieldValue( "UID", (COleVariant)(LONG)iUID );
userRecordset.SetFieldValue( "FName", (COleVariant)sFName );
userRecordset.SetFieldValue( "LName", (COleVariant)sLName );
userRecordset.SetFieldValue( "Address", (COleVariant)sAddress );
userRecordset.SetFieldValue( "City", (COleVariant)sCity );
userRecordset.SetFieldValue( "State", (COleVariant)sState );
userRecordset.SetFieldValue( "Zip", (COleVariant)(LONG)iZip );

userRecordset.Update();

userTable.Close();
userRecordset.Close();
mydb.Close();
}

catch(CException *pE)
{
char txt[301];
pE->GetErrorMessage(txt,300);
AfxMessageBox(txt);
}
}

I used GetDlgItemText to get the values from edit boxes before I called the above function. I have tested to make sure that the GetDlgItemText didn't mess anything up. After I called the above function, after doing the getdlgitemtext's I did a AfxMessageBox(sFName); and it was right. Then before I closed the table I did it again and the value was still correct. It's just when it's writing to the db, for some reason it just writes the first character of the CString. Can you tell what I am doing something wrong?

This is my first time to handle .mdb's within MFC, and I find doing it this hard way rather then using the class wizard will help me learn better. Thanks for any help!