473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Checking to see if a table is empty

133 100+
A procedure is calling a function to see if a table is empty through a Boolean value, however, i am receiving a compiler error. any assistance, would be appreciated.

the code is:

Expand|Select|Wrap|Line Numbers
  1.     If TableExists = False Then
  2.         DoCmd.DeleteObject acTable, "ContractImport"
  3.         DoCmd.DeleteObject acTable, "ContractImport$_ImportErrors"
  4.     End If
  5.  
Expand|Select|Wrap|Line Numbers
  1. Public Function TableExists(strTable As String) As Boolean
  2.     Dim strName As String
  3.  
  4.     On Error Resume Next
  5.     'If table exists already then strName will be > ""
  6.     strName = CurrentDb.TableDefs(strTable).Name
  7.     TableExists = Not (strName = "")
  8.  
  9. End Function
  10.  
Jun 14 '10 #1

✓ answered by ADezii

@jimatqsi
As previously pointed ou by jamitqsi:
Expand|Select|Wrap|Line Numbers
  1. If TableExists("Employees") Then
  2.   MsgBox "Exists"
  3. Else
  4.   MsgBox "No exists"
  5. End If

8 7064
jimatqsi
1,271 Expert 1GB
dowlingm815,
Your function declaration
Expand|Select|Wrap|Line Numbers
  1. Function TableExists(strTable As String)
indicates the function requires an input parameter, yet your call to the function
Expand|Select|Wrap|Line Numbers
  1. If TableExists = False Then
is not supplying the needed parameter.

You need to add the name of the table to the call to the function so it knows what table name to check.

Jim
Jun 14 '10 #2
ADezii
8,834 Expert 8TB
@jimatqsi
As previously pointed ou by jamitqsi:
Expand|Select|Wrap|Line Numbers
  1. If TableExists("Employees") Then
  2.   MsgBox "Exists"
  3. Else
  4.   MsgBox "No exists"
  5. End If
Jun 15 '10 #3
dowlingm815
133 100+
thanks appreciate the correction. here is the working code:
Expand|Select|Wrap|Line Numbers
  1.     If TableExists("ContractImport") Then
  2.     ' delete the table if it exists
  3.         DoCmd.DeleteObject acTable, "ContractImport"
  4.     End If
  5.     If TableExists("ContractImport$_ImportErrors") Then
  6.     ' delete the table if it exists
  7.         DoCmd.DeleteObject acTable, "ContractImport$_ImportErrors"
  8.     End If
  9.  
  10.  
Expand|Select|Wrap|Line Numbers
  1. Public Function TableExists(strTable As String) As Boolean
  2.    On Error GoTo Err_Hndlr
  3.  
  4.    Dim strName As String
  5.  
  6. '    On Error Resume Next
  7.     TableExists = True
  8.     'If table exists already then strName will be > ""
  9.     strName = CurrentDb.TableDefs(strTable).Name
  10.     TableExists = Not (strName = "")
  11.     Exit Function
  12.  
  13. Err_Hndlr:
  14.     TableExists = False
  15.  
  16. End Function
  17.  
Jun 15 '10 #4
ADezii
8,834 Expert 8TB
Given your circumstances, another option to consider is (In-Line Code):
Expand|Select|Wrap|Line Numbers
  1. Dim tdf As DAO.TableDef
  2. Const conTABLE_NAME As String = "ContractImport"
  3.  
  4. For Each tdf In CurrentDb.TableDefs
  5.   If Left$(tdf.Name, Len(conTABLE_NAME)) = conTABLE_NAME Then
  6.     CurrentDb.TableDefs.Delete tdf.Name
  7.   End If
  8. Next
Jun 15 '10 #5
dowlingm815
133 100+
@ADezii
I'm not sure if i follow the instructions, please keep me honest. it looks like it calculating the length of the table correct?

mary
Jun 16 '10 #6
ADezii
8,834 Expert 8TB
@dowlingm815
In a Nutshell, any Table beginning with ContractImport will be DELETED!
Jun 16 '10 #7
dowlingm815
133 100+
@ADezii
that's awesome, but i still don't understand the code.

the following code, reads each table NAME in the db, correct?

Expand|Select|Wrap|Line Numbers
  1. For Each tdf In CurrentDb.TableDefs
  2.  
the next statement, calculates the length of the string, but what does the rest do?

Expand|Select|Wrap|Line Numbers
  1.   If Left$(tdf.Name, Len(conTABLE_NAME)) = conTABLE_NAME Then
  2.  
i appreciate all your guidance.

mary
Jun 17 '10 #8
ADezii
8,834 Expert 8TB
@dowlingm815
The code loops through every Table in the Database and if the Table Name begins with the String defined in the Constant (conTABLE_NAME ), it is then DELETED! The code is compact and efficient, but the only drawback would be if you had Tables named:
Expand|Select|Wrap|Line Numbers
  1. ContractImport_2
  2. ContractImport_New
  3. ContractImport_06182010
  4. ContractImportFK
  5. ContractImportYaDaYaDa
  6. ContractImport Error
They would all be DELETED!
Jun 18 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Dan Bass | last post by:
which one do you use and why? MyString == null || MyString == "" vs MyString == null || MyString.Length == 0
2
by: Ron | last post by:
Can anyone tell me how to check and see if a dataset is empty? I am writing a search function that returns a dataset and binds it to a datagrid. If no data is found, I would like to make the grid...
2
by: momo | last post by:
I need to find out if a user name have been create before in a table. if so I don't want to allow new users to recreate it.
8
by: J-P-W | last post by:
Hi, anyone got any thoughts on this problem? I have sales reps. that remotely send their data to an ftp server. The office downloads all files, the code creates an empty file, then downloads the...
13
by: lithoman | last post by:
I'm stumped here. I run the procedure Batch_Select against the database with @ID=18 and I get the expected data. When it loads into a SqlDataReader, it gets messed up somehow. Initially, after the...
0
by: rdemyan | last post by:
Is there a way to check the validity of front-end table links on a backend file where all permissions to data have been revoked. To get at the data, the front end uses RWOP queries. What I'm...
2
by: Sanjay80 | last post by:
I want to Update one table but same time if any column from table ishaving Primary kay or Foreign Kay it should not allow me to do so. I am writting Stored Procedure
1
by: psbasha | last post by:
Hi, Whether we can check the empty list or dict by i"f "conditon or catch this exception by "try" and "catch" blocks. Which will be the best practctice?. In my work I have to play with...
1
by: BerkshireGuy | last post by:
I have a Access database that should have data for each weekday of the year. There is a field called DateDownloaded, which should have at least one record for each weekday of the current year. ...
1
by: =?Utf-8?B?U2F2dm91bGlkaXMgSW9yZGFuaXM=?= | last post by:
In a RowDataBound event, I want to check if a column is empty (it's a simple column, not a template with a label in it). I use the following syntax to get the value from the cell: s =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.