|
-
Aug 11th, 2005, 05:49 AM
#1
Thread Starter
New Member
[RESOLVED] Class Module
Hi
I am creating a class module that I only want to be able to use if a particular table is within the database.
I know how I can use an exists function to do this, but I'm not sure where to put the error trapping, would it be in the class initiate event, and if so, how do I then exit the class module?
also, presuming I get a response to this, how do I mark a thread as Resolved?
Cheers
Debbie
-
Aug 11th, 2005, 05:59 AM
#2
Thread Starter
New Member
Re: Class Module
Further to my above email, this is wwhat I am checking to see if the table exists
Private Sub Class_Initialize()
If TableExists("01_General_Details") = False Then
' i want some code in here to thorow me out of the class module, I'm not sure what to use to exit the class at this point.
End If
End Sub
-
Aug 11th, 2005, 06:09 AM
#3
Re: Class Module
You can declare a variable (private) at the top of the class module (under option explicit) as a boolean. This will be set on the initialise event to true if the table exists or false if it doesn't.
If you are running this in Access VBA you could use similar code to the following:
Code:
public function TestForTable(byval strTablename as string) as boolean
dim strName as string
On error resume next
TestForTable = false
strName=dbengine(0)(0).tabledefs(strTablename).name
if err.number=0 then
TestForTable=True
else
err.clear
end if
You'd need ADOx if you prefer ADO, and check a schema for the table name.
As to resolving posts, it has been made easier.
On viewing the thread, look up to the first bar, and there should be a "thread options" to click. Click it and select "Mark Thread Resolved"
Edit:
If you are using the class from a form, you may be able to use class events and raise one. Or just set the variable (first suggestion above) and add a property that returns it, so the calling sub can check if the table exists...
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Aug 11th, 2005, 07:05 AM
#4
Thread Starter
New Member
Re: Class Module
hmmm, sort of get it. This is what I have done: but, would I then have to set the variable (BlnTableName) = equal TestForTable somewhere?
Option Compare Database
Public BlnTableName As Boolean
Public Function TestForTable(ByVal strTablename As String) As Boolean
On Error Resume Next
Dim strName As String
TestForTable = False
strName = DBEngine(0)(0).TableDefs(strTablename).Name
If Err.Number = 0 Then
TestForTable = True
Else
Err.Clear
End If
End Function
-
Aug 11th, 2005, 07:22 AM
#5
Re: Class Module
Debbie,
Either set the function to private and call it within the class initilisation which will set the variable to be false.. or any easier method (and better in my eyes) is to change the funtion to a property let and change the variable to private so it is only available inside the class module. You will also need a property get so the code that is using the class module will be able to reference the value..
VB Code:
'in the class module put..
Private blnTableExists As Boolean
Property Get TestForTable() As Boolean
TestForTable = blnTableExists
End Property
Property Let TestForTable(strTableName)
On Error Resume Next
strName = DBEngine(0)(0).TableDefs(strTablename).Name
If Err.Number = 0 Then
blnTableExists = True
Else
Err.Clear
blnTableExists = False
End If
End Property
'in the module set the reference to the class module and exit if the first property is false
Dim MyClass As New ClassName
If TestForTable("TableName") Then
'Code to perform on success
End If
Set MyClass = Nothing 'Terminate the class
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation
-
Aug 11th, 2005, 07:35 AM
#6
Thread Starter
New Member
Re: Class Module
ahhh righ, I get it now, Thanks very much for your help x
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|