Class: Queue in Category General VBA/VB6 : Abstract Data Types from Total Visual SourceBook

Class to implement a queue with a fixed or unlimited number of elements for VB6 and VBA.

A queue is a restricted form of a list. All insertions occur at the end of the list, and all deletions at the front. An example of a queue is a waiting line where the last person is added to the rear of line and the first person served at the front.

The queue data type defines three operations:

  1. EnQueue - Places an item at the rear of the list.
  2. DeQueue - Removes an item from the front of the list.
  3. Front - Retrieves, but does not remove, an item from the front of the list.

The queue is a FIFO, or First-In, First-Out, data type. This means that data enters at one end of the list, and exits from the other. This class is implemented using an array. When the array becomes full, the array index is reset to the beginning of the array and those elements are lost. The queue can also be unlimited by setting the WrapQueue property to False to preserve all items. This class supports object variables in addition to the standard text and numeric types.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CQueue class.
WrapQueue Property Get the WrapQueue setting.
Size Property Get the number of elements the queue can hold. The queue will hold as many items as are available in the array. If you need to store more items than are available in the array, increase the size property.
Class_Initialize Initialize Set initial values to defaults which may be overridden with Public Property settings.
DeQueue Method Removes the front-most item from the queue and places it in the varData variable.
EnQueue Method Place the data specified in varData on the back of the queue. If the queue is full, data at the front of the queue is overwritten.
Front Method Get the data in the front of the queue. This function differs from DeQueue in that it does not remove the item from the queue.
' Example of CQueue
'
' There are two procedures illustrating CQueue.
' Both procedures add the same items to the queue based on the WrapQueue property:
' - Example_CQueueWrapped   holds a limited number
' - Example_CQueueAll       holds all the items
'
' To use this example, create a new module and paste this code into it.
' Then run the procedure by putting the cursor in the procedure and pressing:
'    F5 to run it, or
'    F8 to step through it line-by-line (see the Debug menu for more options)

' Initial queue size
Private Const mcintMax As Integer = 10

' Number of items to add in each of the example batches
Private Const mcintItems As Integer = 21

Private Sub Example_CQueueWrapped()
  ' Comments: Example of using the CQueue class to store a set of items and retrieve them in VBA and VB6.
  '           In this example, a maximum of 10 items are kept which limits the amount of memory this queue ever uses.
  '           If more than 10 items are added, only the last 10 are retained.
  '           See the information in the Immediate Window.

  Dim clsQueue As CQueue
  Dim strData As String
  Dim intCounter As Integer

  Set clsQueue = New CQueue

  ' Set the queue size (no need to set the WrapQueue property since it defaults to True and limits the number of items to this size)
  clsQueue.Size = mcintMax

  ' Enqueue (add) items
  For intCounter = 1 To mcintItems
    strData = "Item " & intCounter
    clsQueue.EnQueue strData
  Next intCounter

  ' Dequeue (retrieve) all the items (only the last 10 are shown since the size is set to this)
  Do While clsQueue.DeQueue(strData)
    Debug.Print strData
  Loop

  ' Add another set to the queue
  For intCounter = 1 To mcintItems
    strData = "Item " & intCounter + mcintItems
    clsQueue.EnQueue strData
  Next intCounter

  ' Show the next 5 at the front of the queue (this leaves 5 in the queue)
  Debug.Print
  Debug.Print "Show Next 5"
  For intCounter = 1 To 5
    If clsQueue.DeQueue(strData) Then
      Debug.Print strData
    End If
  Next intCounter

  ' Add a another set
  For intCounter = 1 To mcintItems
    strData = "Item " & intCounter + (mcintItems * 2)
    clsQueue.EnQueue strData
  Next intCounter

  ' Dequeue all the items (only the last 10 are shown since the maximum size is set to this)
  Debug.Print
  Debug.Print "Show Remaining"
  Do While clsQueue.DeQueue(strData)
    Debug.Print strData
  Loop

  Set clsQueue = Nothing

End Sub

Private Sub Example_CQueueAll()
  ' Comments: Example of using the CQueue class to store a set of items and retrieve them in VBA and VB6.
  '           In this example, by turning off WrapQueue, all items are kept which does not limit the amount of memory this queue uses.
  '           See the information in the Immediate Window.

  Dim clsQueue As CQueue
  Dim strData As String
  Dim intCounter As Integer

  Set clsQueue = New CQueue

  ' Do not wrap the queue so that it expands as additional items are added
  clsQueue.WrapQueue = False

  ' Set the initial queue size, which expands if this number is exceeded
  clsQueue.Size = mcintMax

  ' Enqueue (add) items
  For intCounter = 1 To mcintItems
    strData = "Item " & intCounter
    clsQueue.EnQueue strData
  Next intCounter

  ' Dequeue (retrieve) all the items
  Do While clsQueue.DeQueue(strData)
    Debug.Print strData
  Loop

  ' Add another set to the queue
  For intCounter = 1 To mcintItems
    strData = "Item " & intCounter + mcintItems
    clsQueue.EnQueue strData
  Next intCounter

  ' Show the next 5
  Debug.Print
  Debug.Print "Show Next 5"
  For intCounter = 1 To 5
    If clsQueue.DeQueue(strData) Then
      Debug.Print strData
    End If
  Next intCounter

  ' Add a another set
  For intCounter = 1 To mcintItems
    strData = "Item " & intCounter + (mcintItems * 2)
    clsQueue.EnQueue strData
  Next intCounter

  ' Dequeue all the items, so you can see that none of the items were discarded
  Debug.Print
  Debug.Print "Show Remaining"
  Do While clsQueue.DeQueue(strData)
    Debug.Print strData
  Loop

  Set clsQueue = Nothing

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS