Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing XML values to arrays and then to sub routines

Status
Not open for further replies.

TheFitz

Programmer
Dec 18, 2003
140
0
0
GB
Hi All,

I've got a dynamically created XML file which I'm reading in loops. I am trying to put the nodename and value into a 2 dimentional array. As I do not know the number of sibling nodes it may have, I'm trying to redim the array on the fly.

The array is defined as:

Code:
Dim aryUpdate() As String

Sample code below:

Code:
...
If objNodeData.nodeName <> "" Then
    ReDim Preserve aryUpdate(intL4, 1)
End If

aryUpdate(intL4, 0) = objNodeData.nodeName
aryUpdate(intL4, 1) = objNodeData.Text
...

Later on, I'm calling a subroutine to insert/update/delete the values from the associated table, by using the following call:

Code:
Call UpdateTableValues(strTableName, strUpdateType, strKeyField, strKeyValue, aryUpdate)

I am then using the definition in the called subroutine as below:

Code:
Sub UpdateTableValues(strTableName As String, strUpdateType As String, strKeyField As String, strKeyValue As String, aryValues() As String)

MsgBox strTableName & vbCrLf & strUpdateType & vbCrLf & strKeyField & vbCrLf & strKeyValue & vbCrLf & aryValues(4, 1)

Here is the problem, at the point where I am trying to redefine the array, if I don't use the preserve option, I lose the data, however, the preserve option gives me the error "Subscript out of range". The values for the array are aryUpdate(1,1) when reading the intl4 variable.

Can some help please. . . .

TIA,

End Sub

Fitz
Did you know, there are 10 types of people in this world:
* Those who understand binary
and
* Those who Don't!!
 
Just curious.... What are you trying to do with this XML data? Is your goal to put this in a database (and that's it)? If so, what type of database (SQL Server, Access, MySQL, Oracle, etc...)?

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
It's an Access Database. The function is a generic function designed to work to upload a various number of fields to vaious tables.



Fitz
Did you know, there are 10 types of people in this world:
* Those who understand binary
and
* Those who Don't!!
 
If you were using SQL Server, I would have suggested a purely T-SQL approach to this problem. (which is why I asked).

Now... according to the help topic for REDIM...

If you use the Preserve keyword, you can resize only the last array dimension and you can't change the number of dimensions at all.

Take a look at this example (start a new VB6 project with a single button and copy/paste the code below).

Code:
Option Explicit

Private Sub Command1_Click()
    
    Dim aryUpdate() As String
    
    Dim i As Long
    Dim j As Long
    
    ' put one element in the array
    ReDim Preserve aryUpdate(1, 0)
    aryUpdate(0, 0) = "Blue"
    aryUpdate(1, 0) = "Grape"
    
    ' add more items
    ReDim Preserve aryUpdate(1, UBound(aryUpdate, 2) + 1)
    aryUpdate(0, UBound(aryUpdate, 2)) = "Red"
    aryUpdate(1, UBound(aryUpdate, 2)) = "Strawberry"
    
    ' add more items
    ReDim Preserve aryUpdate(1, UBound(aryUpdate, 2) + 1)
    aryUpdate(0, UBound(aryUpdate, 2)) = "Yellow"
    aryUpdate(1, UBound(aryUpdate, 2)) = "Banana"
    
    ' let's see what is in our array
    
    For i = LBound(aryUpdate, 2) To UBound(aryUpdate, 2)
        For j = LBound(aryUpdate, 1) To UBound(aryUpdate, 1)
            Debug.Print aryUpdate(j, i)
        Next
        Debug.Print ""
    Next
    
End Sub


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top