Printing Array Contents to Label

Reply

Join Date: Nov 2008
Posts: 11
Reputation: MichaelSammels is an unknown quantity at this point 
Solved Threads: 0
MichaelSammels's Avatar
MichaelSammels MichaelSammels is offline Offline
Newbie Poster

Printing Array Contents to Label

 
0
  #1
20 Days Ago
I have the following code:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. For lngPosition = LBound(strNames) To UBound(strNames)
  2. mainForm.NameDisplay.Caption = strNames(lngPosition)
  3. mainForm.StockDisplay.Caption = intLevels(lngPosition)
  4. 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?
Thanks,

Michael
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 805
Reputation: vb5prgrmr will become famous soon enough vb5prgrmr will become famous soon enough 
Solved Threads: 146
vb5prgrmr vb5prgrmr is offline Offline
Practically a Posting Shark
 
0
  #2
19 Days Ago
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. mainForm.StockDisplay.Caption = mainForm.StockDisplay.Caption & vbNewLine & intLevels(lngPosition)



Good Luck
If anyone has helped you solve your problem, please mark your thread as solved.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: MichaelSammels is an unknown quantity at this point 
Solved Threads: 0
MichaelSammels's Avatar
MichaelSammels MichaelSammels is offline Offline
Newbie Poster
 
0
  #3
18 Days Ago
Thanks. This solved that problem. New problem now:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. For lngPosition = LBound(strNames) To UBound(strNames)
  2. Code(lngPosition) = Left(strNames(lngPosition), 3) & Right(strNames(lngPosition), 3)
  3. mainForm.NameDisplay.Caption = mainForm.NameDisplay.Caption & vbNewLine & strNames(lngPosition)
  4. mainForm.CodeDisplay.Caption = mainForm.CodeDisplay.Caption & vbNewLine & Code(lngPosition)
  5. mainForm.StockDisplay.Caption = mainForm.StockDisplay.Caption & vbNewLine & intLevels(lngPosition)
  6. Next lngPosition

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

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Code(lngPosition) = Left(strNames(lngPosition), 3) & Right(strNames(lngPosition), 3)
Thanks,

Michael
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 805
Reputation: vb5prgrmr will become famous soon enough vb5prgrmr will become famous soon enough 
Solved Threads: 146
vb5prgrmr vb5prgrmr is offline Offline
Practically a Posting Shark
 
0
  #4
18 Days Ago
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
If anyone has helped you solve your problem, please mark your thread as solved.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: MichaelSammels is an unknown quantity at this point 
Solved Threads: 0
MichaelSammels's Avatar
MichaelSammels MichaelSammels is offline Offline
Newbie Poster
 
0
  #5
18 Days Ago
Entire Code:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. ' First we will store our variables
  2. Global msg, strNameText As String ' Instructional Message Box, Product Code and Name Text
  3. Global blDimensioned As Boolean ' Is the array dimensioned?
  4. Global strStockText As Integer ' To temporarily hold levels
  5. Global lngPosition As Long ' Counting
  6. Global strNames() As String ' Array of product names
  7. Global Code() As String
  8. Global intLevels() As Integer ' Array of stock levels
  9.  
  10. ' This function loads the required data into the system
  11. Public Function CreateStock(ByVal sender As String, ByVal e As Integer)
  12. 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")
  13. ' The name has not yet been dimensioned
  14. blDimensioned = False
  15.  
  16. Do
  17. ' Ask for a product name
  18. strNameText = InputBox("Please enter the poduct name:", "Stock Control System")
  19. strStockText = Val(InputBox("Please enter the inital stock level:", "Stock Control System"))
  20.  
  21. If strNameText <> "" Then
  22. ' Has the array been dimensioned?
  23. If blDimensioned = True Then
  24. 'Yes, so extend the array one element large than its current upper bound.
  25. 'Without the "Preserve" keyword below, the previous elements in our array would be erased with the resizing
  26. ReDim Preserve strNames(0 To UBound(strNames) + 1) As String
  27. ReDim Preserve intLevels(0 To UBound(intLevels) + 1) As Integer
  28. Else
  29. 'No, so dimension it and flag it as dimensioned.
  30. ReDim strNames(0 To 0) As String
  31. ReDim intLevels(0 To 0) As Integer
  32. blDimensioned = True
  33. End If
  34.  
  35. 'Add the product name to the last element in the array.
  36. strNames(UBound(strNames)) = strNameText
  37. intLevels(UBound(intLevels)) = strStockText
  38. End If
  39. Loop Until strNameText = ""
  40.  
  41. For lngPosition = LBound(strNames) To UBound(strNames)
  42. Code(lngPosition) = Left(strNames(lngPosition), 3) & Right(strNames(lngPosition), 3)
  43. mainForm.NameDisplay.Caption = mainForm.NameDisplay.Caption & vbNewLine & strNames(lngPosition)
  44. mainForm.CodeDisplay.Caption = mainForm.CodeDisplay.Caption & vbNewLine & Code(lngPosition)
  45. mainForm.StockDisplay.Caption = mainForm.StockDisplay.Caption & vbNewLine & intLevels(lngPosition)
  46. Next lngPosition
  47. End Function
  48.  
  49. ' The following function ends the program
  50. Public Function EndProgram(ByVal e As Integer)
  51. ' First of all we declare all our variables
  52. Dim msg As String
  53.  
  54. ' Next we display a message box to the user
  55. msg = MsgBox("Are you sure you wish to exit?", vbYesNo + vbQuestion, "Exit Program?")
  56. e = 1
  57.  
  58. ' The next statement will help to decide what to do with the user input
  59. If msg = vbYes Then
  60. End
  61. Else
  62. mainForm.Show
  63. End If
  64. End Function
  65.  
  66. ' The following function helps to search the array
  67. Public Function Find(ByVal prodcode As String, ByVal msg As String)
  68. 'prodcode = InputBox("Please enter the product to search for:", "System Finder")
  69. 'msg = MsgBox("You searched for '" & prodcode & "'")
  70.  
  71. MsgBox Code
  72.  
  73. End Function
  74.  
  75. Public Function IsInArray(FindValue As Variant, arrSearch As Variant) As Boolean
  76. On Error GoTo LocalError
  77. If Not IsArray(arrSearch) Then Exit Function
  78. If Not IsNumeric(FindValue) Then FindValue = UCase(FindValue)
  79. IsInArray = InStr(1, vbNullChar & Join(arrSearch, vbNullChar) & vbNullChar, vbNullChar & FindValue & vbNullChar) > 0
  80. Exit Function
  81.  
  82. LocalError:
  83. End Function
Thanks,

Michael
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 805
Reputation: vb5prgrmr will become famous soon enough vb5prgrmr will become famous soon enough 
Solved Threads: 146
vb5prgrmr vb5prgrmr is offline Offline
Practically a Posting Shark
 
0
  #6
18 Days Ago
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...

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. ReDim Code(LBound(strNames) To UBound(strNames)) As String

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



Good Luck
If anyone has helped you solve your problem, please mark your thread as solved.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: MichaelSammels is an unknown quantity at this point 
Solved Threads: 0
MichaelSammels's Avatar
MichaelSammels MichaelSammels is offline Offline
Newbie Poster
 
0
  #7
18 Days Ago
Originally Posted by vb5prgrmr View Post
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...

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. 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.
Thanks,

Michael
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 805
Reputation: vb5prgrmr will become famous soon enough vb5prgrmr will become famous soon enough 
Solved Threads: 146
vb5prgrmr vb5prgrmr is offline Offline
Practically a Posting Shark
 
0
  #8
17 Days Ago
Okay, the reason you are getting the index out of bounds is because of something like this...
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim MyVar(1 to 2) As String
  2. MyVar(0) = "Whatever" 'throws error because there is no zero (0) element
  3. 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
If anyone has helped you solve your problem, please mark your thread as solved.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: MichaelSammels is an unknown quantity at this point 
Solved Threads: 0
MichaelSammels's Avatar
MichaelSammels MichaelSammels is offline Offline
Newbie Poster
 
0
  #9
16 Days Ago
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:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Public Function Find(ByVal prodcode As String, ByRef StockCode() As String)
  2. mainForm.NameDisplay.Caption = ""
  3. mainForm.CodeDisplay.Caption = ""
  4. mainForm.StockDisplay.Caption = ""
  5.  
  6. prodcode = InputBox("Please enter the product to search for:", "System Finder")
  7. MsgBox IsInArray(prodcode, StockCode())
  8.  
  9. For lngPosition = LBound(strNames) To UBound(strNames)
  10. mainForm.NameDisplay.Caption = mainForm.NameDisplay.Caption & vbNewLine & strNames(lngPosition)
  11. mainForm.CodeDisplay.Caption = mainForm.CodeDisplay.Caption & vbNewLine & StockCode(lngPosition)
  12. mainForm.StockDisplay.Caption = mainForm.StockDisplay.Caption & vbNewLine & intLevels(lngPosition)
  13. Next lngPosition
  14. End Function

But this does not seem to work.
Thanks,

Michael
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: MichaelSammels is an unknown quantity at this point 
Solved Threads: 0
MichaelSammels's Avatar
MichaelSammels MichaelSammels is offline Offline
Newbie Poster
 
0
  #10
16 Days Ago
I will add the code for IsInArray():

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Public Function IsInArray(FindValue As Variant, arrSearch As Variant) As Boolean
  2. On Error GoTo LocalError
  3. If Not IsArray(arrSearch) Then Exit Function
  4. IsInArray = InStr(1, vbNullChar & Join(arrSearch, vbNullChar) & vbNullChar, vbNullChar & FindValue & vbNullChar) > 0
  5. Exit Function

LocalError:
End Function
Last edited by MichaelSammels; 16 Days Ago at 8:51 am.
Thanks,

Michael
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC