Read data from SAP tables into MS Access 2003 database
Applies to: | SAP and MS Access |
Summary: | The article presents how to create a tool to read data from SAP tables into MS Access database |
Author: | Sergiu Iatco |
Created on: | 21 June 2012 |
The article describes how to create a multifunctional tool to read data from transparent tables of SAP into MS Access database. Basic knowledge of MS Access 2003 and ABAP are necessary. The tool is created in plain VBA with basic GUI in order to keep it simple leaving enough room for further own adjustments. Once the tool is created you may use it for any table, with one table or many tables processed consecutively, you may define selections, you may define the way of logon dialog or silent, you may define if the message should pop-up or issued only on status bar, etc. With additional knowledge of VBA you may redesign it to your specific need. The main purpose of the tool is to save time instead of storing data into external files and converting each time to the same structure but with different content. I consider that processing of data with this tool could be especially helpful during implementation projects and testing of reports when checking and rechecking of results takes lot of time.
You have to follow step by step instructions to create functions and macros with copy-paste, and then you are ready to run macros to create administration tables and definitions for tables to retrieve data from. Also you may download a ready to use database here.
In tab Objects press then press
CreateADMIN() | 00 - CreateADMIN |
CreateTABNAME() | 01 – CreateTABNAME |
CreateDD03L() | 02 - CreateDD03L |
fillDD03L() | 03 - FillDD03L |
CreateTableAsDD03L() | 04 - CreateTableAsDD003L |
ReadTableAsDD03L() | 05 - ReadTableAsDD03 |
DeleteAsTabname() | 06 – DeleteAsTabname |
RRemoveMeter() | 07 – RemoveMeter |
00 Create 00ADMIN | 00 - CreateADMIN | CreateADMIN() |
01 Create 01TABNAME} | 01 – CreateTABNAME | CreateTABNAME() |
02 Create DD03L | 02 - CreateDD03L | CreateDD03L() |
03 Fill DD03L | 03 - FillDD03L | fillDD03L() |
04 CreateTableAs DD03L | 04 - CreateTableAsDD003L | CreateTableAsDD03L() |
05 ReadTableAs DD03L | 05 - ReadTableAsDD03 | ReadTableAsDD03L() |
06 DeleteAsTabnameDeleteAllData | 06 – DeleteAsTabname | DeleteAsTabname() |
07 RRemoveMeter | 07 – RemoveMeter | RRemoveMeter() |
Field | Value |
CNT_STR_APPLN_SRVR | IP of SAP server |
CNT_STR_CLIENT | Client |
CNT_STR_PWD | Password |
CNT_STR_SYS_NUM | System Number |
CNT_STR_SYSTEM | System ID |
CNT_STR_USR | User |
CNT_STR_LOGON_LANG | Logon Language |
MessageOn | [X] – Pop-up dialog message [ ] – Status bar Messages |
Active | [..] – Dialog Logon [X] – User and password is taken automatically from 00ADMIN table |
Tabname | SAP R/3 to retrieve table from |
Options | Selection using ABAP syntax. Do not insert dot at the end. |
CreateDD03L | To create structure of table DD03L |
FillDD03L | To fill structure with content |
CreateAsDD03L | To create table according to definitions from DD03L |
ReadAsDD03L | To retrieve data from table in filled in field Tabname when you run macro {05 ReadTableAs DD03L } |
ReadAsSQLon | When you read more then 100 000 records you may try to improve performance by checking this field |
DeleteAllData | Content of table is deleted when you run macro {06 DeleteAsTabnameDeleteAllData } |
Dim db As Database
Dim tdfNew As TableDef
Set db = CurrentDb
On Error Resume Next 'Table does not exist
db.TableDefs.Delete "ADMIN"
On Error GoTo 0 'Reset error handling
Set tdfNew = db.CreateTableDef("00ADMIN")
With tdfNew
.Fields.Append .CreateField("CNT_STR_APPLN_SRVR", dbText, 50)
.Fields.Append .CreateField("CNT_STR_CLIENT", dbText, 50)
.Fields.Append .CreateField("CNT_STR_PWD", dbText, 50)
.Fields.Append .CreateField("CNT_STR_SYS_NUM", dbText, 50)
.Fields.Append .CreateField("CNT_STR_SYSTEM", dbText, 50)
.Fields.Append .CreateField("CNT_STR_USR", dbText, 50)
.Fields.Append .CreateField("CNT_STR_LOGON_LANG", dbText, 50)
.Fields.Append .CreateField("MessageOn", dbBoolean)
' .Fields.Append .CreateField("LogonSilient", dbBoolean)
.Fields.Append .CreateField("Active", dbBoolean)
db.TableDefs.Append tdfNew
End With
With tdfNew.Fields("MessageOn")
.Properties.Append .CreateProperty("DisplayControl", _
dbInteger, CInt(acCheckBox))
End With
With tdfNew.Fields("LogonSilient")
.Properties.Append .CreateProperty("DisplayControl", _
dbInteger, CInt(acCheckBox))
End With
With tdfNew.Fields("Active")
.Properties.Append .CreateProperty("DisplayControl", _
dbInteger, CInt(acCheckBox))
End With
Dim db As Database
Dim tdfNew As TableDef
Set db = CurrentDb
On Error Resume Next 'Table does not exist
db.TableDefs.Delete "01TABNAME"
On Error GoTo 0 'Reset error handling
Set tdfNew = db.CreateTableDef("01TABNAME")
With tdfNew
.Fields.Append .CreateField("Tabname", dbText, 30)
.Fields.Append .CreateField("Options", dbText, 72)
.Fields.Append .CreateField("CreateDD03L", dbBoolean)
.Fields.Append .CreateField("FillDD03L", dbBoolean)
.Fields.Append .CreateField("CreateAsDD03L", dbBoolean)
.Fields.Append .CreateField("ReadAsDD03L", dbBoolean)
.Fields.Append .CreateField("ReadAsSQLon", dbBoolean)
.Fields.Append .CreateField("DeleteAllData", dbBoolean)
db.TableDefs.Append tdfNew
End With
With tdfNew.Fields("CreateDD03L")
.Properties.Append .CreateProperty("DisplayControl", _
dbInteger, CInt(acCheckBox))
End With
With tdfNew.Fields("FillDD03L")
.Properties.Append .CreateProperty("DisplayControl", _
dbInteger, CInt(acCheckBox))
End With
With tdfNew.Fields("CreateAsDD03L")
.Properties.Append .CreateProperty("DisplayControl", _
dbInteger, CInt(acCheckBox))
End With
With tdfNew.Fields("ReadAsSQLon")
.Properties.Append .CreateProperty("DisplayControl", _
dbInteger, CInt(acCheckBox))
End With
With tdfNew.Fields("ReadAsDD03L")
.Properties.Append .CreateProperty("DisplayControl", _
dbInteger, CInt(acCheckBox))
End With
With tdfNew.Fields("DeleteAllData")
.Properties.Append .CreateProperty("DisplayControl", _
dbInteger, CInt(acCheckBox))
End With
'Create table for method ReadAsSQLon
On Error Resume Next 'Table does not exist
db.TableDefs.Delete "02WA"
On Error GoTo 0 'Reset error handling
Set tdfNew = db.CreateTableDef("02WA")
With tdfNew
.Fields.Append .CreateField("WA", dbMemo)
db.TableDefs.Append tdfNew
End With
'Allow space records
With tdfNew.Fields("WA")
.Properties("AllowZeroLength") = True
End With
Dim tabname_dd03l As String
Dim db As Database, rst As Recordset
Dim intCounter As Integer, lSum As Long
Dim intUpBound As Integer
Dim tdfNew As TableDef
Set db = CurrentDb
Set rst = db.OpenRecordset("01TABNAME", dbOpenDynaset)
Set rst = db.OpenRecordset("SELECT * FROM 01tabname WHERE [CreateDD03L] = Yes and [Tabname] <> null")
Set rst = rst.OpenRecordset
On Error GoTo PROC_ERR:
rst.MoveLast
On Error GoTo 0
intUpBound = rst.RecordCount
rst.MoveFirst
For intCounter = 1 To intUpBound
tabname_dd03l = "DD03L_" & rst!tabname
Set tdfNew = db.CreateTableDef(tabname_dd03l)
With tdfNew
.Fields.Append .CreateField("Fieldname", dbText, 30)
.Fields.Append .CreateField("Position", dbInteger, 4)
.Fields.Append .CreateField("DataType", dbText, 4)
.Fields.Append .CreateField("Leng", dbInteger, 6)
.Fields.Append .CreateField("Choose_Field", dbBoolean)
.Fields.Append .CreateField("Order", dbInteger, 4)
db.TableDefs.Append tdfNew
End With
With tdfNew.Fields("Choose_Field")
.Properties.Append .CreateProperty("DisplayControl", _
dbInteger, CInt(acCheckBox))
End With
rst.MoveNext
Next intCounter
Exit Function
PROC_ERR:
MsgBox "Error " & Err.Number & " " & Err.Description & " Check field [CreateDD03L] in TABNAME"
Exit Function
' ''Add the R/3 RFC function RFC_READ_TABLE to the collection
''------------------------------------------------------------
'Set funcControl = CreateObject("SAP.Functions")
'Dim RFC_READ_TABLE As Object
'Dim db As DAO.Database, RS As DAO.Recordset
'Set RFC_READ_TABLE = funcControl.Add("RFC_READ_TABLE")
''------------------------------------------------------------
'' Create objects for each parameter
''------------------------------------------------------------
Dim db As DAO.Database, RS As DAO.Recordset
saptab = "DD03L"
Set db = CurrentDb
Set rstabname = db.OpenRecordset("SELECT * FROM 01TABNAME where FillDD03L = Yes", dbOpenDynaset)
If rstabname.RecordCount = 0 Then
MsgBox "No record in TABNAME with FillDD03L = Yes"
Exit Function
End If
On Error GoTo PROC_ERR
rstabname.MoveFirst
Do While Not rstabname.EOF ' Check if tables exist
tabname = rstabname("Tabname")
Set RS = db.OpenRecordset("DD03L_" + tabname)
RS.Close
rstabname.MoveNext
Loop
On Error GoTo 0
Set rsadmin = db.OpenRecordset("SELECT Sum(1) AS [Count] FROM 00ADMIN HAVING Active = Yes", dbOpenDynaset)
If IsNull(rsadmin("Count")) Then
MsgBox "In table 00ADMIN there is no active connection (Active = 'X')." & vbNewLine & "Please logon manually."
ActiveConnection = False
Else
Select Case rsadmin("Count")
Case Is > 1
MsgBox "In table 00ADMIN set only one active connection with Active = 'X'"
Exit Function
Case 1
ActiveConnection = True
End Select
End If
'If rsadmin("Count") > 1 Then
' MsgBox "In table 00ADMIN set only one active connection with Active = 'X'"
'Exit Function
'End If
'
'If rsadmin("Count") >= 1 Then
'' Not Null, only one connection active
'ActiveConnection = True
'Else
'' Null
' MsgBox "In table 00ADMIN there is no active connection (Active = 'X'). Please logon manually"
'ActiveConnection = False
'End If
If ActiveConnection = True Then
' Not Null, only one connection active
Set rsadmin = db.OpenRecordset("SELECT * FROM 00ADMIN where Active = Yes", dbOpenDynaset)
AdmMessageOn = rsadmin("MessageOn")
If AdmMessageOn = True Then
MsgBox "Set connection to Server: " & rsadmin("CNT_STR_APPLN_SRVR") & " Client: " & rsadmin("CNT_STR_CLIENT")
Else
RetVal = SysCmd(acSysCmdSetStatus, "Set connection to Server: " & rsadmin("CNT_STR_APPLN_SRVR") & " Client: " & rsadmin("CNT_STR_CLIENT"))
End If
End If
rstabname.MoveFirst
Do While Not rstabname.EOF
Set funcControl = CreateObject("SAP.Functions")
If ActiveConnection = True Then
' Automatic logon
Dim CNT_STR_USR As String
CNT_STR_USR = rsadmin("CNT_STR_USR")
Dim CNT_STR_PWD As String
CNT_STR_PWD = rsadmin("CNT_STR_PWD")
Dim CNT_STR_APPLN_SRVR As String
CNT_STR_APPLN_SRVR = rsadmin("CNT_STR_APPLN_SRVR")
Dim CNT_STR_SYSTEM As String
CNT_STR_SYSTEM = rsadmin("CNT_STR_SYSTEM")
Dim CNT_STR_SYS_NUM As String
CNT_STR_SYS_NUM = rsadmin("CNT_STR_SYS_NUM")
Dim CNT_STR_CLIENT As String
CNT_STR_CLIENT = rsadmin("CNT_STR_CLIENT")
Const CNT_STR_LOGON_LANG As String = "EN"
Const CNT_STR_LOG_FILE As String = "C:\sap_vb.txt"
Const CNT_INT_LOG_LEVEL As Integer = 9
Dim obSAPConn As Object
Set obSAPConn = funcControl.Connection
funcControl.LogLevel = CNT_INT_LOG_LEVEL
With obSAPConn
.ApplicationServer = CNT_STR_APPLN_SRVR
.System = CNT_STR_SYSTEM 'Added on 22.05.2017
.SystemNumber = CNT_STR_SYS_NUM
.User = CNT_STR_USR
.Password = CNT_STR_PWD
.Language = CNT_STR_LOGON_LANG
.Client = CNT_STR_CLIENT
End With
If obSAPConn.Logon(0, True) = False Then
If AdmMessageOn = True Then
MsgBox "R/3 connection failed"
Else
RetVal = SysCmd(acSysCmdSetStatus, "R/3 connection failed")
End If
Else
If AdmMessageOn = True Then
MsgBox "R/3 connection established"
Else
RetVal = SysCmd(acSysCmdSetStatus, "R/3 connection established")
End If
End If
End If
''------------
'Add the R/3 RFC function RFC_READ_TABLE to the collection
'------------------------------------------------------------
'Set funcControl = CreateObject("SAP.Functions")
Dim RFC_READ_TABLE As Object
Set RFC_READ_TABLE = funcControl.Add("RFC_READ_TABLE")
'------------------------------------------------------------
' Create objects for each parameter
'------------------------------------------------------------
Dim obSAPOption As Object
tabname = rstabname("Tabname")
Set eQUERY_TAB = RFC_READ_TABLE.Exports("QUERY_TABLE")
Set toptions = RFC_READ_TABLE.tables("OPTIONS") '
Set tdata = RFC_READ_TABLE.tables("DATA") '
Set tfields = RFC_READ_TABLE.tables("FIELDS") '
eQUERY_TAB.Value = saptab ' pQueryTab is the R/3 name of the table
Set obSAPOption = RFC_READ_TABLE.tables("OPTIONS")
If obSAPOption.rowcount = 0 Then
obSAPOption.RowS.Add
End If
obSAPOption.Value(1, "TEXT") = "Tabname EQ '" + tabname + "' and Rollname ne ''"
If RFC_READ_TABLE.call = True Then
If tdata.rowcount > 0 Then
If AdmMessageOn = True Then
MsgBox "Call to RFC_READ_TABLE successful! Data found in DD03L for " + tabname
Else
RetVal = SysCmd(acSysCmdSetStatus, "Call to RFC_READ_TABLE successful! Data found in " + tabname)
End If
Else
If AdmMessageOn = True Then
MsgBox "Call to RFC_READ_TABLE successful! No data found in DD03L for " + tabname
Else
RetVal = SysCmd(acSysCmdSetStatus, "Call to RFC_READ_TABLE successful! No data found in DD03L for " + tabname)
End If
End If
Else
If AdmMessageOn = True Then
MsgBox "Call to RFC_READ_TABLE failed!"
Else
RetVal = SysCmd(acSysCmdSetStatus, "Call to RFC_READ_TABLE failed!")
End If
End If
Dim fieldspos(4) As Integer, fieldsname(4) As String
fieldspos(1) = 2
fieldsname(1) = "FIELDNAME"
fieldspos(2) = 5
fieldsname(2) = "POSITION"
fieldspos(3) = 18
fieldsname(3) = "DataType"
fieldspos(4) = 19
fieldsname(4) = "LENG"
Set RS = db.OpenRecordset("DD03L_" + tabname)
For Each row In tdata.RowS
If row(1) <> "" Then
RS.AddNew
For i = 1 To 4
Field = tfields(fieldspos(i), 1)
Value = Trim(Mid(row(1), tfields(fieldspos(i), 2) + 1, tfields(fieldspos(i), 3)))
RS(fieldsname(i)) = Value
Next i
RS.Update
End If
tdata.RowS.Remove (1)
Next
RS.Close
Set RS = db.OpenRecordset("SELECT * FROM [DD03L_" + tabname + "] order by [position]", dbOpenDynaset)
If RS.RecordCount > 0 Then
RS.MoveFirst
intCounter = 1
Do While Not RS.EOF
RS.Edit
RS("ORDER") = intCounter
RS.Update
RS.MoveNext
intCounter = intCounter + 1
Loop
End If
RS.Close
rstabname.MoveNext
Loop
rstabname.Close
RetVal = SysCmd(acSysCmdRemoveMeter)
Exit Function
PROC_ERR:
MsgBox "Error " & Err.Number & ". " & "Check if table " & tabname & " has been created in step {02 CreateDD03L}"
Exit Function
Dim tabname_dd03l As String
Dim db As Database, rst As Recordset
Dim tdfNew As TableDef
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT * FROM 01tabname WHERE [CreateAsDD03L] = Yes and [Tabname] <> null")
Set rst = rst.OpenRecordset
rst.MoveFirst
Do While Not rst.EOF
tabname = rst("tabname")
Set rsd = db.OpenRecordset("SELECT * FROM [DD03L_" + tabname + "] WHERE [Choose_Field] = Yes order by [Order]")
Set rsd = rsd.OpenRecordset
rsd.MoveFirst
Set tdfNew = db.CreateTableDef(tabname)
Do While Not rsd.EOF
With tdfNew
.Fields.Append .CreateField(rsd("FieldName"), dbText, rsd("leng"))
End With
rsd.MoveNext
Loop
db.TableDefs.Append tdfNew
rsd.Close
rst.MoveNext
Loop
rst.Close
Dim db As DAO.Database, RS As DAO.Recordset
Dim RecordString As String
Dim obSAPOption As Object
Dim tabname As String
Dim options As String
Set db = CurrentDb
Set rstabname = db.OpenRecordset("SELECT * FROM 01TABNAME where ReadAsDD03L = Yes", dbOpenDynaset)
If rstabname.RecordCount = 0 Then
MsgBox "No record in TABNAME with ReadAsDD03L = Yes"
Exit Function
End If
'Set rsadmin = db.OpenRecordset("SELECT * FROM 00ADMIN where LogonSilient = Yes and Active = Yes", dbOpenDynaset)
''Only first record is used for logon
'If rsadmin.RecordCount <> 0 Then
' rsadmin.MoveFirst
'Else
' MsgBox "In table ADMIN set one active connection with Active = 'X'"
' Exit Function
'End If
'AdmMessageOn = rsadmin("MessageOn")
'
'If AdmMessageOn = True Then
' MsgBox "Set connection to Server: " & rsadmin("CNT_STR_APPLN_SRVR") & " Client: " & rsadmin("CNT_STR_CLIENT")
'Else
' RetVal = SysCmd(acSysCmdSetStatus, "Set connection to Server: " & rsadmin("CNT_STR_APPLN_SRVR") & " Client: " & rsadmin("CNT_STR_CLIENT"))
'End If
Set rsadmin = db.OpenRecordset("SELECT Sum(1) AS [Count] FROM 00ADMIN HAVING Active = Yes", dbOpenDynaset)
If IsNull(rsadmin("Count")) Then
MsgBox "In table 00ADMIN there is no active connection (Active = 'X')." & vbNewLine & "Please logon manually."
ActiveConnection = False
Else
Select Case rsadmin("Count")
Case Is > 1
MsgBox "In table 00ADMIN set only one active connection with Active = 'X'"
Exit Function
Case 1
ActiveConnection = True
End Select
End If
'If rsadmin("Count") > 1 Then
' MsgBox "In table 00ADMIN set only one active connection with Active = 'X'"
'Exit Function
'End If
'
'If rsadmin("Count") >= 1 Then
'' Not Null, only one connection active
'ActiveConnection = True
'Else
'' Null
' MsgBox "In table 00ADMIN there is no active connection (Active = 'X'). Please logon manually"
'ActiveConnection = False
'End If
If ActiveConnection = True Then
' Not Null, only one connection active
Set rsadmin = db.OpenRecordset("SELECT * FROM 00ADMIN where Active = Yes", dbOpenDynaset)
AdmMessageOn = rsadmin("MessageOn")
If AdmMessageOn = True Then
MsgBox "Set connection to Server: " & rsadmin("CNT_STR_APPLN_SRVR") & " Client: " & rsadmin("CNT_STR_CLIENT")
Else
RetVal = SysCmd(acSysCmdSetStatus, "Set connection to Server: " & rsadmin("CNT_STR_APPLN_SRVR") & " Client: " & rsadmin("CNT_STR_CLIENT"))
End If
End If
rstabname.MoveFirst
Do While Not rstabname.EOF
tabname = rstabname("Tabname")
If IsNull(Trim(rstabname("Options"))) Then
msg_options = "EMPTY"
Else
msg_options = Trim(rstabname("Options"))
End If
'->Check if sum of length not greater then 512, SE37/RFC_READ_TABLE, TABLES/TAB512
tabnamedd = "[DD03L_" + tabname + "]"
tableng = "SELECT Sum(" + tabnamedd + "." + "Leng) AS SumOfLeng " & _
"FROM " + tabnamedd + " " & _
"WHERE (((" + tabnamedd + ".Choose_Field)=Yes))"
Set rsleng = db.OpenRecordset(tableng, dbOpenDynaset)
valleng = rsleng("SumOfLeng")
If valleng > 512 Then
MsgBox "In table " & tabnamedd & " sum of fields length " + CStr(valleng) + " > 512. Execution terminated."
Exit Function
End If
'<-
Set funcControl = CreateObject("SAP.Functions")
'If rsadmin.RecordCount <> 0 Then
If ActiveConnection = True Then
' Automatic logon
Dim CNT_STR_USR As String
CNT_STR_USR = rsadmin("CNT_STR_USR")
Dim CNT_STR_PWD As String
CNT_STR_PWD = rsadmin("CNT_STR_PWD")
Dim CNT_STR_APPLN_SRVR As String
CNT_STR_APPLN_SRVR = rsadmin("CNT_STR_APPLN_SRVR")
Dim CNT_STR_SYSTEM As String
CNT_STR_SYSTEM = rsadmin("CNT_STR_SYSTEM")
Dim CNT_STR_SYS_NUM As String
CNT_STR_SYS_NUM = rsadmin("CNT_STR_SYS_NUM")
Dim CNT_STR_CLIENT As String
CNT_STR_CLIENT = rsadmin("CNT_STR_CLIENT")
Const CNT_STR_LOGON_LANG As String = "EN"
Const CNT_STR_LOG_FILE As String = "C:\sap_vb.txt"
Const CNT_INT_LOG_LEVEL As Integer = 9
Dim obSAPConn As Object
Set obSAPConn = funcControl.Connection
funcControl.LogLevel = CNT_INT_LOG_LEVEL
With obSAPConn
.ApplicationServer = CNT_STR_APPLN_SRVR
.System = CNT_STR_SYSTEM 'Added on 22.05.2017
.SystemNumber = CNT_STR_SYS_NUM
.User = CNT_STR_USR
.Password = CNT_STR_PWD
.Language = CNT_STR_LOGON_LANG
.Client = CNT_STR_CLIENT
End With
If obSAPConn.Logon(0, True) = False Then
If AdmMessageOn = True Then
MsgBox "R/3 connection failed"
Else
RetVal = SysCmd(acSysCmdSetStatus, "R/3 connection failed")
End If
Else
If AdmMessageOn = True Then
MsgBox "R/3 connection established"
Else
RetVal = SysCmd(acSysCmdSetStatus, "R/3 connection established")
End If
End If
End If
'Add the R/3 RFC function RFC_READ_TABLE to the collection
'------------------------------------------------------------
'Set funcControl = CreateObject("SAP.Functions")
Dim RFC_READ_TABLE As Object
Set RFC_READ_TABLE = funcControl.Add("RFC_READ_TABLE")
'------------------------------------------------------------
' Create objects for each parameter
'------------------------------------------------------------
Set eQUERY_TAB = RFC_READ_TABLE.Exports("QUERY_TABLE")
Set toptions = RFC_READ_TABLE.tables("OPTIONS") '
Set tdata = RFC_READ_TABLE.tables("DATA") '
Set tfields = RFC_READ_TABLE.tables("FIELDS") '
eQUERY_TAB.Value = tabname ' pQueryTab is the R/3 name of the table
Set obSAPOption = RFC_READ_TABLE.tables("OPTIONS")
If Trim(rstabname("Options")) <> "" Then
obSAPOption.RowS.Add
obSAPOption.Value(1, "TEXT") = rstabname("Options")
End If
'DEVELOP
Set RsDD03L = db.OpenRecordset("SELECT * FROM [DD03L_" + tabname + "] where Choose_Field = Yes order by [order]", dbOpenDynaset)
RsDD03L.MoveFirst
Do While Not RsDD03L.EOF
tfields.RowS.Add
tfields.Value(RsDD03L.AbsolutePosition + 1, "FIELDNAME") = RsDD03L("Fieldname")
RsDD03L.MoveNext
Loop
RsDD03L.Close
RetVal = SysCmd(acSysCmdSetStatus, "Reading data from table " & tabname & " with OPTIONS: " & msg_options & ".")
If RFC_READ_TABLE.call = True Then
If tdata.rowcount > 0 Then
If AdmMessageOn = True Then
MsgBox "Call to RFC_READ_TABLE successful! Data found in " + tabname + " with OPTIONS: " + msg_options + "."
End If
Else
If AdmMessageOn = True Then
MsgBox "Call to RFC_READ_TABLE successful! No data found in " + tabname + " with OPTIONS: " + msg_options + "."
End If
End If
Else
If AdmMessageOn = True Then
MsgBox "Call to RFC_READ_TABLE failed!"
End If
End If
Set RsDD03L = db.OpenRecordset("SELECT * FROM [DD03L_" + tabname + "] where Choose_Field = Yes order by [order]", dbOpenDynaset)
Dim sqlon As Boolean
sqlon = rstabname("ReadAsSQLon")
If sqlon = False Then
Set RS = db.OpenRecordset(tabname)
End If
Dim CurrentRow As Long
Dim count As Long
Dim Sinit As Date
Dim Snow As Date
Dim Spass As Long
Dim RowS As String
count = tdata.rowcount
CurrentRow = 0
Sinit = Now()
If sqlon = True Then
db.Execute ("delete * from 02WA")
sql_fieldpos = RsDD03L.RecordCount
End If
For Each row In tdata.RowS
'-> Speed on status bar
CurrentRow = CurrentRow + 1
Snow = Now()
Spass = DateDiff("s", Sinit, Snow)
Avg = Round(1000 * (Spass / CurrentRow), 2)
RetVal = SysCmd(acSysCmdSetStatus, "Inserting Records in " + tabname + " ..." + CStr(CurrentRow) + " of " + CStr(count) + ". Elapsed time " + CStr(Spass) + " sec." + " Average 1000 rec./" + CStr(Avg) + " sec.")
'<-
'SQL->
'http://msdn.microsoft.com/en-us/library/ms188365.aspx
'BULK INSERT bulktest..t_float
'FROM 'C:\t_float-c.dat' WITH (FORMATFILE='C:\t_floatformat-c-xml.xml');
'GO
'If accepted loop can be replaced with one SQL string and better performance
If sqlon = True Then
If row(1) <> "" Then
RowS = Replace(row(1), "'", "''")
row_len_req = Val(tfields((sql_fieldpos), 2)) + Val(tfields(sql_fieldpos, 3))
'In order to complete to full length when last field is empty
RowS = RowS + String(row_len_req - Len(RowS), " ") + "!"
db.Execute ("INSERT INTO 02WA (WA) VALUES ('" & RowS & "')")
End If
End If
'SQL<-
If sqlon = False Then
RecordString = ""
If row(1) <> "" Then
RS.AddNew
RsDD03L.MoveFirst
Do While Not RsDD03L.EOF
fieldpos = RsDD03L.AbsolutePosition + 1
FieldName = RsDD03L("Fieldname")
Field = tfields(fieldpos, 1)
Value = Trim((Mid(row(1), tfields((fieldpos), 2) + 1, tfields(fieldpos, 3))))
If Value <> "" Then
RS(FieldName) = Value
End If
RecordString = RecordString + Value
RsDD03L.MoveNext
Loop
If RecordString <> "" Then
RS.Update
End If
End If
End If
Next
If sqlon = False Then
RS.Close
End If
'SQL->
If sqlon = True Then
StrInto = "": StrFrom = "": St = "": En = ""
Set RsDD03L = db.OpenRecordset("SELECT * FROM [DD03L_" + tabname + "] where Choose_Field = Yes order by [order]", dbOpenDynaset)
RsDD03L.MoveFirst
Do While Not RsDD03L.EOF
fieldpos = RsDD03L.AbsolutePosition + 1
FieldName = RsDD03L("Fieldname")
Field = tfields(fieldpos, 1)
If fieldpos <> 1 Then
StrSp = ", "
Else
StrSp = ""
End If
StrInto = StrInto + StrSp + "[" + FieldName + "]"
St = Val(tfields((fieldpos), 2) + 1)
En = Val(tfields(fieldpos, 3))
StrFrom = StrFrom + StrSp + "Mid([02WA]![WA]," + CStr(St) + "," + CStr(En) + ") AS " + "[" + FieldName + "]"
RsDD03L.MoveNext
Loop
RsDD03L.Close
StrSQL = "INSERT INTO " & _
" [" + tabname + "] " & _
" ( " & _
StrInto & _
")" & _
" SELECT " & _
StrFrom & _
" FROM 02WA"
'Uncomment in order to insert generated SQL in last row of 02WA for analysis purpose
'db.Execute ("INSERT INTO 02WA (WA) VALUES ('" & StrSQL & "')")
db.Execute (StrSQL)
End If
'SQL<-
rstabname.MoveNext
Loop
rstabname.Close
rsadmin.Close
RetVal = SysCmd(acSysCmdRemoveMeter)
Dim dbs As Database, rst As Recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("SELECT * FROM 01tabname WHERE [DeleteAllData] = Yes and [Tabname] <> null")
Set rst = rst.OpenRecordset
If rst.RecordCount <> 0 Then
rst.MoveFirst
Do While Not rst.EOF
dbs.Execute "DELETE * FROM [" & rst("Tabname") & "]"
rst.MoveNext
Loop
rst.Close
End If
RetVal = SysCmd(acSysCmdInitMeter, "Ready", 1)
RetVal = SysCmd(acSysCmdRemoveMeter)
© Copyright 2012. All rights reserved.
No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of Author.
The information contained herein may be changed without prior notice.
Nothing herein should be construed as constituting an additional warranty.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
6 | |
5 | |
3 | |
3 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 |