Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Database Access / November 2005



Tip: Looking for answers? Try searching our database.

beginner question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MP - 30 Nov 2005 01:08 GMT
Trying to learn ado/adox
Creating a class to simplify calling code and assist in learning process.

example of a couple methods/propeties
.TableExists    (method to determine if table of given name exists)
.Tables    (property to return Tables collection of adox catalog of current
connection)
.Table    (property to return a table object of given name)
.Catalog (property to return adox.Catalog with current open ado.connection
as .ActiveConnection)

but even when the TableExists function returns true, the following call
fails to get the table object
assuming oDb is an instance of my class

 If oDb.TableExists(sTableName) Then
   Debug.Print "Table exists"  '(that prints in immediate so I know table
exists)
   'operation errors here
   Set oTbl = oDb.Tables.Item(sTableName)
'that line raises the following err.description
'Item cannot be found in the collection corresponding to the requested name
or ordinal.

in db.cls module

Function TableExists(sTableName As String) As Boolean
 Dim oTestTbl As Table
 On Error Resume Next
   Set oTestTbl = Me.Tables.Item(sTableName)
   If Not Err Then
     Set oTestTbl = Nothing
     TableExists = True
     Exit Function
   End If
 On Error GoTo 0
End Function

the .Tables Property:
(moCatalog is a member variable of type adox.Catalog object)
Property Get Tables() As Tables
   'ensure connection is open and catalog is set
     If Me.Catalog Is Nothing Then
        MsgBox "No catalog"
     End If

 If moCatalog.Tables Is Nothing Then
     MsgBox "moCatalog.Tables Is Nothing "
   Else
     Set Tables = moCatalog.Tables
 End If

End Property

the .Catalog property
(moConn is a member variable of type ado.Connection object)
Property Get Catalog() As ADOX.Catalog
 If moConn Is Nothing Then
   Exit Property
 End If

 If Not moConn.State = adStateOpen Then
   Exit Property
 End If

 If moCatalog Is Nothing Then
   Set moCatalog = New ADOX.Catalog
 End If

 If moCatalog.ActiveConnection Is Nothing Then
   Set moCatalog.ActiveConnection = moConn
 End If

 Set Catalog = moCatalog

End Property

not sure what I'm doing wrong here.
Thanks for any pointers
Mark
MP - 30 Nov 2005 04:15 GMT
ok, I figured part of it out, the tableExists function returns a false
positive...
I don't understand why, but I rewrote it so now it works

> Function TableExists(sTableName As String) As Boolean
>   Dim oTestTbl As Table
>   On Error Resume Next

'I have no idea why this doesn't throw an error inside this function but the
same line does throw an error in the calling proc.
>     Set oTestTbl = Me.Tables.Item(sTableName)

>     If Not Err Then
>       Set oTestTbl = Nothing
[quoted text clipped - 3 lines]
>   On Error GoTo 0
> End Function

so I rewrote it thus:

Function TableExists(sTableName As String) As Boolean
 Dim oTbl As Table
 For Each oTbl In Me.Tables
   If UCase(oTbl.Name) = UCase(sTableName) Then
     TableExists = True
     Exit For
   End If
 Next oTbl
End Function

which seems incredibly lame way to do it but it works so I guess that's what
counts.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.