Click to See Complete Forum and Search --> : MS-Access and Visual C++


heyrung
May 29th, 2005, 01:42 PM
Hi
I have data structure in Visual C++ that I have to write a program/function to save to as a new MS-Access database file. I have no knowledge about how to deal with database thru C++, and I'm trying to find a way to do this.

I would like to make 2 tables out of that data structure, which are linked thru 1 key. I've read tutorial and tried some exercise, but none of them seems to perfectly fit what I want. After trying what the tutorial said (I make a database file in Access, add it to a new ODBC data source, and in VC++, I use CRecordset class to dump data into Access thru ODBC), I got some questions below.

1. Is there a way to save many records at one time? (now I use CRecordset, but I can only save one record at a time. I do this by open, add new, update, and close. So if I have 50 records, I have to do these set of steps 50 times which may take too long)

2. How do I save the data structure in VC++ into MS-Access as a new file every time I execute this function/program? (What I am doing now is I build a database file in MS-Access and overwrite it, but I can not save as a new file....and if I could, I wonder how I can skip adding new ODBC data source every time I save as a new database file.) Do u think ODBC is the right choice for me or do I need DAO?

Please also give me the link to the tutorial (for someone who starts from ground up) if you have one.

Thank you

panayotisk
May 30th, 2005, 02:21 AM
Which version of VC++ are you using? Also which version of MS Access?
I could not manage to use ODBC correctly in my application with VC 6.0 since it could not handle Unicode data. So I moved to ADO.

1. Saving many records at a time may be achieved by issuing an
INSERT INTO...SELECT statement. I do not know if Access also supports a bulk insert capability.

2. Do you need a new file (different filename) or just a new table in the existing file? You may try to issue a CREATE TABLE call to create a new table so you do not need to add a new data source everytime.

Anyway consider using ADO, although I guess everything you want can be done by ODBC too.

heyrung
May 30th, 2005, 06:30 PM
Hi
I use Visual C++ 6.0...not the .NET one. Will ADO work? And I would like to create a new file under a new file name...not a new table within the old file though. So with the ADO, do I have to create new ODBC datasource for every new database file?

Do u have a link to an ADO tutorial? I would like to know what it is, what it can do, and how go get started with programming ADO.

Thank you

heyrung
May 30th, 2005, 08:50 PM
I got 2 questions
1.) I tried using the INSERT INTO...VALUES statement like u suggested, but I don't know how to insert 2 or more records at a time. How do u insert more than 1 record? This is the code I did for the SQL statement


UpdateDate(TRUE);
//m_myDB is a CRecordSet class object
if(!m_myDB.Open( CRecordset::dynaset,
_T( "INSERT INTO tblOne (FieldOne,FieldTwo)
VALUES(m_lValue1,m_lValue2")))
{
AfxMessageBox("Cannot insert");
return;
}

m_myDB.Close();

I tried Open twice so I can do INSERT 2 times, but the data get stored to the table only the first time I INSERT.

2.) Since my insert doesn't work, I came back to my old way. This is my code

//m_myDB is a CRecordSet class object
if (!m_myDB.Open ())
{
AfxMessageBox("cannot open");
return;
}



if(!m_myDB.CanAppend ())
{
AfxMessageBox("cannot append");
return;
}
UpdateData(TRUE);
m_myDB.AddNew( );




//tblScanPosition table
m_myDB.m_FieldOne = m_lValue1;
m_myDB.m_FieldTwo = m_lValue2;


if( !m_myDB.Update( ) )
{
AfxMessageBox( "Cannot Add" );
return;
}

if(!m_myDB.CanAppend ())
{
AfxMessageBox("Cannot Append");
return;
}
m_myDB.AddNew( );


//tblScanPosition table
m_myDB.m_FieldOne = m_lValue1+1;
m_myDB.m_FieldTwo = m_lValue2+1;

if( !m_myDB.Update( ) )
{
AfxMessageBox( "cannot update." );
return;
}

m_myDB.Close();

I want the code to store 2 new entries onto the table, but it doesn't. It only stores 1 new entry, which is the one that gets executed first (m_lValue1, and m_lValue2). How can I make this code stores 2 entries without re-openning, and re-closing the CRecordset object twice?

Thank you

panayotisk
May 31st, 2005, 09:27 AM
Using ADO you do not have to create a datasource like you do when using ODBC. You may find some tutorials on this site.
I cannot help you with ODBC since I abandonned it away quite quickly.
An example for starting with ADO: http://www.codeproject.com/database/simpleado.asp
If you choose to stick to ODBC see the articles on ODBC...

heyrung
May 31st, 2005, 02:47 PM
Can Visual C++ 6.0 do ADO?

panayotisk
June 1st, 2005, 02:41 AM
Of course.