Dragon Drop
Dragon Drop - A Software Consultancy
Home      Our Products      Consultancy      Web Page Development      Services      Coding      Windows      External Resources     
Links      Newsletter      News And Issues      Books etc.      About Us     
CODING:   Exchange      Visual Basic      VBA      HomeSite     |     Coding Tools      Software Clinic     

Software Clinic - VB

Why Doesn't .Me Work As I Want?

Problem

I tried passing in the database name in my Form_Activate event:


Private Sub Form_Activate()

Static blnIsOld As Boolean                



  If blnIsOld = False Then

    Set m_DataClass = New dataClass



    With m_DataClass

      Set .FormName = Me                      'pass in the current form

      .dbName = "SalesBusinessDatabase.mdb"   'pass in name of database to connect to

      ............

     End With

but I couldn't get it to work.

This hardcoding does work:


Private Sub Class_Initialize()



  Dim sDBPath As String



    sDBPath = App.Path

    If Right(App.Path, 1) <> "\" Then

        sDBPath = sDBPath & "\"

    End If

    sDBPath = sDBPath & "SalesBusinessDatabase.mdb"   



    ' Establish the connection

    Set m_cnn = New Connection



    m_cnn.CursorLocation = adUseClient

    m_cnn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & sDBPath & ";"	

What am I missing here as far as passing in my database name from my form?

J.

Solution

Dear J

I can see the problem. If you put a break point on the Set .FormName = Me line and then hover the mouse over the Me variable you will see the name of the form. This is what you are expecting no doubt.

This is where the confusion lies. Me is a pointer to an Object - the form itself. What you are trying to do is to pass an Object reference where a string is expected. In the dedebugger, however, when you put the mouse over the object pointer it has to return something so, helpfully, it returns something which helps to identify the object.

As you perhaps know a form object has many properties. These include the Height of the form, the Width, the background colour, the border sizing properties and the caption. The caption is what you are wanting, therefore you should change the code to:


  Set .FormName = Me.Caption

and it would work.

If you want to explore this a little further put a breakpoint on this line and then when the breakpoint is triggered select the Me pointer and then add it to the Watch window and then drill down through the form object and have a look at the properties of the form and the objects which it contains.

Updates

If there are any suggestions for updates then please drop me a mail at malcolm.smith@dragondrop.com.