Reputation: 11972
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?
Upvotes: 4
Views: 17087
Reputation: 180787
Put the function 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
Function
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
Upvotes: 3
Reputation: 1
Use On Error Resume Next
as given below example. There if a table (temp) is exist in database then it drops otherwise it leave the error and goes to next instruction's without giving any error.
On Error Resume Next
cmd.ActiveConnection = cn
cmd.CommandText = "drop table temp"
cmd.Execute , , adCmdText
Upvotes: 0