Showing results 1 to 6 of 6

Thread: Drawn event

  1. #1
    flajoso Gast

    Default Drawn event

    Hi all,

    i hope this is not a stupid question (the probability is high!):

    i made a VBA script to check a value of a variable in a "Numerical Value" element, compare it to an array that i have and if is equal, do something... well this works great when i call the script by another button called "CHECK". But what i would like to do is call the script everytime the user changes the value on the inputbox, without clicking on the button "check". So, i called the script on the "DRAWN" event of the "Numerical Value" element and the element becomes invisible... i click on it, the dialogbox open..it runs the script great.. but the element is invisible! The element properties (color, representation) where unchanged.. maybe i'm missing something..

    Thanks in advance!
    Regards,

  2. #2
    Join Date
    30.05.2007
    Location
    Salzburg
    Posts
    879

    Default Re: Drawn event

    First of all the "Draw" Event is not the suitable Event for such action.

    Note:"With this method a dynamic element or a picture can be refreshed. The handle defined as a parameter is important for the drawing operation."

    The most used (and also the most propper) event in this case is the "LeftClickUp" Event. Using this I'm sure that your mentioned effect will not appear.

    But what i would like to do is call the script everytime the user changes the value on the inputbox, without clicking on the button "check".
    Using the VariableChange() Event at the VBA Variable OnlineContainer will give you the automatism which you want to have. Every time the value of a variable changes this event will be triggered.
    Please check following link regarding the VariableChange() Event:http://www.copadata.com/forums/showthread.php?t=41


    Private Sub zOLV_VariableChange(ByVal obVar As IVariable)
    Dim vValue As Variant
        vValue = obVar.Value
        'ignore error values (i.e. on startup)
        If IsError(vValue) Then Exit Sub
           if obVar.Name = "YourVariableName" 'name of the variable which should be compared with the array
               'comparision value and array
           end if
        End Sub

    Regards,
    Herbert

  3. #3
    flajoso Gast

    Default Re: Drawn event

    Hi Herbert,
    thanks!

    i'll try it and post the result!
    Regards,

  4. #4
    flajoso Gast

    Default Re: Drawn event

    Hi Herbert,
    i used the code like you advised and it is working 100%.
    Well, since we are not using the Recipe Group Manager, and just Standard Recipes, but we needed a Product description in a String, the goal was to display a field with the product description everytime the product_code changes. The product_description is checked in a CSV file wich is used by every system in the plant. Everytime the product_code is changed the CSV file will be checked for the existing code, and if it exists it will copy the description to a string.
    I'll put here the code.. maybe it can be usefull for someone..
    Thank you for all!


    Dim WithEvents zOLV As OnlineVariable
    Dim arrValues() As Variant
    
    Private Sub Check_CSV()
    
      Dim arBuffer() As String
      
      ' my CSV file with BARCODE,PRODUCT_DESCRIPTION
      
      Const FileRead = "c:\temp\myfile.txt"
     
      
      Dim FF1 As Integer
      Dim num As Integer
      Dim InputData As String
      Dim colA() As String
      Dim colB() As String
      Dim temp() As String
      
      Dim varCodBarras As String
      Dim varDescProd As Variable
      
      
      Dim j As Integer
      
      'barcode
      
     varCodBarras = GetArrayValue("strL1Prod.iO1CodBarras")
     
     
     On Error GoTo erh
    
    
    
    'Product Description
    
    Set varDescProd = thisProject.Variables.Item("strL1Prod.sO1Desc")
    
    'READ FILE
      FF1 = FreeFile
      num = 0
      Open FileRead For Input As #FF1
        'loop thru file
        Do While Not EOF(FF1)
          'get a line
          Line Input #FF1, InputData
          num = num + 1
          ReDim Preserve arBuffer(num)
          'store in array
          arBuffer(num) = InputData
        Loop
      Close #FF1
      
      'ok, all data in an array, now separate lines..
      
      For j = 1 To UBound(arBuffer)
    
      
            temp = Split(arBuffer(j), ",")
            ReDim Preserve colA(j)
            colA(j) = temp(0)    ' Column A of the file
            ReDim Preserve colB(j)
            colB(j) = temp(1)     ' column B of the file
                 If colA(j) = varCodBarras Then               'ok product exists, copy description..
       
                    varDescProd.Value = CStr(colB(j))
               
                    GoTo xit
                
                    Else
                    varDescProd.Value = "Produto INEXISTENTE" ' no product in the file
                 End If
            
       
      Next j
      
    xit:
      Exit Sub
    erh:
       MsgBox Err.Description, vbCritical, Err.Number
      Resume Next
    End Sub
    
    'routines from COPADATA Forum
    '|---------------------------------------------------------------------|
    '| init onlinevariable to fill an array with variable values on change |
    '|---------------------------------------------------------------------|
    Private Sub Project_Active()
        ReDim arrValues(1, 2)
        Set zOLV = thisProject.OnlineVariables.CreateOnlineVariables("MyOLV")
        If Not zOLV Is Nothing Then
            zOLV.Add "strL1Prod.iO1CodBarras"
            arrValues(0, 0) = "strL1Prod.iO1CodBarras"
            zOLV.Add "strL1Prod.iO2CodBarras"
            arrValues(0, 1) = "strL1Prod.iO2CodBarras"
            zOLV.Add "strL1Prod.iO3CodBarras"
            arrValues(0, 2) = "strL1Prod.iO3CodBarras"
            zOLV.Define
        Else
            MsgBox ("OnlineVariable ERROR, please restart!")
        End If
    End Sub
     
    '|---------------------------------------------------------------------|
    '| release objects                                                     |
    '|---------------------------------------------------------------------|
    Private Sub Project_Inactive()
        If Not zOLV Is Nothing Then zOLV.Undefine
        Call thisProject.OnlineVariables.DeleteOnlineVariables("MyOLV")
    End Sub
     
    '|---------------------------------------------------------------------|
    '| fill an array with the actually change variable values...           |
    '|---------------------------------------------------------------------|
    Private Sub zOLV_VariableChange(ByVal obVar As IVariable)
    Dim vValue As Variant
    Dim i As Integer
    Dim bAdded As Boolean
     
        vValue = obVar.Value
        If IsError(vValue) Then Exit Sub
        'update value within the array
        arrValues(1, GetArrayIndex(obVar.Name)) = vValue
        
                If obVar.Name = "strL1Prod.iO1CodBarras" Then
                'MsgBox ("naooooo!")
                Check_CSV
                
                End If
                
    End Sub
     
    '|---------------------------------------------------------------------|
    '| get index of array entry by the name of the variable                |
    '|---------------------------------------------------------------------|
    Function GetArrayIndex(strVarName As String) As Integer
    Dim i As Integer
        For i = 0 To UBound(arrValues, 2)
            If arrValues(0, i) = strVarName Then
                GetArrayIndex = i
                Exit Function
            End If
        Next i
    End Function
     
    '|---------------------------------------------------------------------|
    '| get value of array entry by the name of the variable                |
    '|---------------------------------------------------------------------|
    Function GetArrayValue(strVarName As String) As Variant
    Dim i As Integer
        For i = 0 To UBound(arrValues, 2)
            If arrValues(0, i) = strVarName Then
                GetArrayValue = arrValues(1, i)
                Exit Function
            End If
        Next i
    End Function

    Regards,

  5. #5
    Join Date
    30.05.2007
    Location
    Salzburg
    Posts
    879

    Default Re: Drawn event

    Nice to hear that it is working for you

    Viewing your code everything seems to be done correctly (and also in a very clean way). As far as I can see I think that have got the idea how to work with VBA inside zenOn

    One thing which I would change is the use of MsgBox (i.e. in Project_Active event and also in the Errorhandler). Instead of the information-output via MsgBox I would suggest to write this into the CEL via the WriteCelString or WriteCelStringEx method.

    Generaly using msgbox inside an application (regardless if zenOn Editor/Runtime or another application with integrated VBA interface) is not a good idea, because VBA runs in the same thread as the application itself. Thus, if a user interaction is necessary (like MessageBox) the application can be halted until the MessageBox is acknowleged.

    Beside that when working with files using the FileSystemObject (FSO) is a common way. Using this there are also methods implemented i.e. check if the file exists aso....

    Nethertheless, ... happy projecting with zenOn

    Regards,
    Herbert

  6. #6
    flajoso Gast

    Default Re: Drawn event

    Good idea! I'll change the Msgbox output information and i'll write it in CEL.
    Thnx!
    Regards,

Similar Threads

  1. Replies: 1
    Last Post: 6th May 2008, 12:43
  2. The event OnElementCreated is not executed
    By markclemens in forum VBA
    Replies: 0
    Last Post: 21st February 2008, 14:31

Posting Rules

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •