APISDWTutorialVBA: Difference between revisions

From ReliaWiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
Line 17: Line 17:
1. Before we begin, you'll need to reference the Microsoft Excel Object Library. Choose '''Tools > Reference''', and then select the library from the list. Click '''OK'''.
1. Before we begin, you'll need to reference the Microsoft Excel Object Library. Choose '''Tools > Reference''', and then select the library from the list. Click '''OK'''.


2. Create a new module. Start with the following code to create an SDW data collection. The code uses the <code>RawDataSet</code> class to represent an SDW data collection and the <code>ExtractedType</code> property to specify that the data collection is for use with Weibull++.  
2. Create a new module. Start with the following code to create an SDW data collection. The code uses the <code>RawDataSet</code> class to represent an SDW data collection, and the <code>ExtractedType</code> property to specify that the data collection is for use with Weibull++.  


  '''VBA'''
  '''VBA'''
Line 31: Line 31:
3. Next, use the <code>RawData</code> class to represent a single row of data, and then use the <code>AddDataRow</code> method to add the row to the data collection.
3. Next, use the <code>RawData</code> class to represent a single row of data, and then use the <code>AddDataRow</code> method to add the row to the data collection.


The following code demonstrates one way to extract a row of data from the Excel sheet. For <code>i = 2</code>, the code creates a new <code>RawData</code> object, and then sets its properties by using the values from row #2 of the Excel file. After the code finishes reading the row of data, it adds the object to the data collection. When the code reaches <code>Next i</code>, it increases <code>i</code> with 1 and jumps back to the <code>For</code> statement, which adds another new <code>RawData</code> object for the data in row #3 of the Excel sheet and so on. The loop continues until <code>i</code> reaches the specified number of rows (MaxRow).   
The following code demonstrates one way to extract a row of data from the Excel sheet. For <code>i = 2</code>, the code creates a new <code>RawData</code> object, and then sets its properties by using the values from row #2 of the Excel file. After the code finishes reading the row of data, it adds the object to the data collection. When the code reaches <code>Next i</code>, it increases <code>i</code> with 1 and jumps back to the <code>For</code> statement, which adds another new <code>RawData</code> object for the data in row #3 of the Excel sheet, and so on. The loop continues until <code>i</code> reaches the specified number of rows (MaxRow).   


  '''VBA'''
  '''VBA'''

Revision as of 20:36, 17 November 2015

Template:InProgress

APIWiki.png


<< Back to Quick Start Guide

Synthesis Data Warehouse (SDW) Tutorial

This VBA tutorial will demonstrate one approach to read data from an Excel file and extract it for the SDW. Note that this tutorial is for demonstration purposes only and does not include any exception handling. To complete this tutorial successfully, please use the sample Excel data that was provided for this exercise.



Transfer Data from an Excel File to the SDW (VBA)

1. Before we begin, you'll need to reference the Microsoft Excel Object Library. Choose Tools > Reference, and then select the library from the list. Click OK.

2. Create a new module. Start with the following code to create an SDW data collection. The code uses the RawDataSet class to represent an SDW data collection, and the ExtractedType property to specify that the data collection is for use with Weibull++.

VBA

 'Declare a new instance of the RawDataSet class. 
  Dim DataCollection As New RawDataSet

 'Name the data collection "New Data Collection," and then specify that 
 'it is for use with Weibull++. 
  DataCollection.ExtractedName = "New Data Collection"
  DataCollection.ExtractedType = RawDataSetType_Weibull

3. Next, use the RawData class to represent a single row of data, and then use the AddDataRow method to add the row to the data collection.

The following code demonstrates one way to extract a row of data from the Excel sheet. For i = 2, the code creates a new RawData object, and then sets its properties by using the values from row #2 of the Excel file. After the code finishes reading the row of data, it adds the object to the data collection. When the code reaches Next i, it increases i with 1 and jumps back to the For statement, which adds another new RawData object for the data in row #3 of the Excel sheet, and so on. The loop continues until i reaches the specified number of rows (MaxRow).

VBA

 'Declare a new instance of the RawData class. 
  Dim Row As New RawData

 'Read each row of data from the Excel sheet. 
  Dim i As Integer, MaxRow As Integer
  MaxRow = 20

  For i = 2 to MaxRow
      Set Row = New RawData

     'Set the desired properties for the current row of data. 
      Row.StateFS = Sheet1.Cells(i, 1)
      Row.StateTime = Sheet1.Cells(i,2)
      Row.FailureMode = Sheet1.Cells(i,3)

     'Add the current row to the data collection. 
      Call DataCollection.AddDataRow(Row)
  Next i

4. Now that the data from the Excel sheet have been copied to the data collection, the next step is to save the data collection to the Synthesis repository.

Connect to the Synthesis repository and then use the DataWarehouse.SaveRawDataSet method to save the data collection to the repository.

VBA

 'Connect to the Synthesis repository. The following code assumes that 
 'a standard repository called "RSRepository1.rsr10" exists in the C: drive.   
  Dim MyRepository As New Repository   
  MyRepository.ConnectToRepository ("C:\RSRepository1.rsr10")

 'Save the extracted data collection to the repository. 
  Call MyRepository.DataWarehouse.SaveRawDataSet(DataCollection)

Test the Code

Let's verify whether the code can import the data successfully into the SDW.

Below is the complete code list for this example. On the Debug menu, click Start to run the application. Then verify that the data were imported by launching Weibull++, opening the SDW (Home > Synthesis > Synthesis Data Warehouse) and then selecting the new data collection. (You may need to display the StateTime, StateFS and FailureMode columns by right-clicking a column header and choosing Select Columns.)

VBA
 
Sub Main()

 'Declare a new instance of the RawDataSet class. 
  Dim DataCollection As New RawDataSet

 'Name the data collection "New Data Collection," and then specify that 
 'it is for use with Weibull++. 
  DataCollection.ExtractedName = "New Data Collection"
  DataCollection.ExtractedType = RawDataSetType_Weibull

 'Declare a new instance of the RawData class. 
  Dim Row As New RawData

 'Read each row of data from the Excel sheet. 
  Dim i As Integer, MaxRow As Integer
  MaxRow = 20

  For i = 2 to MaxRow
      Set Row = New RawData

     'Set the desired properties for the current row of data. 
      Row.StateFS = Sheet1.Cells(i, 1)
      Row.StateTime = Sheet1.Cells(i,2)
      Row.FailureMode = Sheet1.Cells(i,3)

     'Add the current row to the data collection. 
      Call DataCollection.AddDataRow(Row)
  Next i

 'Connect to the Synthesis repository. The following code assumes that 
 'a standard repository called "RSRepository1.rsr10" exists in the C: drive. 
  Dim MyRepository As New Repository
  MyRepository.ConnectToRepository ("C:\RSRepository1.rsr10")

 'Save the extracted data collection to the repository. 
  Call MyRepository.DataWarehouse.SaveRawDataSet(DataCollection)

End Sub

References

To learn more, see the reference documentation for the classes and methods discussed in this section: