| | |
Printing Array Contents to Label
![]() |
I have the following code:
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?
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
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?
Thanks,
Michael
Michael
•
•
Join Date: Mar 2009
Posts: 814
Reputation:
Solved Threads: 148
0
#2 23 Days Ago
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
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
Thanks
0
#3 23 Days Ago
Thanks. This solved that problem. New problem now:
I get error 9: "subscript out of range". It highlights the following line:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
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:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Code(lngPosition) = Left(strNames(lngPosition), 3) & Right(strNames(lngPosition), 3)
Thanks,
Michael
Michael
0
#5 22 Days Ago
Entire Code:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
' 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
Thanks,
Michael
Michael
•
•
Join Date: Mar 2009
Posts: 814
Reputation:
Solved Threads: 148
0
#6 22 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...
which should go between your do until loop and your for loop.
Good Luck
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)
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
Thanks
0
#7 22 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)
ReDim Code(LBound(strNames) To UBound(strNames)) As String
which should go between your do until loop and your for loop.
Good Luck
Thanks,
Michael
Michael
•
•
Join Date: Mar 2009
Posts: 814
Reputation:
Solved Threads: 148
0
#8 21 Days Ago
Okay, the reason you are getting the index out of bounds is because of something like this...
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
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
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
If anyone has helped you solve your problem, please mark your thread as solved.
Thanks
Thanks
0
#9 20 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:
But this does not seem to work.
I now need to search the StockCode() array. I tried to use the following code:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
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.
Thanks,
Michael
Michael
0
#10 20 Days Ago
I will add the code for IsInArray():
LocalError:
End Function
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
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
Last edited by MichaelSammels; 20 Days Ago at 8:51 am.
Thanks,
Michael
Michael
![]() |
Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: making objects invisible
- Next Thread: can any one tell me how to open image of .CAL extension or .TIFF extension in vb6
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows





