Access 2010 Programming

Hello,

I'm learning the basics on how to interface VC++ with Microsoft Access 2010. This website: http://msdn.microsoft.com/en-us/library/ff965871.aspx#DataProgrammingWithAccess2010_DirectODBCExample provides a great example on how to run queries using several methods (DAO, ADO). However, I'm also looking for a simple example on how to add, modify and delete records from a table. I tried following a VB example from here: http://www.functionx.com/vbaccess/Lesson24.htm , but trying to translate this code to VC++ is proving to be above my level expertise. So far, I have the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
bool AddItem(_bstr_t bstrDatabaseName, _bstr_t bstrTableName)
{

    HRESULT hr = CoInitialize(NULL);
	if(FAILED(hr)) {printf("%s: Failed to CoInitialize() COM.\n",DAM); return false;}
	hr = CoCreateInstance(__uuidof(DAO::DBEngine), NULL, CLSCTX_ALL, IID_IDispatch, (LPVOID*)&pEngine);
	if(SUCCEEDED(hr) && pEngine)
	{
		DAO::RecordsetPtr pRS = NULL;
		try
		{
			pDbPtr = pEngine->OpenDatabase(bstrDatabaseName, false, false, ";PWD=");
			if(pDbPtr)
			{
				_variant_t vtZeroParam, vtBUNO;
				vtBUNO = (_variant_t) 123456;
				pRS = pDbPtr->OpenRecordset(bstrTableName);
				pRS->AddNew();
				pRS->Fields->GetItem((long)1)->PutValue(vtBUNO); printf("Made it this far!\n");
				pRS->Update(1, (_variant_t) FALSE);
				pRS->Close();
			}
		}
		catch(_com_error& e)
		{
			cout<<DAM<<": _com_error: "<<e.ErrorMessage()<<endl;
			cout<<DAM<<": _com_error description: "<<e.Description()<<endl;
		}
	}
	CoUninitialize();
	return true;

}


I'm trying to add a record to field 1 (field 0 is reserved for the key) only, but the code fails at line 19. All I'm looking for is for the simplest way to add, modify and delete records from Access 2010. Any method (DAO or ADO) works for me.

Thanks in advance,

DominicanJB
Last edited on
Got it. Line 19 should be pRS->Fields->Item[(long)1]->Value = (_variant_t) "123456";. The trick here is that the entry needs to be a char cast into a _variant_t regardless of the data type. In addition, line 20 should be pRS->Update(1, 0);.

Thanks,
DominicanJB
Last edited on
Topic archived. No new replies allowed.