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:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job