Building Views with Arrays
So far we’ve seen examples of passing numbers, strings, and Python objects back and forth between Python and VB. Now we’ll develop some more kinds of views and look at passing around arrays of data at a time. This is a real time saver and can simplify the preceding code in several places.
Many financial reports take the form of a 2D grid or spreadsheet.
What’s needed is a generic solution for getting a Python list
of lists into such a grid. In the last chapter we showed a number of
BookSet methods for getting a list of accounts,
the details of an account and so on. For example,
getAccountDetails returns a list of tuples of
(transaction
number,
date, comment,
amount, runningTotal). This is
effectively a matrix with five columns but an unknown number of rows.
You need to expose this the usual way in
COMBookSet, but then access the entire matrix from
VB. The Python COM framework automatically converts lists (or lists
of lists, or lists of tuples) into arrays that can be accessed in VB.
We’ve created a new form, frmAccountView,
which can be given the name of an account as a string. This has a
Microsoft FlexGrid control called grdTable.
Here’s the UpdateView method that fetches
and displays the data:
Public Sub UpdateView() Dim table As Variant Dim rows As Integer, cols As Integer Dim row As Integer, col As Integer table = frmMain.BookServer.getAccountDetails(AccountName) rows = UBound(table, 1) - LBound(table, 1) + 1 cols = UBound(table, 2) - LBound(table, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access