[AccessD] Check if array is initialized

A.D.Tejpal adtp at touchtelindia.net
Thu Jun 2 09:34:32 CDT 2005


SD,

    Function Fn_IsInitialized() given below is another simplified  way of getting the desired information. It will return true only if the argument is an array which has been re-dimmed. The following sub-routine illustrates the use of this function. If statement (A) is enabled, the immediate window will show True, otherwise False.

Sub P_Test_IfArrayInitialized()
    Dim StrArray() As String
    ReDim StrArray(5)     ' (A)
    Debug.Print Fn_IsInitialized(StrArray)
End Sub

Best wishes,
A.D.Tejpal
--------------

========================================
Function Fn_IsInitialized(ByVal varArray _
                                        As Variant) As Boolean
    Dim Rtv As Variant    
    On Error Resume Next
    Fn_IsInitialized = Not IsError(Rtv = UBound(varArray))
End Function
========================================

  ----- Original Message ----- 
  From: Sad Der 
  To: Acces User Group 
  Sent: Thursday, June 02, 2005 16:43
  Subject: [AccessD] Check if array is initialized


  Hi group,

  one of our programmers came to me with a question
  regarding an error when the UBOUND() of an array was
  retrieved. The error occures when an array has been
  DIMmed, without specifing the size, but not REDIMmed.

  In the sample below I've created a simple example.
  When the function is called a byref array is to be
  filled with values. However the line:
    for lngCntr = 0 to UBOUND(r_alngMyArray)
  Will always fill since it hasn't been ReDimmed.

  I suggested a workaround:
  1 - use the function IsInitialized() and check if the
  function returns -1. If so the array isnt't
  intitialized.

  However, is there an official way of handling this
  error/problem/occurence?

  Thnx

  Regards,

  SD
  --------------------
  EXAMPLE
  --------------------

  Main
    Dim alngValues() as long
    Call RetrieveSomething(alngValues())
  End Main

  Public Function RetrieveSomething(byref
  r_alngMyArray() as Long)
  dim lngCntr as long 

  for lngCntr = 0 to UBOUND(r_alngMyArray)
    Redim PRESERVE r_alngMyArray(lngCntr)
    r_alngMyArray(lngCntr) = "THIS DOES NOT WORK!"
  next lngCntr

  End Function

  ------------------
  WORKAROUND
  ------------------
  Function IsInitialized(byref r_alngInitialized) as
  Long
   On Error Goto error_not_initialized
   
    IsInitialized = UBOUND(r_alngInitialized)

  Exit Function
  Goto error_not_initialized
    IsInitialized = -1
  End Function




More information about the AccessD mailing list