Wrox Programmer Forums
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 16th, 2004, 04:42 AM
Registered User
 
Join Date: May 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default constant array

can any1 pls kindly show me how to declare a constant multi dimension array? i try this but it's wrong...

const arr(3,3) as string = ("a1","a2","a3","b1","b2","b3","c1","c2","c3")


thanks!
snowbelly.

 
Old June 16th, 2004, 04:24 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

I don't believe its possible to declare a constant array using VBA (or VB for that matter), though anyone please correct me if that isn't the case. One suggestion, though, that might make your code more maintainable if you are using a large number of constant elements would be to maintain a table of constant values, and load your array from the table. The easiest way I can think of to get your sample values into a multi-dimensional array would be to first load a one-dimensional array, and use that to load the multi-dimensional array (looking for a short cut if one is out there).

The code below prints the following to the debug window:

One-Dimensional Array

A1
A2
A3
B1
B2
B3
C1
C2
C3

Two-Dimensional Array

          Col 1 Col 2 Col 3
Row 0 A1 A2 A3
Row 1 B1 B2 B3
Row 2 C1 C2 C3

Also included is code to load the 1-dimensional array from a text file if you wanted to store your contants there.

*******Code*************************************** ****************

Dim m_arrConstants(2, 2) As String

Sub LoadOneDimensionalArray()

    Dim cnn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim arrConstants() As String
    Dim intCount As Integer

    Set cnn = CurrentProject.Connection
    Set rst = New ADODB.Recordset
    rst.Open "tblConstants", cnn, adOpenStatic, adLockPessimistic

    intCount = 0

    Debug.Print "One-Dimensional Array" & vbCrLf

    With rst
        Do Until .EOF
            ReDim Preserve arrConstants(intCount)
            arrConstants(intCount) = rst!ConstDesc
            Debug.Print arrConstants(intCount)
            intCount = intCount + 1
            .MoveNext
         Loop
         .Close
    End With

    Set rst = Nothing

    Call LoadArray(arrConstants())
    Call PrintMultidimensionalArray

End Sub

'**LOAD MULTIDIMENSIONAL ARRAY FROM 1-DIMENSIONAL ARRAY
Sub LoadArray(a() As String)

    Dim row As Integer, col As Integer
    Dim intCount As Integer

    intCount = 0

    For row = LBound(m_arrConstants) To UBound(m_arrConstants)
        For col = LBound(m_arrConstants, 2) To UBound(m_arrConstants, 2)
            m_arrConstants(row, col) = a(intCount)
            intCount = intCount + 1
        Next col
    Next row
End Sub

'**PRINT MULTIDIMENTIONAL ARRAY
Sub PrintMultidimensionalArray()
    Dim row As Integer, col As Integer
    Dim temp As String

    Debug.Print vbCrLf & "Two-Dimensional Array" & vbCrLf

    temp = " Col 1 Col 2 Col 3"
    Debug.Print Space$(9) & temp

    For row = LBound(m_arrConstants) To UBound(m_arrConstants)
        temp = "Row " & row & " "
        For col = LBound(m_arrConstants, 2) To UBound(m_arrConstants, 2)
            temp = temp & Space$(4) & m_arrConstants(row, col) & " "
        Next col
        Debug.Print temp
    Next row

End Sub

'**LOAD 1-DIMENSIONAL ARRAY FROM A TEXT FILE
Sub LoadFile(strSourceFile As String)
    Dim arrConstants()
    Dim intFileDesc As Integer 'File descriptor for output file
    Dim itCount As Integer

    intCount = 0

    intFileDesc = FreeFile
    Open strSourceFile For Input As #intFileDesc
    Do While Not EOF(intFileDesc)
        Line Input #intFileDesc, strTextLine 'Read line into buffer
        ReDim Preserve arrConstants(intCount)
        arrConstants(intCount) = strTextLine
        Debug.Print arrConstants(intCount)
        intCount = intCount + 1
    Loop
    Close #intFileDesc

End Sub






Similar Threads
Thread Thread Starter Forum Replies Last Post
constant reference in C#? texasraven C# 19 December 13th, 2007 08:28 AM
C# constant functions viveksrivastava C# 4 December 6th, 2007 10:44 PM
A constant value is expected dcct84 C# 2 October 1st, 2007 09:08 AM
Constant pointer and pointer to a constant amit_mande@yahoo.com C++ Programming 2 June 5th, 2007 01:39 AM
'Constant' Error MattLeek Access VBA 2 September 14th, 2003 08:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.