We are a group of users of Blackbaud products and are not affiliated with Blackbaud. We'd love to have you join our community to help and be helped in getting the most from your Blackbaud software.
Register now to join us to get independant advice on your system, connect with 3rd party consultants to help you maximize your database and have a real alternative to the official Blackbaud website.
One of the things I found most helpful when learning VBA was viewing some of the utility code that someone else had left on our instance of RE. Not only were they great examples, but they really simplified tasks which I find I need to do all the time.
I'm sure we all have a long list of functions we call on a regular basis that we've created, so don't keep them a secret, share them with the community! Maybe someone keen could create a wiki if the thread gets too big...
So my first submission is a simple procedure that loads a query into a listview control.
Caveat-no error trapping in this one!
Code:
Public Sub LoadQueryIntoListView(oListView As ListView, strQueryName As String)
Dim ColCtr As Long
Dim RowCtr As Long
Dim strRowData As String
Dim strColName As String
Dim oQueryObject As CQueryObject
Set oQueryObject = New CQueryObject
oQueryObject.Init REApplication.SessionContext
'Load using the Query name
oQueryObject.LoadByField uf_QUERY_NAME, strQueryName
'This opens the resultset for access
oQueryObject.QuerySet.OpenQuerySet
' Load Array Headers into ListView
oListView.ColumnHeaders.Clear
' Add the row number column as column 0
With oListView.ColumnHeaders.Add
.Text = "Row"
.Width = 20
End With
' Now load the columns from the query
For ColCtr = 1 To oQueryObject.QuerySet.FieldCount
strColName = oQueryObject.QuerySet.FieldName(ColCtr)
With oListView.ColumnHeaders.Add
.Text = strColName
' .Width = Utils.GetColumnWidth(strColName) * 0.05 'Use your own code here if you like
End With
Next
If oQueryObject.QuerySet.NumberOfRecords > 0 Then
' Now load the Rows from the query
For RowCtr = 1 To oQueryObject.QuerySet.NumberOfRecords
oListView.ListItems.Add , , RowCtr
For ColCtr = 1 To oQueryObject.QuerySet.FieldCount
oListView.ListItems(RowCtr).SubItems(ColCtr) = oQueryObject.QuerySet.fieldValue(ColCtr)
Next ColCtr
oQueryObject.QuerySet.MoveNext
Next RowCtr
End If
' Tidy Up
oQueryObject.Closedown
Set oQueryObject = Nothing
End Sub