' 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