Custom Data types

VBA language, among other things, allows you to create custom data types. These are structures that allow users to arrange the data in the desired way so that can easily access them, for example, to define records that store person data as a combination of: ID number, name, surname, address, location, phone number and e- mail address. The arranged data thus facilitates batch processing, and are often used in combination with arrays.

Syntax of commands for creating custom types:

{ Public | Private} Type <type name>
<field 1> As <data type>

<field n> As <data type>
End Type

The data type has to be defined at the beginning of the module (out of subroutines) and, later in the program, it is used to declare the variable to which we assign the specified type. We access such data by giving the name of the variable, followed by point, and then the name of the field to which we want to access. Practical application of such features can be viewed in the program:

Private Type PR
  ID As Integer
  Name As String
  Surname As String
  Address As String
  City As String
  Phone As String
  EMail As String
End Type

Sub DT()

Dim person(9) As PR
person(0).ID = 1
person(0).Name = “Peter”
person(0).Surname = “Johnson”
person(0).Address = “1 Knez Mihailova St.”
person(0).City = “Belgrade”
person(0).Phone = “+381(11)111222”
person(0).EMail = “PeJo@example.com”
MsgBox person(0).Name

End Sub