Results 1 to 8 of 8

Thread: [RESOLVED] WithEvents Not firing

  1. #1

    Thread Starter
    Member Mythrandil's Avatar
    Join Date
    Mar 2006
    Posts
    55

    Resolved [RESOLVED] WithEvents Not firing

    Hey

    Im fairly new to OO programming in VB and this is my first time trying to create my own events.

    What i have so far is simply:

    declaration:
    VB Code:
    1. Public Event Connected()
    routines:
    VB Code:
    1. Public Sub SQL_Connect()
    2. On Error GoTo sql_connect_err_handler
    3.     Set cn = New ADODB.Connection
    4.     If cn_string = vbNullString Then Err.Raise CN_STR_NULL
    5.     cn.ConnectionString = cn_string
    6.     cn.Open
    7.     Call Event_Connected
    8.  
    9. Exit Sub
    10. sql_connect_err_handler:
    11. If Err.Number = CN_STR_NULL Then
    12.     RaiseEvent Error(CN_STR_NULL, "", "Connection String Invalid")
    13. Else
    14.     RaiseEvent Error(Err.Number, Err.Source, Err.Description)
    15. End If
    16.  
    17. Private Sub Event_Connected()
    18.     RaiseEvent Connected
    19. End Sub

    And on the form:
    VB Code:
    1. Public WithEvents NewSql As ClsSQL
    2.  
    3. Private Sub Command1_Click()
    4. Set NewSql = New ClsSQL
    5. End Sub
    6.  
    7. Private Sub NewSql_Connected()
    8.     label1.caption "Connection Active"
    9. End Sub
    Tracing the code shows that the connection to db is fine etc etc but it passes through the RaiseEvent call without raising the forms event handler

    any ideas?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: WithEvents Not firing

    ADO already has a connection event. Actually its a WillConnect and ConnectComplete.
    VB Code:
    1. Option Explicit
    2.  
    3. Public WithEvents conn As ADODB.Connection
    4.  
    5. Private Sub conn_ConnectComplete(ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pConnection As ADODB.Connection)
    6.     MsgBox "Connected"
    7. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member Mythrandil's Avatar
    Join Date
    Mar 2006
    Posts
    55

    Re: WithEvents Not firing

    thanks for the reply but i'd like to use withevents for other situations like raising errors within the class if possible.

    thanks tho

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: WithEvents Not firing

    You never called the SQL_Connect procedure.

    VB Code:
    1. Private Sub Command1_Click()
    2.   Set NewSql = New ClsSQL
    3.   NewSQL.SQL_Connect
    4. End Sub

  5. #5
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667

    Re: WithEvents Not firing

    You are implementing the Event in the class, this is somthing you dont do implemtation of an event takes place in the form that uses an object basied on the event.

    Hope this helped!!


  6. #6

    Thread Starter
    Member Mythrandil's Avatar
    Join Date
    Mar 2006
    Posts
    55

    Re: WithEvents Not firing

    SQL_Connect is called on class initialization so it does connect.

    Implmentation of the event is on the form, the last code box is from the form the previous 2 are from the class. I just dont raise the event directly, i do it indirectly through a sub incase i need to change the way the event is called.

  7. #7
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    468

    Re: WithEvents Not firing

    Quote Originally Posted by Mythrandil
    SQL_Connect is called on class initialization so it does connect.
    Although I don't seem to be able to find it documented, I don't believe you can raise events from a class object until after the class' Initialization event has completed.

  8. #8

    Thread Starter
    Member Mythrandil's Avatar
    Join Date
    Mar 2006
    Posts
    55

    Re: WithEvents Not firing

    Quote Originally Posted by bpd
    Although I don't seem to be able to find it documented, I don't believe you can raise events from a class object until after the class' Initialization event has completed.
    give this man a medal!!

    thanks very much

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