Find if Recordset exist

Hello experts!

I need to find out if a recordset exists in my database.
I have tried open the database and then a recordset with the given name, but since/if it do not exist then I ofcource get an error. Is there a nice  way to handle this? I want to see if it exist. Maybe a simple IF..then.. way??

Thank you
/Jens* - sorry but I only have these 10points...but guess it is an easy one.
Who is Participating?
alokanantConnect With a Mentor Commented:
Hi Jens,

You can use the foll. code to display all the tables in the database. This includes the system tables. The name of system tables begins with MSys... In case the table you r referring to in the select clause does not exist in the database, it shall give an error message. You can replace the MsgBox statement with a suitable if clause.

   Dim db As Database
   Dim i As Integer
   Set db = Workspaces(0).OpenDatabase("c:\winnt\profiles\90127\alok\vb5\db1.mdb")
   For i = 1 To db.TableDefs.Count
    MsgBox db.TableDefs(i).Name
   Next i

Jens979Author Commented:
It is not that I want to count the tables, I want to look for a specific.
Named fx 'brian'.

What about this little function:

Function DBTblExists(thisdb As Database, tblName As String) As Boolean

Dim Dummy As String

On Error GoTo DbTblExistsErr

Dummy = thisdb.TableDefs(tblName).Name
DBTblExists = True
Exit Function

DBTblExists = False
End Function

You could use it inside your programm eg like

Dim db as database
db = openDatabas(....)

If dbTblExists(db,"brian") Then
End If
Hi Jeff,

As I have already mentioned u can replace the msgbox function by a statement like if db.TableDefs(i).Name
   = "brian" then 'do ....

richsteig has suggested another method.

Jens979Author Commented:
Ok thanks guys!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.