Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Option Compare Database
Option Explicit
Private siTop As StackItem
Property Get StackTop() As Variant
If StackEmpty Then
StackTop = Null
Else
StackTop = siTop.Value
End If
End Property
Property Get StackEmpty() As Boolean
' Is the stack empty? It can
' only be empty if siTop is Nothing.
StackEmpty = (siTop Is Nothing)
End Property
Public Function Pop() As Variant
If Not StackEmpty Then
' Get the value from the current top stack element.
' Then, get a reference to the new stack top.
Pop = siTop.Value
Set siTop = siTop.NextItem
End If
End Function
Public Sub Push(ByVal varText As Variant)
' Add a new item to the top of the stack.
Dim siNewTop As New StackItem
siNewTop.Value = varText
Set siNewTop.NextItem = siTop
Set siTop = siNewTop
End Sub
Option Compare Database
' Keep track of the next stack item,
' and the value of this item.
Public Value As Variant
Public NextItem As StackItem
Dim stkTest As New Stack
Sub TestStacks()
' Push some items, and then pop them.
stkTest.Push "Hello"
stkTest.Push "There"
stkTest.Push "How"
stkTest.Push "Are"
stkTest.Push "You"
Do While Not stkTest.StackEmpty
Debug.Print stkTest.Pop()
Loop
' Now, call a bunch of procedures.
' For each procedure, push the proc name
' at the beginning, and pop it on the way out.
Debug.Print
Debug.Print "Testing Procs:"
stkTest.Push "Main"
Debug.Print stkTest.StackTop
Call A
Debug.Print stkTest.Pop
End Sub
Sub A()
stkTest.Push "A"
Debug.Print stkTest.StackTop
Call B
Debug.Print stkTest.Pop
End Sub
Sub B()
stkTest.Push "B"
Debug.Print stkTest.StackTop
Call C
Debug.Print stkTest.Pop
End Sub
Sub C()
stkTest.Push "C"
Debug.Print stkTest.StackTop
' You'd probably do something in here...
Debug.Print stkTest.Pop
End Sub
Option Compare Database
Option Explicit
Private mStack As New Collection
Private mStackType As Long
Public Enum theStackType
FILO = 1
FIFO = 2
End Enum
Public Function Push(theObject As Object, objName As String) As Object
On Error GoTo errlabel
If mStack.count = 0 Then
mStack.Add Item:=theObject, Key:=objName
ElseIf mStackType = FIFO Then
mStack.Add Item:=theObject, Key:=objName, After:=mStack.count
Else
mStack.Add Item:=theObject, Key:=objName, Before:=1
End If
Set Push = theObject
Exit Function
errlabel:
MsgBox Err.Number & " " & Err.Description
End Function
Public Property Get count() As Integer
count = mStack.count
End Property
Public Property Get Item(ByVal index As Variant) As Object
Set Item = mStack(index)
End Property
Public Sub Remove(index As Variant)
mStack.Remove (index)
End Sub
Private Sub Class_Initialize()
mStackType = FILO
End Sub
Private Sub Class_Terminate()
Set mStack = Nothing
End Sub
Public Sub Clear()
Set mStack = New Collection
End Sub
Public Function Pop() As Object
If Me.count > 0 Then
Set Pop = mStack.Item(1)
Me.Remove (1)
Else
MsgBox "Collection Empty"
End If
End Function
Public Property Get StackType() As theStackType
StackType = mStackType
End Property
Public Property Let StackType(ByVal theStackType As Long)
mStackType = theStackType
End Property
Public Property Get StackTypeName() As String
If mStackType = FIFO Then
StackTypeName = "FIFO"
Else
StackTypeName = "FILO"
End If
End Property
Public Property Get LastObjectName() As String
LastObjectName = mStack.Item(mStack.count).Name
End Property
Public Property Get FirstObjectName() As String
FirstObjectName = mStack.Item(1).Name
End Property
Public Sub testStack()
Dim ctl As Access.Control
Dim counter As Integer
DoCmd.OpenForm "form1"
Set myStack = New Stack
'myStack.StackType = FILO
myStack.StackType = FIFO
Debug.Print myStack.StackTypeName
For Each ctl In Forms("form1").Controls
Debug.Print "Pushing " & ctl.Name
myStack.Push ctl, ctl.Name
Next ctl
Debug.Print "Final StacK" & vbCrLf
For counter = 1 To myStack.count
Debug.Print "Stack Order " & myStack.Item(counter).Name
Next counter
Debug.Print "Popping" & vbCrLf
For counter = 1 To myStack.count
Debug.Print "Pop " & myStack.Pop.Name & " Remaining " & myStack.count
Next counter
End Sub
FILO
Pushing Check0
Pushing Label1
Pushing Option2
Pushing Label3
Pushing Text4
Pushing Label5
Final StacK
Stack Order Label5
Stack Order Text4
Stack Order Label3
Stack Order Option2
Stack Order Label1
Stack Order Check0
Popping
Pop Label5 Remaining 5
Pop Text4 Remaining 4
Pop Label3 Remaining 3
Pop Option2 Remaining 2
Pop Label1 Remaining 1
Pop Check0 Remaining 0
************************************
FIFO
Pushing Check0
Pushing Label1
Pushing Option2
Pushing Label3
Pushing Text4
Pushing Label5
Final StacK
Stack Order Check0
Stack Order Label1
Stack Order Option2
Stack Order Label3
Stack Order Text4
Stack Order Label5
Popping
Pop Check0 Remaining 5
Pop Label1 Remaining 4
Pop Option2 Remaining 3
Pop Label3 Remaining 2
Pop Text4 Remaining 1
Pop Label5 Remaining 0
Option Compare Database
Property Get Peek() As Variant
End Property
Property Get IsEmpty() As Boolean
End Property
Public Function Pop() As Variant
End Function
Public Sub Push(dsi As Variant)
End Sub
Option Compare Database
Option Explicit
Implements DataStructure
Private dsiTop As DataStructureItem
Property Get DataStructure_IsEmpty() As Boolean
' Is the stack empty? It can
' only be empty if siTop is Nothing.
DataStructure_IsEmpty = (dsiTop Is Nothing)
End Property
Public Function DataStructure_Pop() As Variant
If Not DataStructure_IsEmpty Then
' Get the value from the current top stack element.
' Then, get a reference to the new stack top.
DataStructure_Pop = dsiTop.Value
Set dsiTop = dsiTop.RelatedItem
End If
End Function
Public Sub DataStructure_Push(dsi As Variant)
' Add a new item to the top of the stack.
Dim dsiNewTop As New DataStructureItem
dsiNewTop.Value = dsi
Set dsiNewTop.RelatedItem = dsiTop
Set dsiTop = dsiNewTop
End Sub
Private Property Get DataStructure_Peek() As Variant
If DataStructure_IsEmpty Then
DataStructure_Peek = Null
Else
DataStructure_Peek = dsiTop.Value
End If
End Property
Option Explicit
Implements DataStructure
Private dsiFirst As DataStructureItem
Private dsiLast As New DataStructureItem
Property Get DataStructure_Peek() As Variant
If DataStructure_IsEmpty Then
DataStructure_Peek = Null
Else
DataStructure_Peek = dsiFirst.Value
End If
End Property
Property Get DataStructure_IsEmpty() As Boolean
' Is the stack empty? It can
' only be empty if siTop is Nothing.
DataStructure_IsEmpty = (dsiFirst Is Nothing)
End Property
Public Function DataStructure_Pop() As Variant
If Not DataStructure_IsEmpty Then
' Get the value from the current top stack element.
' Then, get a reference to the new stack top.
DataStructure_Pop = dsiFirst.Value
Set dsiFirst = dsiFirst.RelatedItem
End If
End Function
Public Sub DataStructure_Push(dsi As Variant)
' Add a new item to the top of the stack.
Dim dsiNewItem As New DataStructureItem
Dim emptyQueue As Boolean
emptyQueue = DataStructure_IsEmpty
dsiNewItem.Value = dsi
Set dsiLast.RelatedItem = dsiNewItem
Set dsiLast = dsiNewItem
If emptyQueue = True Then
Set dsiFirst = dsiNewItem
End If
End Sub
Option Compare Database
Option Explicit
' Keep track of the next stack item,
' and the value of this item.
Public Value As Variant
Public RelatedItem As DataStructureItem
Option Compare Database
Option Explicit
Dim testObject As DataStructure
Sub TestDataStructures(ByRef struct As DataStructure)
' Push some items, and then pop them.
Set testObject = struct
testObject.Push "Hello"
testObject.Push "There"
testObject.Push "How"
testObject.Push "Are"
testObject.Push "You"
Do While Not testObject.IsEmpty
Debug.Print testObject.Pop()
Loop
End Sub
Sub TestQueue()
Dim q As New DataStructure
Set q = New Queue
Debug.Print "Testing queue"
TestDataStructures q
Debug.Print
End Sub
Sub TestStack()
Dim s As New Stack
Set s = New Stack
Debug.Print "Testing stack"
TestDataStructures s
Debug.Print
End Sub
Sub TestMaster()
TestQueue
TestStack
End Sub
Option Compare Database
Option Explicit
Private mQueue As New Collection
Private mQueueType As Long
Public Enum theQueueType
FILO_STACK = 1
FIFO = 2
End Enum
Public Function Push(theObject As Object, objName As String) As Object
On Error GoTo errlabel
If mQueue.count = 0 Then
mQueue.Add Item:=theObject, Key:=objName
ElseIf mQueueType = FIFO Then
mQueue.Add Item:=theObject, Key:=objName, After:=mQueue.count
Else
mQueue.Add Item:=theObject, Key:=objName, Before:=1
End If
Set Push = theObject
Exit Function
errlabel:
MsgBox Err.Number & " " & Err.Description
End Function
Public Property Get count() As Integer
count = mQueue.count
End Property
Public Property Get Item(ByVal index As Variant) As Object
Set Item = mQueue(index)
End Property
Public Sub Remove(index As Variant)
mQueue.Remove (index)
End Sub
Private Sub Class_Initialize()
mQueueType = FILO_STACK
End Sub
Private Sub Class_Terminate()
Set mQueue = Nothing
End Sub
Public Sub Clear()
Set mQueue = New Collection
End Sub
Public Function Pop() As Object
If Me.count > 0 Then
Set Pop = mQueue.Item(1)
Me.Remove (1)
Else
MsgBox "Collection Empty"
End If
End Function
Public Property Get QueueType() As theQueueType
QueueType = mQueueType
End Property
Public Property Let QueueType(ByVal theQueueType As Long)
mQueueType = theQueueType
End Property
Public Property Get QueueTypeName() As String
If mQueueType = FIFO Then
QueueTypeName = "FIFO"
Else
QueueTypeName = "FILO (STACK)"
End If
End Property
Public Property Get LastObjectName() As String
LastObjectName = mQueue.Item(mQueue.count).Name
End Property
Public Property Get FirstObjectName() As String
FirstObjectName = mQueue.Item(1).Name
End Property
Option Compare Database
Option Explicit
Private mQueue As New Collection
Public Function Push(theObject As Object, objName As String) As Object
On Error GoTo errlabel
mQueue.Add Item:=theObject, Key:=objName
Set Push = theObject
Exit Function
errlabel:
MsgBox Err.Number & " " & Err.Description
End Function
Public Property Get count() As Integer
count = mQueue.count
End Property
Public Property Get Item(ByVal index As Variant) As Object
Set Item = mQueue(index)
End Property
Public Sub Remove(index As Variant)
mQueue.Remove (index)
End Sub
Private Sub Class_Terminate()
Set mQueue = Nothing
End Sub
Public Sub Clear()
Set mQueue = New Collection
End Sub
Public Function Pop() As Object
If Me.count > 0 Then
Set Pop = mQueue.Item(1)
Me.Remove (1)
Else
MsgBox "Collection Empty"
End If
End Function
Public Property Get LastObjectName() As String
If Me.count > 0 Then
LastObjectName = mQueue.Item(mQueue.count).Name
Else
MsgBox "Queue Empty"
End If
End Property
Public Property Get FirstObjectName() As String
If Me.count > 0 Then
FirstObjectName = mQueue.Item(1).Name
Else
MsgBox "Queue Empty"
End If
End Property
Public Property Get LastObject() As Object
If Me.count > 0 Then
Set LastObject = mQueue.Item(mQueue.count)
Else
MsgBox "Queue Empty"
End If
End Property
Public Property Get FirstObject() As Object
If Me.count > 0 Then
Set FirstObject = mQueue.Item(1)
Else
MsgBox "Queue Empty"
End If
End Property
Option Compare Database
Option Explicit
Private mStack As New Collection
Public Function Push(theObject As Object, objName As String) As Object
On Error GoTo errlabel
If mStack.count = 0 Then
mStack.Add Item:=theObject, Key:=objName
Else
mStack.Add Item:=theObject, Key:=objName, before:=1
End If
Set Push = theObject
Exit Function
errlabel:
MsgBox Err.Number & " " & Err.Description
End Function
Public Property Get count() As Integer
count = mStack.count
End Property
Public Property Get Item(ByVal index As Variant) As Object
Set Item = mStack(index)
End Property
Public Sub Remove(index As Variant)
mStack.Remove (index)
End Sub
Private Sub Class_Terminate()
Set mStack = Nothing
End Sub
Public Sub Clear()
Set mStack = New Collection
End Sub
Public Function Pop() As Object
If Me.count > 0 Then
Set Pop = mStack.Item(1)
Me.Remove (1)
Else
MsgBox "Collection Empty"
End If
End Function
Public Property Get LastObjectName() As String
If Me.count > 0 Then
LastObjectName = mStack.Item(mStack.count).Name
Else
MsgBox "Stack Empty"
End If
End Property
Public Property Get FirstObjectName() As String
If Me.count > 0 Then
FirstObjectName = mStack.Item(1).Name
Else
MsgBox "Stack Empty"
End If
End Property
Public Property Get LastObject() As Object
If Me.count > 0 Then
Set LastObject = mStack.Item(mStack.count)
Else
MsgBox "Stack Empty"
End If
End Property
Public Property Get FirstObject() As Object
If Me.count > 0 Then
Set FirstObject = mStack.Item(1)
Else
MsgBox "Stack Empty"
End If
End Property
Option Compare Database
Option Explicit
Implements DataStructure
Private coll As New Collection
Private Sub Class_Terminate()
Set coll = Nothing
End Sub
Public Function DataStructure_Push(theObject As Object)
coll.Add Item:=theObject
End Function
Public Function DataStructure_Count() As Integer
DataStructure_Count = coll.Count
End Function
Public Function DataStructure_Pop() As Object
If coll.Count > 0 Then
Set DataStructure_Pop = coll.Item(1)
coll.Remove (1)
Else
DataStructure_Pop = Nothing
End If
End Function
Public Function DataStructure_Peek() As Object
If coll.Count > 0 Then
Set DataStructure_Peek = coll.Item(1)
Else
DataStructure_Peek = Nothing
End If
End Function
Property Get DataStructure_IsEmpty() As Boolean
DataStructure_IsEmpty = False
If coll.Count = 0 Then
DataStructure_IsEmpty = True
End If
End Property
Option Compare Database
Option Explicit
Implements DataStructure
Private coll As New Collection
Private Sub Class_Terminate()
Set coll = Nothing
End Sub
Public Function DataStructure_Push(theObject As Object)
coll.Add Item:=theObject
End Function
Public Function DataStructure_Count() As Integer
DataStructure_Count = coll.Count
End Function
Public Function DataStructure_Pop() As Object
If coll.Count > 0 Then
Set DataStructure_Pop = coll.Item(coll.Count)
coll.Remove (coll.Count)
Else
DataStructure_Pop = Nothing
End If
End Function
Public Function DataStructure_Peek() As Object
If coll.Count > 0 Then
Set DataStructure_Peek = coll.Item(coll.Count)
Else
DataStructure_Peek = Nothing
End If
End Function
Property Get DataStructure_IsEmpty() As Boolean
DataStructure_IsEmpty = False
If coll.Count = 0 Then
DataStructure_IsEmpty = True
End If
End Property
Option Compare Database
Option Explicit
Property Get Peek() As Variant
End Property
Property Get IsEmpty() As Boolean
End Property
Public Function Pop() As Variant
End Function
Public Sub Push(dsi As Variant)
End Sub
Public Function Count() As Integer
End Function
David W. Fenton said:We could be confused in exactly the same way, but confusion might be like Nulls, and not comparable.