Link to home
Start Free TrialLog in
Avatar of talonsblade
talonsblade

asked on

access Table C++

My company uses a database that uses a fixed lenght dat file for its tables.  Me and another programmer who is not here anymore worked out a way to pull the data into access and also into msSQL.  this works out great, i know vba and sql pretty well so no problems.  every month, for billing porposes we have to update the parts costs in one of the tables.  The new parts cost are listed in another program that is updated and distributed to everyone that uses it month.  It uses propriety table structures and files and such but also had tools built into it for programmers to access the data through C++ and VB.  THis worked out great because we knew vb and was fast, we could update 53 thousand parts in about 5 minutes or so.  
Now though, the parts program is coded by another company and they have ripped out all vb compatibilty and only uses c++ which i barely knew 8 years ago.  now i know nothing.  I know you can integrate vb.net and c++.net but all of our program coding, which is extensive by now is done in access really.  we have a distributed adp which grants access to reports, table, querys and such.  

My question is, is there a way to get c++ to read an access table into an array or something.  once in the array i can loop through it and get the new parts cost using the tool provided.  i would rather not have to work on moving to vb.net out of our adp because we have been expanding/improving it for 4 years now and it is rather large.  I dont know vb.net and have started to learn it some but its just not clicking in my head yet.
Avatar of jkr
jkr
Flag of Germany image

Well, maybe http://www.codeproject.com/KB/database/readdb.aspx ("Using the CDatabase class to read an Access databases") can serve you as a starting point. This article demonstrates how to do what5 you described and comes with full source code.
Avatar of talonsblade
talonsblade

ASKER

from reading that it looks like only the very beginning would be useful, the part about the recordset.  i did not know about recordsets in c++.  if i open the recordset from the access table, will any changes i make in the recordset be pushed back to the access table because that would be preferable.  i would like to keep the table in access because of all the processes we run with it
The changes will be reflected in the DB as soon as you call 'CRecordset::Update()' (http://msdn.microsoft.com/en-us/library/Aa312376.aspx). Maybe http://www.codeproject.com/KB/MFC/BeginnerDatabaseOpt.aspx ("Using Database in your MFC program - A beginner's experience") will help a bit more.
this did not help much, like i said i dont know much about c++ and it kept giving me errors.  i did find the example in  this question
https://www.experts-exchange.com/questions/20136580/C-program-linking-Access-database-file.html
the example from AlexVirochovsky works perfectly but i cant figure out how to edit information in the recordset.  i changed the recordset to adopendynamic and adlockoptimistic but cant figure out how to change the data.
here is a copy of  the code i am using
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
// This code comes from : www.geocities.
//     com/mokarrabin
#include <stdio.h>
#include <iostream.h>
void main(void)
 
 
    {
    CoInitialize(NULL);
    try 
 
 
        {
        _RecordsetPtr pRst("ADODB.Recordset");
        // Connection String
        _bstr_t strCnn("DRIVER={Microsoft Access Driver (*.mdb)};UID=admin;DBQ=GBOOK.mdb");
              // Open table
             pRst->Open("SELECT * FROM ProductService where ProductService like '%samir%';", strCnn, adOpenDynamic, adLockOptimistic, adCmdText);
              
              pRst->MoveFirst();
 
 
                  while (!pRst->EndOfFile) {
                       cout<<(char*) ((_bstr_t) pRst->GetFields()->GetItem("ProductService")->GetValue())<<endl;
                       pRst->MoveNext();
                  }
                  pRst->Close(); 
                  
        }
        catch (_com_error &e)
 
 
            {
            cout<<(char*) e.Description();
        }
        ::CoUninitialize();
    }

Open in new window

jkr, could you please respond if you see this.  

like i said i have figured out how to change data in the recordset/table.  the websites i have read say that it is as easy as vb, which the begining isnt that much different until you get to output.  in vb you use recordset!field.  this example uses
(char*) ((_bstr_t) pRst->GetFields()->GetItem("ProductService")->GetValue())
ok i get pRst->GetFields()->GetItem("ProductService")->GetValue()), its kind of convoluted but its self explanitory.  ive done some searching on line and i know that _bstr_t is a function that takes the VB string, which i guess this recordset uses, and puts a c++ wrapper around it so its usable.

What i dont understand is why you have to take a pointer from it, i think that is what the (char*) is doing right?  Plus, how do i use this to update the fields.  would i have to convert it back to a bstr before?

Thanks for all your help jkr
err the second line was supposed to say, i have NOT figured out how to change the data in the recorest/table
Just to be clear... do you need to update the Access Db, or do you just need the data to be in a memory array?
ASKER CERTIFIED SOLUTION
Avatar of talonsblade
talonsblade

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial