Isn’t there a function that checks to see if a table exists? I’ve been searching to no avail so if anyone can help I’m in your debt.
Thx,
Kathi
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Does a table exist (Access 2K)
Here you have a function to check if a table exist.
Function TableExist(TableName As String) As Boolean Dim db As DAO.Database Dim tdf As DAO.TableDef Set db = CurrentDb For Each tdf In db.TableDefs If tdf.Name = TableName Then TableExist = True GoTo TableExistExit End If Next tdf TableExist = False TableExistExit: Set tdf = Nothing Set db = Nothing End Function
Copy the code in a module. Save the module with any name but not TableExist (This is the function name and can’t be also the module name)
You can call the the function with :
TableExist(“YourTableName”)
It will return true if exist in the database, false if not.
Don’t forget to set a refence to Microsoft DAO 3.6 Object Libray as this is DAO code.
HTH
Here you have a function to check if a table exist.
Function TableExist(TableName As String) As Boolean Dim db As DAO.Database Dim tdf As DAO.TableDef Set db = CurrentDb For Each tdf In db.TableDefs If tdf.Name = TableName Then TableExist = True GoTo TableExistExit End If Next tdf TableExist = False TableExistExit: Set tdf = Nothing Set db = Nothing End Function
Copy the code in a module. Save the module with any name but not TableExist (This is the function name and can’t be also the module name)
You can call the the function with :
TableExist(“YourTableName”)
It will return true if exist in the database, false if not.
Don’t forget to set a refence to Microsoft DAO 3.6 Object Libray as this is DAO code.
HTH
Here are two more from
Code sample from Accessory http://www22.brinkster.com/accessory%5B/url%5D
'2 ways of checking if a table exists '97 / 2000 / 2002 'There are two ways to check whether a table exists in your database. 'The first, and easiest, is to make use of the MSysObjects system table. 'We'll make use of three fields, ID (the primary key - for speed, we'll count this 'rather than anything else), Name (the name of the object, in this case our table) 'and Type (tables have a type of 1). This function will return True if the specified 'table exists, False otherwise. ' Print TableExistsDC("Customers") ' returns True Function TableExistsDC(strTable As String) As Boolean ' Code sample from Accessory http://www22.brinkster.com/accessory TableExistsDC = (DCount("ID", "MSysObjects", "Name='" & strTable & "' AND Type=1") > 0) End Function 'This is fine, but the DCount() function can be a bit slow. A slightly more long-winded 'solution is to enumerate through all tables in the database, stopping if one is found 'that matches the specified table. This is also slow the first time you run it, but is 'faster thereafter. Again, this function will return True if the specified table exists, 'False otherwise. ' Print TableExists("Customers") ' returns True Function TableExists(strTable As String) As Boolean ' Code sample from Accessory http://www22.brinkster.com/accessory Dim db As Database Dim i As Integer Set db = DBEngine.Workspaces(0).Databases(0) TableExists = False db.TableDefs.Refresh For i = 0 To db.TableDefs.Count - 1 If strTable = db.TableDefs(i).Name Then 'Table exists TableExists = True Exit For End If Next i Set db = Nothing End Function
Here are two more from
Code sample from Accessory http://www22.brinkster.com/accessory%5B/url%5D
'2 ways of checking if a table exists '97 / 2000 / 2002 'There are two ways to check whether a table exists in your database. 'The first, and easiest, is to make use of the MSysObjects system table. 'We'll make use of three fields, ID (the primary key - for speed, we'll count this 'rather than anything else), Name (the name of the object, in this case our table) 'and Type (tables have a type of 1). This function will return True if the specified 'table exists, False otherwise. ' Print TableExistsDC("Customers") ' returns True Function TableExistsDC(strTable As String) As Boolean ' Code sample from Accessory http://www22.brinkster.com/accessory TableExistsDC = (DCount("ID", "MSysObjects", "Name='" & strTable & "' AND Type=1") > 0) End Function 'This is fine, but the DCount() function can be a bit slow. A slightly more long-winded 'solution is to enumerate through all tables in the database, stopping if one is found 'that matches the specified table. This is also slow the first time you run it, but is 'faster thereafter. Again, this function will return True if the specified table exists, 'False otherwise. ' Print TableExists("Customers") ' returns True Function TableExists(strTable As String) As Boolean ' Code sample from Accessory http://www22.brinkster.com/accessory Dim db As Database Dim i As Integer Set db = DBEngine.Workspaces(0).Databases(0) TableExists = False db.TableDefs.Refresh For i = 0 To db.TableDefs.Count - 1 If strTable = db.TableDefs(i).Name Then 'Table exists TableExists = True Exit For End If Next i Set db = Nothing End Function
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.