Results 1 to 6 of 6

Thread: [RESOLVED] Class Module

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    11

    Resolved [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

  2. #2

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    11

    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

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    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...

    BOFH Now, BOFH Past, Information on duplicates

    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...

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    11

    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

  5. #5
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    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:
    1. 'in the class module put..
    2. Private blnTableExists As Boolean
    3. Property Get TestForTable() As Boolean
    4.   TestForTable = blnTableExists
    5. End Property
    6. Property Let TestForTable(strTableName)
    7.   On Error Resume Next
    8.   strName = DBEngine(0)(0).TableDefs(strTablename).Name
    9.   If Err.Number = 0 Then
    10.     blnTableExists = True
    11.   Else
    12.     Err.Clear
    13.     blnTableExists = False
    14.   End If
    15. End Property
    16.  
    17. 'in the module set the reference to the class module and exit if the first property is false
    18. Dim MyClass As New ClassName
    19. If TestForTable("TableName") Then
    20.   'Code to perform on success
    21. End If
    22. Set MyClass = Nothing 'Terminate the class
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    11

    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
  •  



Click Here to Expand Forum to Full Width