Perform Some Common Database Chores

Perform Some Common Database Chores

These several database functions work together and perform various utility functions, such as checking if fields and tables exist, creating fields and tables, and so on. The interface hides all the code and returns True or False to report the status of the functions:

 Function CreateDatabase(DatabasePath As String, dbLanguage _	As String, JetVersion As Integer) As Boolean	Dim TempWs As Workspace	Dim TempDB As Database	On Error GoTo Errors:		Set TempWs = DBEngine.Workspaces(0)		Set TempDB = TempWs.CreateDatabase(DatabasePath, _			dbLanguage, JetVersion)		CreateDatabase = True	Exit FunctionErrors:	CreateDatabase = FalseEnd FunctionFunction CreateTable(DatabasePath As String, NewTableName _	As String) As Boolean	Dim dbsTarget As Database	Dim tdfNew As TableDef	On Error GoTo Errors:		If TableExists(DatabasePath, NewTableName) = False _			Then			'This table does not exist on the target			'database, so it is ok to add it.			Set dbsTarget = OpenDatabase(DatabasePath)			Set tdfNew = _				dbsTarget.CreateTableDef(NewTableName)			With tdfNew				.Fields.Append .CreateField("Temp", dbInteger)			End With			'The new table has been created, append it to the			'database			dbsTarget.TableDefs.Append tdfNew			dbsTarget.TableDefs(NewTableName).Fields. _				Delete ("Temp")			dbsTarget.Close			CreateTable = True		Else			'This table does exist on the target			'database, so do not add it.		End If	Exit FunctionErrors:   CreateTable = FalseEnd FunctionFunction CreateField(DatabasePath As String, _	TargetTableName As String, NewFieldName As String, _	FieldDataType As Integer) As Boolean	Dim dbsTarget As Database	Dim tdfTarget As TableDef	On Error GoTo Errors:		CreateField = False		Set dbsTarget = OpenDatabase(DatabasePath)		If TableExists(DatabasePath, TargetTableName) Then			'The table exists, assign the table to the 			'tabledef and proceed.			Set tdfTarget = _				dbsTarget.TableDefs(TargetTableName)			If Not FieldExists(DatabasePath, _				TargetTableName, NewFieldName) Then				'The Field doesn't exist, so create it.				With tdfTarget					.Fields.Append _						.CreateField(NewFieldName, _						FieldDataType)				End With				CreateField = True			Else				'Field exists, we cannot create it.			End If		Else			'The table does not exist, so we cannot add a new 			'field to it.		End If	Exit FunctionErrors:	CreateField = FalseEnd FunctionFunction TableExists(DatabasePath As String, TableName As _	String) As Boolean	Dim dbsSource As Database	Dim tdfCheck As TableDef	On Error GoTo Errors:		TableExists = False		Set dbsSource = OpenDatabase(DatabasePath)		With dbsSource			' Enumerate TableDefs collection.			For Each tdfCheck In .TableDefs				If tdfCheck.Name = TableName Then					TableExists = True					Exit For				Else			End If			Next tdfCheck		End With	Exit FunctionErrors:   TableExists = FalseEnd FunctionFunction FieldExists(DatabasePath As String, TableName As _	String, FieldName As String) As Boolean	Dim dbsSource As Database	Dim tdfSource As TableDef	Dim fldCheck As Field	On Error GoTo Errors:		FieldExists = False		If TableExists(DatabasePath, TableName) Then			Set dbsSource = OpenDatabase(DatabasePath)			Set tdfSource = dbsSource.TableDefs(TableName)			With tdfSource				' Enumerate TableDefs collection.				For Each fldCheck In .Fields					If fldCheck.Name = FieldName Then						FieldExists = True						Exit For					End If				Next fldCheck			End With		Else			'The Table doesn't exist, so neither			'can the field.			FieldExists = False		End If	Exit FunctionErrors:	FieldExists = FalseEnd Function

If you do frequent lookups, it’s more productive to open and close your database externally to the functions. Because this code opens and closes the database each time, it’s not meant for intensive or constant calling.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as