My Windows Form Disappears in VBA

2

I am facing a challenge with my VBA project.

I recently created a form where the ListBox component and CommandButton, nothing complex. But does the form always disappear from the VBA, any idea what it should be?

Form Code:

Private Sub CommandButtonGuardar_Click()

On Error GoTo TrataErro

    EditorVendas.DocumentoVenda.CamposUtil("CDU_Banco") = ListBox1.List(ListBox1.ListIndex, 1)

    Unload Me

Exit_point:
    On Error Resume Next
    Exit Sub

TrataErro:
  PSO.Dialogos.MostraAviso "Ocorreu um erro ao selecionar o banco: " & Err.Description, PRI_Critico

  Resume Exit_point

End Sub

Private Sub UserForm_Activate()

On Error GoTo TrataErro

    Dim ListaSql As StdBELista

    'Buscar os Bancos
    Set ListaSql = BSO.Consulta("Select Conta,Banco,DescBanco,NumConta from contasbancarias where Banco <> ''")

    If Not (ListaSql Is Nothing) Then
       'Percorrer a lista
       If ListaSql.NumLinhas <> 0 Then
            While Not (ListaSql.NoInicio Or ListaSql.NoFim)
                'Adciona item a lista
                With ListBox1
                    .ColumnCount = 4
                    .AddItem
                    .List(i, 0) = ListaSql("Banco")
                    .List(i, 1) = ListaSql("Conta")
                    .List(i, 2) = ListaSql("DescBanco")
                    .List(i, 3) = Format(ListaSql("NumConta"), "#### #### #####")
                End With
               i = i + 1
                ListaSql.Seguinte
            Wend
        End If

    End If

Exit_point:

    On Error Resume Next

    Set ListaSql = Nothing

    Exit Sub

TrataErro:
  PSO.Dialogos.MostraAviso "Ocorreu um erro ao inicializar o formulario.: " & Err.Description, PRI_Critico

Resume Exit_point

End Sub

Here, in the EditorDialog , this is where I call the form:

Private Sub EditorVendas_AntesDeImprimir(Cancel As Boolean)
On Error GoTo TrataErro

    '######## Pedir Conta Bancaria imprimir no report de Venda (FA ou outros que venham a existir ######
    '######## Author: Cabral Ussene
    '######## Date: 2018/05/09
    '######## Review Date: 2018/05/11 by Cabral Ussene

        If BSO.Comercial.TabVendas.DaValorAtributo(Me.DocumentoVenda.TipoDoc, "CDU_MostrarCBancaria") Then frmContaBancaria.Show

        'Validar se foi actualizado a conta bancaria
        If Antiga_ContaBancaria <> DocumentoVenda.CamposUtil("CDU_Banco") Then
            'Actualizar a CDU_Banco
            BSO.Comercial.Vendas.ActualizaValorAtributo DocumentoVenda.Filial, DocumentoVenda.TipoDoc, DocumentoVenda.Serie, DocumentoVenda.NumDoc, "CDU_Banco", DocumentoVenda.CamposUtil("CDU_Banco")
        End If

    '####################################################################################################

Exit_point:

    On Error Resume Next

    Exit Sub

TrataErro:
  PSO.Dialogos.MostraAviso "Ocorreu um erro antes de imprimir o documento...", PRI_Critico, Err.Description

  Resume Exit_point

End Sub

However, what happens is when the user is going to print the document, this is an error, and when they check in the VBA project the form frmContaBancaria is not there, as if someone had deleted it .

    
asked by anonymous 05.06.2018 / 10:02

1 answer

1

First, avoid using Activate (), because in this case the form is being activated several times. Therefore, you should use Initialize () and the form will run once before it is presented.

Second, for the sake of best practice, you should create a new instance of the form object. So the code should be something like this:

Private Sub EditorVendas_AntesDeImprimir(Cancel As Boolean)
Dim objForm As New UserForm1

  On Error GoTo TrataErro

  objForm.Show

  'Validar se foi actualizado a conta bancaria
  If Antiga_ContaBancaria <> DocumentoVenda.CamposUtil("CDU_Banco") Then

    'Actualizar a CDU_Banco
    BSO.Comercial.Vendas.ActualizaValorAtributo DocumentoVenda.Filial, DocumentoVenda.TipoDoc, DocumentoVenda.Serie, DocumentoVenda.NumDoc, "CDU_Banco", DocumentoVenda.CamposUtil("CDU_Banco")

  End If

  Set objForm = Nothing
  Exit Sub

TrataErro:
    PSO.Dialogos.MostraAviso "Ocorreu um erro antes de imprimir o documento...", PRI_Critico, Err.Description

End Sub

Private Sub EditorVendas_TipoDocumentoIdentificado(Tipo As String, Cancel As Boolean)

End Sub
    
05.06.2018 / 19:37