SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Reading data from MS Access table using OleDB



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> OLE2, Excel, WinWord
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Sun Sep 02, 2007 3:50 pm    Post subject: Reading data from MS Access table using OleDB Reply with quote

A simple program that show how to read data from Microsoft Access.
In this case we read data from MS Access Table: PA0001 with 2 field: PERNR and Name.
MS Access file is: Data.mdb and saved in drive C.

Type and Data declaration:

Code:
TYPE-POOLS OLE2.

DATA: CONN      TYPE OLE2_OBJECT,
          RSDB      TYPE OLE2_OBJECT,
          SQL        TYPE C LENGTH 1024.

DATA: BEGIN OF WA_TEMP,
            VALUE TYPE C LENGTH 1024,
          END OF WA_TEMP,
          IT_TEMP LIKE STANDARD TABLE OF WA_TEMP.
DATA: BEGIN OF WA_PA0001,
            PERNR TYPE PERSNO ,
            ENAME TYPE EMNAM,
          END OF WA_PA0001,
          IT_PA0001 LIKE TABLE OF WA_PA0001.


Create ADODB Object:

Code:
START-OF-SELECTION.

  CREATE OBJECT CONN 'ADODB.Connection'.
  CREATE OBJECT RSDB 'ADODB.Recordset'.
 


Set connection and run query:

Code:
* MDB Connetion infomations
  CONCATENATE 'Provider=Microsoft.Jet.OLEDB.4.0;'
  'Data Source=C:\Data.mdb;'
  INTO SQL.

  CALL METHOD OF CONN 'Open' EXPORTING #1 = SQL.

* Query Statement ...
  SQL = 'SELECT * FROM PA0001'.

* Run Query
  CALL METHOD OF RSDB 'OPEN'
    EXPORTING #1 = SQL
    #2 = CONN
    #3 = '1'.


Save records into internal table:

Code:

DO.
    CALL METHOD OF RSDB 'getstring' = SQL
    EXPORTING #1 = '2' "Do not modify!
    #2 = 1    "Do not modify!
    #3 = '|'  "Do not modify!
    #4 = '|'. "Do not modify!

    IF SY-SUBRC EQ 0.
      REFRESH IT_TEMP.

      SPLIT SQL AT '|' INTO TABLE IT_TEMP.
      LOOP AT IT_TEMP INTO WA_TEMP.
        CASE SY-TABIX.
          WHEN 1.
            WA_PA0001-PERNR = WA_TEMP-VALUE.
          WHEN OTHERS.
            WA_PA0001-ENAME = WA_TEMP-VALUE.
        ENDCASE.
      ENDLOOP.
      APPEND WA_PA0001 TO IT_PA0001.
      CLEAR WA_PA0001.
    ELSE.
      EXIT.
    ENDIF.

  ENDDO.


Display data:

Code:

ULINE AT (55).
  WRITE: / '|' NO-GAP, (010) 'N I K'   LEFT-JUSTIFIED,
           '|' NO-GAP, (040) 'N A M E' LEFT-JUSTIFIED,
           '|'.
  ULINE AT /(55).
  LOOP AT IT_PA0001 INTO WA_PA0001.
    WRITE: / '|' NO-GAP, (010) WA_PA0001-PERNR   LEFT-JUSTIFIED,
             '|' NO-GAP, (040) WA_PA0001-ENAME   LEFT-JUSTIFIED,
             '|'.
  ENDLOOP.
  ULINE AT /(55).
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> OLE2, Excel, WinWord All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.