Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Does anyone how to write and read value of a multivalued field in access from MFC using DAO?
Is It Possible? :-O

I'm using Access version 2003.. i found everywhere for answer,i couldn't find it :((

Pls Help me...
Posted

1 solution

Maybe this sample could help you out:

#import "G:\Program Files\Common Files\Microsoft Shared\DAO\dao360.dll"
#include "Debug\dao360.tlh"
#include<atlbase.h>
#include<iostream>

using namespace std;

int main()
{
    CoInitialize(0);
    {
        HRESULT hr = -1;
        CComPtr<IUnknown> app;
        DAO::_DBEngine* eng;
        DAO::Workspace* wr;
        DAO::Database* db;
        DAO::Recordset* rs;
        VARIANT_BOOL eof;
        VARIANT_BOOL bof;
        DAO::Fields* fs;
        DAO::_Field* f1;
        DAO::_Field* f2;
        hr = app.CoCreateInstance(L"DAO.DBEngine.36");
        
        hr = app->QueryInterface(&eng);
        
        hr = eng->raw_CreateWorkspace(L"TestWorkSpace1", L"Admin", L"", _variant_t(DAO::dbUseJet), &wr);
        //open dbname, ifexclusive, ifreadonly, connect, retdb
        hr = wr->raw_OpenDatabase(_bstr_t(L"c:\\db1.mdb"), _variant_t(0), _variant_t(0), _variant_t(L""), &db);
        hr = db->raw_OpenRecordset(_bstr_t(L"select * from Table1"), 
            _variant_t(DAO::dbOpenDynaset), _variant_t(0), _variant_t(2), &rs);
        hr = rs->get_Fields(&fs);
        hr = fs->get_Item(_variant_t(0), &f1);
        hr = fs->get_Item(_variant_t(1), &f2);
        wcout<< L"<"<< (BSTR)f1->Name<< L"><" << f2->Name<< L">"<< endl;
        VARIANT valx, valy;
        
        while(true)
        {
            hr = rs->get_EOF(&eof);
            hr = rs->get_BOF(&bof);
            if(eof)break;
            if(bof)break;
            f1->get_Value(&valx);
            f2->get_Value(&valy);
            wcout<< L"{"<< (wchar_t*)_bstr_t(_variant_t(valx))<< L"}{";
            wcout<< (wchar_t*)_bstr_t(_variant_t(valy))<< L"}"<< endl;
            hr = rs->MoveNext();
        }

        hr = hr;
    }
    CoUninitialize();
    
    return 0;
}
 
Share this answer
 
Comments
Rashmi R 29-Jan-11 2:42am    
Thanks for the code:)
Ryan Zahra 29-Jan-11 5:03am    
You're welcome :)
Rashmi R 15-Feb-11 3:29am    
I tried the above code but dao360.dll file which need to be imported is having error (My Complier states like that) If i remove that line, all below code cannot be used.. Please help me..
Ryan Zahra 15-Feb-11 4:19am    
Can you give me the exact error code?
Also make sure that the path to the dll and the path to the tlh are correct
Rashmi R 16-Feb-11 3:43am    
ya i've written correct code path but, "Debug\dao360.tlh" is givin error like below

Compiling...
MyPgmDlg.cpp
f:\MyProject\debug\dao360.tlh(1859) : error C2629: unexpected 'short ('
f:\MyProject\debug\dao360.tlh(1859) : error C2238: unexpected token(s) preceding ';'

This are the code errors..
F:\MyProject\MyPgmDlg.cpp(6011) : error C2440: 'type cast' : cannot convert from 'enum DAO::WorkspaceTypeEnum' to 'class _variant_t'
No constructor could take the source type, or constructor overload resolution was ambiguous
F:\MyProject\MyPgmDlg.cpp(6013) : error C2440: 'type cast' : cannot convert from 'const int' to 'class _variant_t'
No constructor could take the source type, or constructor overload resolution was ambiguous

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900