tags:

views:

1168

answers:

2

How to check table exist or not?

USING VB 6.0 AND ACCESS 2003

My code.

Cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & App.Path & "\iTDC-ACS.MDB"
Cn.Open

cmdCardEvent.ActiveConnection = Cn
cmd.ActiveConnection = Cn

cmdcardevent1.ActiveConnection = Cn
cmd.ActiveConnection = Cn

cmd.CommandText = "DROP TABLE tmp_cardevent"
cmd.Execute

cmd.CommandText = "drop table tmp_MOI"
cmd.Execute

Here I want to check whether table exist then drop table the else no need.

How to check table exist or not?

Need VB 6 code Help?

A: 
Public Function IsExistingTable( _
      ByVal Database As String, _
      ByVal TableName As String _
   ) As Boolean

   Dim ConnectString As String
   Dim ADOXConnection As Object
   Dim ADODBConnection As Object
   Dim Table As Variant

   ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Database
   Set ADOXConnection = CreateObject("ADOX.Catalog")
   Set ADODBConnection = CreateObject("ADODB.Connection")
   ADODBConnection.Open ConnectString
   ADOXConnection.ActiveConnection = ADODBConnection
   For Each Table In ADOXConnection.Tables
      If LCase(Table.Name) = LCase(TableName) Then
         IsExistingTable = True
         Exit For
      End If
   Next
   ADODBConnection.Close

End Function

NOTE: It's going to be cheaper to just do the DROP TABLE without checking first.

Robert Harvey
am new to vb? How can i use this function from my posted code
Gopal
A: 

Put the function in the answer below this one into a public module.

Example code to call the function:

Dim result as boolean
result = IsExistingTable("c:\myFolder\myDatabase.mdb","myTableName")
If result Then
   'Do something
Else
   'Do something else.
Endif
Robert Harvey
Showing arguement not option error "IsExistingTable"
Gopal
Argument not optional? There are two arguments, Database (a string), and TableName (also a string). Which one are you missing?
Robert Harvey