Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 MS Access
 Check for Access table existance from SQL HELP!!!

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-07-10 : 09:07:46
brad writes "What I am trying to do might be impossible but here goes anyway...

Every day i need to run a DTS package that imports data from up to 5 tables in an Access database into a table in the SQL database.

The table names are called 'MMDD' (i.e., [0705], [0704], etc.)

I need to select and insert from tables within the last 5 days.
to do this i wrote this SQL statement:

declare @TableName nvarchar(30),
@sql nvarchar(255),
@dval int
set @dval = 5

while @dval > 0
begin
set @TableName= right(convert(varchar, getdate() - @dval, 112),4)

set @sql = 'select * from opendatasource(' + char(39) + 'Microsoft.Jet.OLEDB.4.0'
+ char(39) + ',' + char(39) + 'Data Source="C:\MyDatabase.mdb"'
+ char(39) + ')...[' + @TableName + ']'

execute(@sql)
set @dval= @dval- 1
end

This results in an error when the table does not exist in the database (we don't get a table during weekends)


My solution was to include the statement 'if exists(select * from MSysObjects [ the access equivalent of dbo.sysobjects ] where name =' + @TableName + ')'

When I tried to query the MSysObjects table in the Access db but it says that the table "does not contain any columns"

Upon reading up on the error , (and after opening the db itself and double-checking) I discovered that it is inaccessible by ODBC due to readonly permissions.

The database requires no login, and I'm not sure why I dont have access to it but I can't include that table in my DTS package either.

The other idea I had was to tell SQL to ignore the error and continue the loop, but I haven't had any luck finding any documentation on how to achieve this...

My only other option is to do this in an ActiveX script but my VB is not very good, and that is a last resort.

Please Help

Brad"

SMerrill
Posting Yak Master

206 Posts

Posted - 2003-05-12 : 12:57:48
Here is an Access VB routine to check for the existence of an object in the MS Access database:

' Pass the Object type: Table, Query, Form, Report, Macro, or Module
' Pass the Object Name
Function ObjectExists(strObjectType As String, strObjectName As String) As Boolean
Dim db As Database
Dim tbl As TableDef
Dim qry As QueryDef
Dim i As Integer

Set db = CurrentDb()
ObjectExists = False

If strObjectType = "Table" Then
For Each tbl In db.TableDefs
If tbl.NAME = strObjectName Then
ObjectExists = True
Exit Function
End If
Next tbl
ElseIf strObjectType = "Query" Then
For Each qry In db.QueryDefs
If qry.NAME = strObjectName Then
ObjectExists = True
Exit Function
End If
Next qry
ElseIf strObjectType = "Form" Or strObjectType = "Report" Or strObjectType = "Module" Then
For i = 0 To db.Containers(strObjectType & "s").Documents.Count - 1
If db.Containers(strObjectType & "s").Documents(i).NAME = strObjectName Then
ObjectExists = True
Exit Function
End If
Next i
ElseIf strObjectType = "Macro" Then
For i = 0 To db.Containers("Scripts").Documents.Count - 1
If db.Containers("Scripts").Documents(i).NAME = strObjectName Then
ObjectExists = True
Exit Function
End If
Next i
Else
MsgBox "Invalid Object Type passed, must be Table, Query, Form, Report, Macro, or Module"
End If

End Function


The only problem with this for you would be that the CurrentDB() function would have to be replaced with an object reference bound to the Access database.


--SMerrill
Seattle, WA
Go to Top of Page
   

- Advertisement -