G
Gordon Bentley-Mix on news.microsoft.com
I have a UserForm that contains functionality to collected multiple sets of
data points for similar things; e.g. the details of parties to a contract or
the details of rented goods. I collect the information in an array, which is
declared as Public (because I follow good coding practices and use separate
modules for UserForm-related and document-related operations and the data is
used in both places).
An example of the code for doing this, which is called in the Click event of
a button on the UserForm:
Private Sub AddCustomer()
CustomerCount = CustomerCount + 1
ReDim Preserve CustomersArray(1, 1 To CustomerCount) As Variant
CustomersArray(0, CustomerCount) = Trim(txtCustomerName.Value)
CustomersArray(1, CustomerCount) = Trim(txtCustomerNumber.Value)
End Sub
Further, I save the values from the arrays into document variables so I can
retrieve them to rerun the code. An example of the code that does this:
Private Sub SaveCustomerVariables()
Dim i As Long
Dim n As Long
Dim myVariable As String
For i = 1 To UBound(CustomersArray, 2)
For n = 0 To UBound(CustomersArray, 1)
myVariable = "Customer" & i & "Variable" & n
SaveVariableValue myVariable, CustomersArray(n, i)
Next n
Next i
End Sub
And an example of the code that loads them back into the UserForm on a rerun:
Private Sub LoadCustomerVariables()
Dim i As Long
Dim n As Long
Dim myVariable As String
ReDim CustomersArray(1, 1 To CustomerCount) As Variant
For i = 1 To CustomerCount
For n = 1 To 2
myVariable = "Customer" & i & "Variable" & n
CustomersArray(n - 1, i) = fcnLoadTextVariableValue(myVariable)
Next n
Next i
CheckCustomerButtons
LoadCustomersList
End Sub
And if the template is being rerun, I also have code that deletes any
existing doc vars prior to saving the new ones. An example of the code that
does this:
Private Sub DeleteCustomerVariables()
Dim PreviousCustomerCount As Long
Dim i As Long
Dim n As Long
Dim myVariable As String
With myDoc
If fcnFindVariable("CustomerCount") = True Then
PreviousCustomerCount =
fcnLoadNumericVariableValue("CustomerCount")
For i = 1 To PreviousCustomerCount
For n = 1 To 2
myVariable = "Customer" & i & "Variable" & n
DeleteVariable myVariable
Next n
Next i
End If
End With
End Sub
Now here's where things get fun.
Any one template may have as many as five (or more) instances of similar
functionality, which means five arrays and - more importantly - five separate
routines each for saving, retrieving and deleting the doc vars related to
each instance of the functionality. Obviously these are prime candidates for
using a 'generic' routine that accepts arguments so as to cut down on the
number of lines of code and make maintenance easier. And to that end, I've
developed such a routine for deleting the variables, which looks like this:
Public Sub DeleteArrayVariables(myName As String, myCount As Long)
Dim CountVariableName As String
Dim PreviousCount As Long
Dim i As Long
Dim n As Long
Dim myVariableName As String
CountVariableName = myName & "Count"
With myDoc
If fcnFindVariable(CountVariableName) = True Then
PreviousCount = fcnLoadNumericVariableValue(CountVariableName )
For i = 1 To PreviousCount
For n = 0 To myCount
myVariableName = myName & i & "Variable" & n
DeleteVariable myVariableName
Next n
Next i
End If
End With
End Sub
and gets called like this:
Private Sub DeleteCustomerVariables()
DeleteArrayVariables "Customer", 1
End Sub
However, what I'm struggling with is the routines for saving and retrieving
the array values - especially around how to identify the array used in the
routine. It seems to me that since the arrays are declared as Public, there
should be a way to just pass in (or build) the name of the array rather than
pass in the entire array. Suggestions?
--
Cheers!
Gordon Bentley-Mix
Word MVP
Please post all follow-ups to the newsgroup.
Read the original version of this post in the Office Discussion Groups - no
membership required!
data points for similar things; e.g. the details of parties to a contract or
the details of rented goods. I collect the information in an array, which is
declared as Public (because I follow good coding practices and use separate
modules for UserForm-related and document-related operations and the data is
used in both places).
An example of the code for doing this, which is called in the Click event of
a button on the UserForm:
Private Sub AddCustomer()
CustomerCount = CustomerCount + 1
ReDim Preserve CustomersArray(1, 1 To CustomerCount) As Variant
CustomersArray(0, CustomerCount) = Trim(txtCustomerName.Value)
CustomersArray(1, CustomerCount) = Trim(txtCustomerNumber.Value)
End Sub
Further, I save the values from the arrays into document variables so I can
retrieve them to rerun the code. An example of the code that does this:
Private Sub SaveCustomerVariables()
Dim i As Long
Dim n As Long
Dim myVariable As String
For i = 1 To UBound(CustomersArray, 2)
For n = 0 To UBound(CustomersArray, 1)
myVariable = "Customer" & i & "Variable" & n
SaveVariableValue myVariable, CustomersArray(n, i)
Next n
Next i
End Sub
And an example of the code that loads them back into the UserForm on a rerun:
Private Sub LoadCustomerVariables()
Dim i As Long
Dim n As Long
Dim myVariable As String
ReDim CustomersArray(1, 1 To CustomerCount) As Variant
For i = 1 To CustomerCount
For n = 1 To 2
myVariable = "Customer" & i & "Variable" & n
CustomersArray(n - 1, i) = fcnLoadTextVariableValue(myVariable)
Next n
Next i
CheckCustomerButtons
LoadCustomersList
End Sub
And if the template is being rerun, I also have code that deletes any
existing doc vars prior to saving the new ones. An example of the code that
does this:
Private Sub DeleteCustomerVariables()
Dim PreviousCustomerCount As Long
Dim i As Long
Dim n As Long
Dim myVariable As String
With myDoc
If fcnFindVariable("CustomerCount") = True Then
PreviousCustomerCount =
fcnLoadNumericVariableValue("CustomerCount")
For i = 1 To PreviousCustomerCount
For n = 1 To 2
myVariable = "Customer" & i & "Variable" & n
DeleteVariable myVariable
Next n
Next i
End If
End With
End Sub
Now here's where things get fun.
Any one template may have as many as five (or more) instances of similar
functionality, which means five arrays and - more importantly - five separate
routines each for saving, retrieving and deleting the doc vars related to
each instance of the functionality. Obviously these are prime candidates for
using a 'generic' routine that accepts arguments so as to cut down on the
number of lines of code and make maintenance easier. And to that end, I've
developed such a routine for deleting the variables, which looks like this:
Public Sub DeleteArrayVariables(myName As String, myCount As Long)
Dim CountVariableName As String
Dim PreviousCount As Long
Dim i As Long
Dim n As Long
Dim myVariableName As String
CountVariableName = myName & "Count"
With myDoc
If fcnFindVariable(CountVariableName) = True Then
PreviousCount = fcnLoadNumericVariableValue(CountVariableName )
For i = 1 To PreviousCount
For n = 0 To myCount
myVariableName = myName & i & "Variable" & n
DeleteVariable myVariableName
Next n
Next i
End If
End With
End Sub
and gets called like this:
Private Sub DeleteCustomerVariables()
DeleteArrayVariables "Customer", 1
End Sub
However, what I'm struggling with is the routines for saving and retrieving
the array values - especially around how to identify the array used in the
routine. It seems to me that since the arrays are declared as Public, there
should be a way to just pass in (or build) the name of the array rather than
pass in the entire array. Suggestions?
--
Cheers!
Gordon Bentley-Mix
Word MVP
Please post all follow-ups to the newsgroup.
Read the original version of this post in the Office Discussion Groups - no
membership required!