Results 1 to 24 of 24

Thread: Store variable content at runtime.

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    32

    Store variable content at runtime.

    Hi,

    I am trying to store the contents of the ADODB .CommandText property at run time every time it's value changes.
    This will allow me to collect all the used SQL and make a proper impact analysis of our companies proposed system changes.
    I have attempted to do this with an Add-In but I don't know enough about writing Add-Ins to pull it off.
    It needs to be reusable across hundreds of programs so any ideas very welcome.

    Kindest regards,

    Matt.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Store variable content at runtime.

    are you doing this in vb6 or vba?
    if in vb6 probably try with an activex dll
    then use
    WithEvents m_ADOConnection As ADODB.Connection
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    32

    Re: Store variable content at runtime.

    Hi there, thanks for the reply.
    Yes I am using VB6.

    I am currently trying to do it using an Add-In is that the same as an activeX dll? (Excuse my ignorance this is new ground for me).

    I'll look into the WithEvents m_ADOConnection As ADODB.Connection code you suggested and see how I get on. I'll post back with my progress.

    Matt.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Store variable content at runtime.

    An Addin is an extension to the VB editor - usually in the form of extra buttons on the toolbar (I've got one which automatically indents code the way I like it, and another that pastes an error handler).

    An ActiveX DLL is a VB program which doesn't get run by itself (it gets used by other programs), and can only expose class modules.


    Pete's idea seems to be that you make a class in it which uses WithEvents so you can detect when things happen with the connection - but I'm not sure if there events that will be useful for you.

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    32

    Re: Store variable content at runtime.

    I agree, I know what you mean.

    Anyone have any ideas how I can monitor all instances of .CommandText?

    What I need is kind of similar to the VB Watch Window but with the added functionality to store the changes in the variables/properties while the code is being run.

    Matt.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Store variable content at runtime.

    What I would do is save the values to a text file, which you can look at afterwards, or while in break mode. I doubt you could do it using an Addin (which I think are limited to design-time only), and using a form could be awkward.


    I just checked which events are available for the connection, and it seems like _ExecuteComplete could be your best bet - one of the various parameters is an ADODB.Command, which will hopefully be whichever Command object you used.

    If that works, you will only need to add a small amount of code to each project - to initiate the DLL, and pass the connection to it.

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    32

    Re: Store variable content at runtime.

    ok, that sounds promising.

    So in the code I need to pass each ADODB connection to the dll to be 'listened into'?

    I'll do some reading up on _ExecuteComplete now and post back.

    Thanks,

    Matt.

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Store variable content at runtime.

    I don't know if this will be useful for you but here is how you can keep track of variable changes, store and retrieve them.

    Code:
    Option Explicit
    Private mVar As Integer
    Private Sub Form_Load()
    MyVar = 2
    
    ' And to find out the value someplace else
    MyVar = GetSetting(App.EXEName, "Saved Variables", "Var One", 0)
    MsgBox MyVar
    End Sub
    Public Property Get MyVar() As Integer
    
        MyVar = mVar
        
    End Property
    
    Public Property Let MyVar(ByVal intNew As Integer)
    
        mVar = intNew
        SaveSetting App.EXEName, "Saved Variables", "Var One", mVar
    
    End Property

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    32

    Re: Store variable content at runtime.

    Hi Martin,

    Thank you for the reply, I dno't think it is quite what I'm after because I can't easily initiate this for every project I need to test and it is also difficult to implement because I will have several different ADODB objects in each project.

    Basically I need an alternate version of the Watch Window which dumps the values of variables and properties instead of displaying them on screen.

    Anyone know how this is done?

    Matt.

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Store variable content at runtime.

    you could use a timer, check if the variable changes then write it out to file or text box, whatever
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Store variable content at runtime.

    I briefly tested the following code and it seemed to worked, have a look!

    Class1 (just a sample)
    Code:
    Option Explicit
    
    Private WithEvents Conn As ADODB.Connection
    
    Private Sub Class_Initialize()
        Set Conn = New ADODB.Connection
        Conn.ConnectionString = ConnString
        Conn.Open
    End Sub
    
    Private Sub Class_Terminate()
        If Not Conn Is Nothing Then
            If Conn.state = adStateOpen Then
                Conn.Close
            End If
        End If
        Set Conn = Nothing
    End Sub
    
    Public Property Get AdoConnection() As ADODB.Connection
        Set AdoConnection = Conn
    End Property
    
    Private Sub Conn_WillExecute(Source As String, CursorType As ADODB.CursorTypeEnum, LockType As ADODB.LockTypeEnum, Options As Long, adStatus As ADODB.EventStatusEnum, ByVal pCommand As ADODB.Command, ByVal pRecordset As ADODB.Recordset, ByVal pConnection As ADODB.Connection)
        MsgBox Source, , "Source"
    End Sub
    Sample usage
    Code:
    Private Sub Command1_Click()
        Dim adoR    As ADODB.Recordset
        Dim adoC    As ADODB.Command
        Dim x       As Class1
        
        Set x = New Class1
        Set adoC = New ADODB.Command
        With adoC
            Set .ActiveConnection = x.AdoConnection
            .CommandText = "SELECT TOP 1 * FROM tabPatientRecords"
            .CommandType = adCmdText
            Set adoR = .Execute
        End With
    End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Store variable content at runtime.

    here is a function you could add to dee-u code
    vb Code:
    1. Public Function GetADO_Error() As String
    2.  
    3.     Dim i As Integer
    4.     Dim sQuery As String
    5.     Dim nNativeError As Long
    6.     Dim ADORecordset As ADODB.Recordset
    7.  
    8.     GetADO_Error = ""
    9.    
    10.     If (m_ADOConnection.Errors.Count = 0) Then Exit Function
    11.    
    12.     For i = 0 To m_ADOConnection.Errors.Count - 1
    13.         If (InStr(m_ADOConnection.Errors(i).Description, "Transaction rollback") > 0) Then
    14.             nNativeError = m_ADOConnection.Errors(i).NativeError
    15.             Exit For
    16.         End If
    17.     Next i
    18.        
    19.     If ((nNativeError >= 10000) And (nNativeError < 20000)) Then 'Error
    20.        GetADO_Error = "Error: " & Trim(Str(nNativeError))
    21.        nNativeError = nNativeError - 10000
    22.        sQuery = "Select Description from ImportErrors where ImportErrorID = " & Trim(Str(nNativeError))
    23.     ElseIf ((nNativeError >= 1) And (nNativeError < 10000)) Then 'Warning
    24.        sQuery = "Select Description from ImportWarnings where ImportWarningID = " & Trim(Str(nNativeError))
    25.        GetADO_Error = "Warning: " & Trim(Str(nNativeError))
    26.     ElseIf (nNativeError >= 20000) Then
    27.        sQuery = "Select Description from InternalODBCErrors where NativeErrorNumber = '" & Trim(Str(nNativeError) & "'")
    28.        GetADO_Error = Trim(Str(nNativeError))
    29.     Else
    30.         GetADO_Error = Err.Description
    31.         Exit Function
    32.     End If
    33.    
    34.     Set ADORecordset = m_ADOConnection.Execute(sQuery)
    35.     GetADO_Error = GetADO_Error & " - " & ADORecordset.Fields("Description").Value
    36.     ADORecordset.Close
    37.    
    38. End Function
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    32

    Unhappy Re: Store variable content at runtime.

    Hi,

    I really appreciate everyones input to this problem, thank you.

    The trouble I have is that all these solutions require changes to the actual code of the program to be tested and as I am dealing with hundreds of programs it just isn't feasible.

    That's why I thought about an ActiveX Add-In or some such thing. Trouble is I have a feeling Add-In's can only function at design time (though if someone knows different please tell me).

    This seems such a simple thing to want to do but yet there is nothing online about it.

    I have seen software available to buy which emulates the VB Watch Window with extended functionality but even this doesn't appear to have a 'dump to file' feature.

    If I could only write a simple version of the Watch Window myself I can easily include a function to dump all 'watched' variables and properties to file.
    It's so frustrating.

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Store variable content at runtime.

    i do not believe there is anyway to do what you want without some changes to the program, adding an object to an external activex seemed like the minimum amout of code within existing applications, to build a watch window would still have to be done within the existing program, or in an activex, then create an object of it within the program
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  15. #15

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    32

    Re: Store variable content at runtime.

    I take your point but the Watch Window isn't part of a project is it.

    The pay-for extended Watch windows are not part of a project either, they just interact with the project from the IDE.

    That's what I want to achieve, I can't beleive it's that difficult :-(

  16. #16
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Store variable content at runtime.

    i think all replying here thought you wanted it to work in your compiled program
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  17. #17

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    32

    Re: Store variable content at runtime.

    Yes, I think you are right, I just assumed I'd made it clear with:

    I have attempted to do this with an Add-In but I don't know enough about writing Add-Ins to pull it off.
    It needs to be reusable across hundreds of programs so any ideas very welcome.
    and

    What I need is kind of similar to the VB Watch Window but with the added functionality to store the changes in the variables/properties while the code is being run.
    So, to clarify, I really need to be able to programatically nominate several ***.CommandText properties to ""Watch"" and while running the program (from the compiler) I need to dump the changing value of these various properties (in their various contexts.).

    Please, if anyone knows, this would make a huge difference to me.

    Thanks,

    Matt.

  18. #18
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Store variable content at runtime.

    What dee-u posted is on a similar line to what I suggested before, but requires more work to integrate it.

    This modification is more like what I was thinking for the class/DLL, which should monitor all command/recordset objects that use a connection object:
    Code:
    Option Explicit
    
    Private WithEvents Conn As ADODB.Connection
    
    Private Sub Class_Terminate()
      Set Conn = Nothing
    End Sub
    
    Public Property Set AdoConnection(AdoConnection As ADODB.Connection)
      Set Conn = AdoConnection
    End Property
    
    Private Sub Conn_WillExecute(Source As String, CursorType As ADODB.CursorTypeEnum, LockType As ADODB.LockTypeEnum, Options As Long, adStatus As ADODB.EventStatusEnum, ByVal pCommand As ADODB.Command, ByVal pRecordset As ADODB.Recordset, ByVal pConnection As ADODB.Connection)
      'whatever logging features you want here
        MsgBox Source, , "Source"
    End Sub
    And the usage in each of your projects would be something like this:
    Code:
    'in the same declarations section as the connection (change Public to Private/Dim if apt)
    Public adoLogger  As YourClassName
    
    'Just after initialising the connection object ("Set yourConnection = New ... ")
        Set adoLogger = New YourClassName
        Set adoLogger.AdoConnection = yourConnection
    
    'Just before terminating the connection object ("Set yourConnection = Nothing")
        Set adoLogger = Nothing
    If you have more than one connection object in a project, simply repeat the usage section for each of them.


    An alternative would be to create a class which is a wrapper for the connection object (so you simply need to change the declaration for your connection objects), however you would have to put a lot of effort either into the class (to wrap each function/property your programs use) or modifying the usage (admittedly that could be a find/replace, but I wouldn't be keen on doing it).

  19. #19

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    32

    Re: Store variable content at runtime.

    Hi,

    These are all feasible solutions but they aren't quite what I'm after. Only because they would require too much fiddling with existing projects.
    This is a large company with many many users and many many programs. The logistics would be too much.

    To put this simply, I need the exact same functionality as the Watch Window but with the ability to dump to file the changes in variable values without breaking the code.

    This is all going to be tested before the project is compiled to an exe.

    I hope this makes sense and I really hope smoeone out there has the solution I need as it would make a HUGE difference to me.

    Thanks, Matt.

  20. #20
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Store variable content at runtime.

    We are aware of what you are dreaming of, but it seems that none of us have ever seen something like that, and can't think of a way to create exactly what you want.

    I don't know why you think the posted example would be too much fiddling - for each project it just requires adding a class (either directly into the project, or via a DLL you Reference) and four lines of code per connection object. Due to the usage of the class, those four lines could even be reduced to two by using Dim as New (which is normally a bad idea, but might be apt here).

  21. #21

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    32

    Re: Store variable content at runtime.

    I accept that and I am very grateful to everyone for their input.

    The reason I say it's too much fiddling is that we have about 400 individual programs, these have (and I quote a rough average here) about 10 DIFFERENT unique connections to the database, some less obviously and some many more.
    All the projects are controled by eChangeMan as a source control tool which adds another level of complexity. It's possible but just a whole lot of effort.

    I know I'm probably being lazy but I have a lot of other things going on too :-)

    Is it not possible to write something which listens in to a compiled exe and checks for SQL calls being parsed to the ODBC driver?

    Regards,

    Matt.

  22. #22
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Store variable content at runtime.

    There might be a way of writing some kind of wrapper for an ODBC Driver/OLEDB Provider, but it would not be easy at all (and it is likely to be almost undocumented).

    There might be a program out there already that does it, and if memory serves ODBC itself provides a logging feature (via the control panel applet), but using it slows database interaction down dramatically.

  23. #23

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    32

    Re: Store variable content at runtime.

    Quote Originally Posted by si_the_geek
    There might be a way of writing some kind of wrapper for an ODBC Driver/OLEDB Provider, but it would not be easy at all (and it is likely to be almost undocumented).
    I expect it would be :-(

    Quote Originally Posted by si_the_geek
    There might be a program out there already that does it, and if memory serves ODBC itself provides a logging feature (via the control panel applet), but using it slows database interaction down dramatically.
    I have tried this and yes, it slows function down way way too much but interesting ly it didn't actually catalogue the SQL strings!?!?!?!? This was a bit confusing.

    Matt.

  24. #24
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Store variable content at runtime.

    Another idea could be a packet sniffer that would track the sql statements being passed by your program though I am certain that would be difficult. When I had the need to use a packet sniffer I believe my sql statements are also being sniffed.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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