I have the following code:

For lngPosition = LBound(strNames) To UBound(strNames)
      mainForm.NameDisplay.Caption = strNames(lngPosition)
      mainForm.StockDisplay.Caption = intLevels(lngPosition)
    Next lngPosition

This code, in my opinion, should print everything in array in a label. However, it only prints the last element of the array. Anyone got any ideas on how to fix this?

mainForm.StockDisplay.Caption = mainForm.StockDisplay.Caption & vbNewLine & intLevels(lngPosition)

Good Luck

Thanks. This solved that problem. New problem now:

For lngPosition = LBound(strNames) To UBound(strNames)
      Code(lngPosition) = Left(strNames(lngPosition), 3) & Right(strNames(lngPosition), 3)
      mainForm.NameDisplay.Caption = mainForm.NameDisplay.Caption & vbNewLine & strNames(lngPosition)
      mainForm.CodeDisplay.Caption = mainForm.CodeDisplay.Caption & vbNewLine & Code(lngPosition)
      mainForm.StockDisplay.Caption = mainForm.StockDisplay.Caption & vbNewLine & intLevels(lngPosition)
    Next lngPosition

I get error 9: "subscript out of range". It highlights the following line:

Code(lngPosition) = Left(strNames(lngPosition), 3) & Right(strNames(lngPosition), 3)

Have you dimed the code variable to the same number of elements as the strNames array? Look up redim and redim preserve if needed.

Good Luck

Entire Code:

' First we will store our variables
Global msg, strNameText As String   ' Instructional Message Box, Product Code and Name Text
Global blDimensioned As Boolean           ' Is the array dimensioned?
Global strStockText As Integer            ' To temporarily hold levels
Global lngPosition As Long                ' Counting
Global strNames() As String               ' Array of product names
Global Code() As String
Global intLevels() As Integer             ' Array of stock levels

' This function loads the required data into the system
Public Function CreateStock(ByVal sender As String, ByVal e As Integer)
  msg = MsgBox("Welcome to our Stock Control System. Enter the items first in the following input box, and press the cancel button when you are finished", vbOKOnly + vbInformation, "Stock Control System")
  ' The name has not yet been dimensioned
  blDimensioned = False
  
  Do
    ' Ask for a product name
    strNameText = InputBox("Please enter the poduct name:", "Stock Control System")
    strStockText = Val(InputBox("Please enter the inital stock level:", "Stock Control System"))
    
    If strNameText <> "" Then
      ' Has the array been dimensioned?
      If blDimensioned = True Then
        'Yes, so extend the array one element large than its current upper bound.
        'Without the "Preserve" keyword below, the previous elements in our array would be erased with the resizing
        ReDim Preserve strNames(0 To UBound(strNames) + 1) As String
        ReDim Preserve intLevels(0 To UBound(intLevels) + 1) As Integer
      Else
        'No, so dimension it and flag it as dimensioned.
        ReDim strNames(0 To 0) As String
        ReDim intLevels(0 To 0) As Integer
        blDimensioned = True
      End If
      
      'Add the product name to the last element in the array.
      strNames(UBound(strNames)) = strNameText
      intLevels(UBound(intLevels)) = strStockText
    End If
    Loop Until strNameText = ""
    
    For lngPosition = LBound(strNames) To UBound(strNames)
      Code(lngPosition) = Left(strNames(lngPosition), 3) & Right(strNames(lngPosition), 3)
      mainForm.NameDisplay.Caption = mainForm.NameDisplay.Caption & vbNewLine & strNames(lngPosition)
      mainForm.CodeDisplay.Caption = mainForm.CodeDisplay.Caption & vbNewLine & Code(lngPosition)
      mainForm.StockDisplay.Caption = mainForm.StockDisplay.Caption & vbNewLine & intLevels(lngPosition)
    Next lngPosition
End Function

' The following function ends the program
Public Function EndProgram(ByVal e As Integer)
  ' First of all we declare all our variables
  Dim msg As String
  
  ' Next we display a message box to the user
  msg = MsgBox("Are you sure you wish to exit?", vbYesNo + vbQuestion, "Exit Program?")
  e = 1
  
  ' The next statement will help to decide what to do with the user input
  If msg = vbYes Then
    End
  Else
    mainForm.Show
  End If
End Function

' The following function helps to search the array
Public Function Find(ByVal prodcode As String, ByVal msg As String)
  'prodcode = InputBox("Please enter the product to search for:", "System Finder")
  'msg = MsgBox("You searched for '" & prodcode & "'")
  
  MsgBox Code
  
End Function

Public Function IsInArray(FindValue As Variant, arrSearch As Variant) As Boolean
  On Error GoTo LocalError
  If Not IsArray(arrSearch) Then Exit Function
  If Not IsNumeric(FindValue) Then FindValue = UCase(FindValue)
  IsInArray = InStr(1, vbNullChar & Join(arrSearch, vbNullChar) & vbNullChar, vbNullChar & FindValue & vbNullChar) > 0
Exit Function

LocalError:
End Function

Okay, for future reference you need to be in the .NET forum...


Now, I see where you redim preserve strNames and intLevels but no where do I see where you...

ReDim Code(LBound(strNames) To UBound(strNames)) As String

which should go between your do until loop and your for loop.

Good Luck

Okay, for future reference you need to be in the .NET forum...


Now, I see where you redim preserve strNames and intLevels but no where do I see where you...

ReDim Code(LBound(strNames) To UBound(strNames)) As String

which should go between your do until loop and your for loop.

Good Luck

Sorry about not replying I figured it out before you replied. Is there anyway I can search the Code() array from within another function? I tried to use Code(lngPosition) but it just ends up back where I was before. Remember this is from another function.

Okay, the reason you are getting the index out of bounds is because of something like this...

Dim MyVar(1 to 2) As String
MyVar(0) = "Whatever" 'throws error because there is no zero (0) element
MyVar(3) = "Another error because there is not element number 3"

As for accessing an array from another proceedure, yes it is possible if you do one of two things. Either declare the array in the general declarations of the form or public in a module or pass the array in of which I see you have it declared in the general declarations section of the form. So you should be able to access the code array from within another proceedure within that same form. If however, you are accessing the code array from a module or from another form, then you will need to prefix the array variable with the form name where it is declared in...

Which means, if in form2 you want to access form1's global code array variable then you would need to do form1.code, but also don't forget to check the bounds of the array (LBound, UBound).

Good Luck

As you can see from the above code, this program takes in a certain number of values from the user and stores them in three different arrays.

I now need to search the StockCode() array. I tried to use the following code:

Public Function Find(ByVal prodcode As String, ByRef StockCode() As String)
  mainForm.NameDisplay.Caption = ""
  mainForm.CodeDisplay.Caption = ""
  mainForm.StockDisplay.Caption = ""
  
  prodcode = InputBox("Please enter the product to search for:", "System Finder")
  MsgBox IsInArray(prodcode, StockCode())
  
  For lngPosition = LBound(strNames) To UBound(strNames)
    mainForm.NameDisplay.Caption = mainForm.NameDisplay.Caption & vbNewLine & strNames(lngPosition)
    mainForm.CodeDisplay.Caption = mainForm.CodeDisplay.Caption & vbNewLine & StockCode(lngPosition)
    mainForm.StockDisplay.Caption = mainForm.StockDisplay.Caption & vbNewLine & intLevels(lngPosition)
  Next lngPosition
End Function

But this does not seem to work.

I will add the code for IsInArray():

Public Function IsInArray(FindValue As Variant, arrSearch As Variant) As Boolean
  On Error GoTo LocalError
  If Not IsArray(arrSearch) Then Exit Function
  IsInArray = InStr(1, vbNullChar & Join(arrSearch, vbNullChar) & vbNullChar, vbNullChar & FindValue & vbNullChar) > 0
Exit Function

LocalError:
End Function

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.