Notifications
Clear all

Como Configurar textbox inseridos dinamicamente

3 Posts
2 Usuários
0 Reactions
1,581 Visualizações
(@filoap)
Posts: 0
New Member
Topic starter
 

Pessoal estou com um probleminha em um desenvolvimento que estou trabalhando, tenho um excel e nele crio dinamicamente uma linha que contem alguns textbox e combobox.

O problema começa quando tenho que definir o textbox para Moeda e Valor, não consigo localizar o "nome" do textbox e com isso todos os campos ficam livres.
Tenho o mesmo problema quando para fazer a configuração de um dos textbox para data.

Podem me ajudar? Estou pesquisando no forum mas até o momento não achei nada relacionado.

Private Sub Add_Product_Click()

'declara no novo controle
Dim pntxt As Control
Dim qtdtxt As Control
Dim mocbx As Control
Dim estxt As Control
Dim oftxt As Control
Dim ortxt As Control
Dim sntxt As Control
Dim crdtxt As Control
Dim sttxt As Control

Dim prodlbl As Control
Dim qtdlbl As Control
Dim currlbl As Control
Dim exsulbl As Control
Dim oflbl As Control
Dim orlbl As Control
Dim snlbl As Control
Dim crdlbl As Control
Dim stlbl As Control


Dim t As Integer

            'SE USUARIO NÃO DIGITAR VALOR, RETORNA A MSG

            If QITtxt = "" Then
                MsgBox "Necessário Inserir um valor no campo Qty de Itens", vbExclamation, "Atenção"
                Else
                      
'//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////'
                            
'//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////'
                            
'//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////'

                        'TEXT & COMBOBOX DA LINHAS INSERIDAS
                        ' Adiciona o Textbox do PN iniciando na posição Top 60 + 45 * a quantidade de itens que o usuario ira preencher
                        For t = 1 To QITtxt
                            Set pntxt = Frame1.Controls.Add("Forms.TextBox.1", True)
                            Set qtdtxt = Frame1.Controls.Add("Forms.TextBox.1", True)
                            Set mocbx = Frame1.Controls.Add("Forms.combobox.1", True)
                            Set estxt = Frame1.Controls.Add("Forms.TextBox.1", True)
                            Set oftxt = Frame1.Controls.Add("Forms.TextBox.1", True)
                            Set ortxt = Frame1.Controls.Add("Forms.TextBox.1", True)
                            Set sntxt = Frame1.Controls.Add("Forms.TextBox.1", True)
                            Set crdtxt = Frame1.Controls.Add("Forms.TextBox.1", True)
                            Set sttxt = Frame1.Controls.Add("Forms.TextBox.1", True)
                            
                            With pntxt
                                .Top = 60 + (45 * t)
                                .Left = 6
                                .Width = 120
                                .Height = 24
                                .Font.Size = 10
                                .Font.Name = "Tahoma"
                                .MultiLine = True
                                .EnterKeyBehavior = True
                                .ScrollBars = 2
                            End With
                        
                            With qtdtxt
                                .Top = 60 + (45 * t)
                                .Left = 130
                                .Width = 45
                                .Height = 24
                                .Font.Size = 10
                                .Font.Name = "Tahoma"
                                .MultiLine = True
                                .EnterKeyBehavior = True
                                .ScrollBars = 2
                            End With
                            
                            With mocbx
                                .Name = "Result_Text" & t
                                '.Caption = ""
                                .Top = 60 + (45 * t)
                                .Left = 179
                                .Width = 50
                                .Height = 24
                                .Font.Size = 10
                                .Font.Name = "Tahoma"
                                .BackColor = &H80FFFF
                                .AddItem "EUR"
                                .AddItem "USD"
                                .AddItem "BRL"
                                .AddItem "CLP"
                                .AddItem "MXN"
                                .AddItem "ARS"
                            End With
                        
                            With estxt
                                .Top = 60 + (45 * t)
                                .Left = 233
                                .Width = 72
                                .Height = 24
                                .Font.Size = 10
                                .Font.Name = "Tahoma"
                                .MultiLine = True
                                .EnterKeyBehavior = True
                                .ScrollBars = 2
                            End With
                                                                               
                            With oftxt
                                .Top = 60 + (45 * t)
                                .Left = 309
                                .Width = 72
                                .Height = 24
                                .Font.Size = 10
                                .Font.Name = "Tahoma"
                                .MultiLine = True
                                .EnterKeyBehavior = True
                                .ScrollBars = 2
                            End With
                        
                            With ortxt
                                .Top = 60 + (45 * t)
                                .Left = 385
                                .Width = 72
                                .Height = 24
                                .Font.Size = 10
                                .Font.Name = "Tahoma"
                                .MultiLine = True
                                .EnterKeyBehavior = True
                                .ScrollBars = 2
                            End With
                        
                            With sntxt
                                .Top = 60 + (45 * t)
                                .Left = 461
                                .Width = 72
                                .Height = 24
                                .Font.Size = 10
                                .Font.Name = "Tahoma"
                                .MultiLine = True
                                .EnterKeyBehavior = True
                                .ScrollBars = 2
                            End With
                        
                            crdtxt.MaxLength = 10
                            With crdtxt
                                .Top = 60 + (45 * t)
                                .Left = 537
                                .Width = 72
                                .Height = 24
                                .Font.Size = 10
                                .Font.Name = "Tahoma"
                                .MultiLine = True
                                .EnterKeyBehavior = True
                                .ScrollBars = 2
                            End With
                             With sttxt
                                .Top = 60 + (45 * t)
                                .Left = 613
                                .Width = 72
                                .Height = 24
                                .Font.Size = 10
                                .Font.Name = "Tahoma"
                                .MultiLine = True
                                .EnterKeyBehavior = True
                                .ScrollBars = 2
                            End With
                        Next
                           
                            
'//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////'
                            
'//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////'
                            
'//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////'

                        'LABLEL DA LINHAS INSERIDAS
                        ' Adiciona o label do Produto iniciando na posição Top 45 + 45 * a quantidade de itens que o usuario ira preenher
                        For t = 1 To QITtxt
                           Set prodlbl = Frame1.Controls.Add("Forms.label.1", True)
                           Set qtdlbl = Frame1.Controls.Add("Forms.label.1", True)
                           Set currlbl = Frame1.Controls.Add("Forms.label.1", True)
                           Set exsulbl = Frame1.Controls.Add("Forms.label.1", True)
                           Set oflbl = Frame1.Controls.Add("Forms.label.1", True)
                           Set orlbl = Frame1.Controls.Add("Forms.label.1", True)
                           Set crdlbl = Frame1.Controls.Add("Forms.label.1", True)
                           Set snlbl = Frame1.Controls.Add("Forms.label.1", True)
                           Set stlbl = Frame1.Controls.Add("Forms.label.1", True)
                           
                           With prodlbl
                                .Name = "FieldLabel"
                                .Caption = "Product"
                                .Top = 45 + (45 * t)
                                .Left = 6
                                .Width = 72
                                .Height = 12
                                .Font.Size = 9
                                .Font.Name = "Tahoma"
                                .FontBold = True
                                '.BackColor = &H80FFFF
                            End With
                        
                           With qtdlbl
                                .Name = "FieldLabel"
                                .Caption = "Quantity"
                                .Top = 45 + (45 * t)
                                .Left = 135
                                .Width = 50
                                .Height = 12
                                .Font.Size = 9
                                .Font.Name = "Tahoma"
                                .FontBold = True
                                '.BackColor = &H80FFFF
                            End With
                       
                           With currlbl
                                .Name = "FieldLabel"
                                .Caption = "Currency"
                                .Top = 45 + (45 * t)
                                .Left = 179
                                .Width = 72
                                .Height = 12
                                .Font.Size = 9
                                .Font.Name = "Tahoma"
                                .FontBold = True
                                '.BackColor = &H80FFFF
                            End With
                       
                           With exsulbl
                                .Name = "FieldLabel"
                                .Caption = "Exec Summary"
                                .Top = 45 + (45 * t)
                                .Left = 233
                                .Width = 72
                                .Height = 12
                                .Font.Size = 9
                                .Font.Name = "Tahoma"
                                .FontBold = True
                                '.BackColor = &H80FFFF
                           End With
                                               
                           With oflbl
                                .Name = "FieldLabel"
                                .Caption = "Offer Value"
                                .Top = 45 + (45 * t)
                                .Left = 309
                                .Width = 72
                                .Height = 12
                                .Font.Size = 9
                                .Font.Name = "Tahoma"
                                .FontBold = True
                                '.BackColor = &H80FFFF
                            End With
                        
                           With orlbl
                                .Name = "FieldLabel"
                                .Caption = "Order Value"
                                .Top = 45 + (45 * t)
                                .Left = 385
                                .Width = 72
                                .Height = 12
                                .Font.Size = 9
                                .Font.Name = "Tahoma"
                                .FontBold = True
                                '.BackColor = &H80FFFF
                            End With
                        
                           With crdlbl
                                .Name = "FieldLabel"
                                .Caption = "CRD"
                                .Top = 45 + (45 * t)
                                .Left = 461
                                .Width = 72
                                .Height = 12
                                .Font.Size = 9
                                .Font.Name = "Tahoma"
                                .FontBold = True
                                '.BackColor = &H80FFFF
                            End With
                        
                           With snlbl
                                .Name = "FieldLabel"
                                .Caption = "Serial Number"
                                .Top = 45 + (45 * t)
                                .Left = 537
                                .Width = 50
                                .Height = 12
                                .Font.Size = 9
                                .Font.Name = "Tahoma"
                                .FontBold = True
                           End With
                           
                           With stlbl
                                .Name = "FieldLabel"
                                .Caption = "Status"
                                .Top = 45 + (45 * t)
                                .Left = 613
                                .Width = 50
                                .Height = 12
                                .Font.Size = 9
                                .Font.Name = "Tahoma"
                                .FontBold = True
                           End With
                    
                    Next
                 End If

End Sub
 
Postado : 08/11/2019 6:42 am
(@faraha)
Posts: 0
New Member
 

Bom dia Filoap,

Não entendi muito bem a sua dúvida, mas vamos lá, passo a passo o que você está fazendo:

1 - Você está declarando uma variável para o objeto do tipo "controle";
2 - Depois você adiciona ao seu formulário no "frame1" este objeto;
3 - Creio que esteja adicionando as propriedades de cada controle um a um, pois por algum motivo quer que a propriedade de cada controle não seja igual ao dos outros controles.

Como você está fazendo na unha cada controle, para localizar um controle você tem duas opções:
1 - Ao definir a propriedades de cada controle, Exemplo:
Para os controles do tipo label você está definindo um nome "igual" para todos:

 With prodlbl
           .Name = "FieldLabel"
           <Mais propriedades>
End with

Poderia adicionar um nome diferente para cada um, já que está fazendo na "unha". Talvez ficasse mais fácil para encontrar o valor dentre as labels.
Mas não vi você definindo um nome para todos os controles terminados em "txt"

2 - Sendo que a forma acima não é a melhor forma,
Se você declara uma variável "dataTXT" para especificamente utilizar como campo de data, para manipulá-la basta usar o With deste controle.
Exemplo:

With dataTXT
          .name = "Escolha_um_nome"
End with

Caso a dúvida seja para encontrar o objeto ciclando entre todos, segue exemplo:

Dim Controles as control
For each Controles in frame1.controls
      if controles.name = "Escolha_um_nome" then
           <Faça o que quiser>
      end if
Next Controles

Caso isto não lhe dê uma direção, peço que compartilhe um pouco melhor a sua dúvida, dê algum exemplo mais concreto do que deseja realizar. Forte abraço.

 
Postado : 08/11/2019 9:03 am
(@filoap)
Posts: 0
New Member
Topic starter
 

Ola FarahA, obrigado pelas dicas, deixe me tentar explicar um pouco o que estou criando:

1 - Um userform onde o usuário pode incluir várias linhas (cada linha consta 8 textbox e 1 combobox), cada Txt tem uma função, como item, valor, data, Serie entre outros.

2 - O Usuário preenche a quantidade de linhas que deseja e com isso após preencher as linhas eu salvo em uma base.

3 - O problema que venho enfrentando é... quando utilizo outro botão (SALVAR), não consigo trazer as informações das linhas criadas dinamicamente. Mesmo inserindo o .name = "TXT"

4 - Abaixo o código que utilizo para salvar cada CBBox e TextBox que criei, sendo a linha com o codigo em amarelo cells(i+j,16)= PN_TXT o nome do meu primeiro textbox inserido dinamicamente.

Muito obrigado desde já

Private Sub GravaEimp_Click()
    
'Dim cont As Integer
Dim i As Integer
Dim wkbOrigem As Workbook
Dim wksOrigem As Worksheet
Dim result As VbMsgBoxResult
Dim j As Integer
Dim v As Integer

Dim PN_TXT As TextBox


        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
        
            
                  
        If QITtxt = "" Then
            MsgBox "Preencha a quantidade de itens de acordo com a Proposta/OC cliente", vbExclamation, "ATENÇÃO - FALTA DE PREENCHIMENTO"
            QITtxt.SetFocus
            Else
            If CBCOU = "" Then
                MsgBox "Preencha o campo país", vbExclamation, "ATENÇÃO - FALTA DE PREENCHIMENTO"
                CBCOU.SetFocus
                Else
                If CBSALES = "" Then
                    MsgBox "Preencha o tipo de venda, Direta/Indireta", vbExclamation, "ATENÇÃO - FALTA DE PREENCHIMENTO"
                    CBSALES.SetFocus
                    Else
                    If CBDI = "" Then
                        MsgBox "Preencha o tipo de CLIENTE, PUBLICO/PRIVADO", vbExclamation, "ATENÇÃO - FALTA DE PREENCHIMENTO"
                        CBSALES.SetFocus
                        Else
                        If SFDCtxt = "" Then
                            MsgBox "Preencha o número da OPP do Sales Force", vbExclamation, "ATENÇÃO - FALTA DE PREENCHIMENTO"
                            SFDCtxt.SetFocus
                            Else
                            If DTRectxt = "" Then
                                MsgBox "Preencha a data de recebimento do Pedido do Cliente", vbExclamation, "ATENÇÃO - FALTA DE PREENCHIMENTO"
                                DTRectxt.SetFocus
                                Else
                                If CBCN = "" Then
                                    MsgBox "Preencha o nome do cliente", vbExclamation, "ATENÇÃO - FALTA DE PREENCHIMENTO"
                                    CBCN.SetFocus
                                    Else
                                    If CBINCO = "" Then
                                        MsgBox "Preencha o Incoterm de acordo com a Proposta", vbExclamation, "ATENÇÃO - FALTA DE PREENCHIMENTO"
                                        CBINCO.SetFocus
                                        Else
                                        If custpotxt = "" Then
                                            MsgBox "Preencha a Orden de compra de acordo com a Proposta", vbExclamation, "ATENÇÃO - FALTA DE PREENCHIMENTO"
                                            custpotxt.SetFocus
                                           Else
                                           If CBPTER = "" Then
                                               MsgBox "Preencha a condição de pagamento de acordo com a Proposta", vbExclamation, "ATENÇÃO - FALTA DE PREENCHIMENTO"
                                               CBPTER.SetFocus
                                                Else
                                                If contemailtxt = "" Then
                                                    MsgBox "Preencha a contato do cliente", vbExclamation, "ATENÇÃO - FALTA DE PREENCHIMENTO"
                                                    contemailtxt.SetFocus
                                                   Else
                                                   If contnametxt = "" Then
                                                        MsgBox "Preencha o nome do cliente", vbExclamation, "ATENÇÃO - FALTA DE PREENCHIMENTO"
                                                        contemailtxt.SetFocus
                                                        Else
                                                        If Me.CBSALES.Text = "INDIRECT" And Me.CBINCO.Text = "EXCEPTION" And Me.Exceptxt.Text = "" Then
                                                            MsgBox "É necessário ter uma razão para ter uma exeção, preencher motivo.", vbExclamation, "ATENÇÃO - FALTA DE PREENCHIMENTO"
                                                            Exceptxt.SetFocus
                                                            Else
                                               
                                                            Set wkbOrigem = Workbooks.Open("\ame-it-srv01DeptosSales_Operations5 - Base de bados SOPDBBANCO DE DADOS SALES OPS ITRON.xlsx")
                                                            Set wksOrigem = wkbOrigem.Worksheets("DB")
                                                           
                                                            
                                                            
                                                            'Verificando a quantidade de linhas usadas na planilha
                                                            i = Range("A" & Rows.Count).End(xlUp).Row + 1
                                                            
                                                            'Mensagem de gravação concluída
                                                            result = MsgBox("Are you sure you want to save?", vbYesNo + vbQuestion)
                                                            
                                                                If result = vbYes Then
                                                                                                                                    
                                                                    For j = 1 To QITtxt
                                                                        'Selecionando a planilha a ser usada como banco de dados
                                                                        Worksheets("DB").Select 'Armazenando na planilha os dados inseridos pelo usuário
                                                                                    Cells(i + j, 1) = i & "-" & j
                                                                                    Cells(i + j, 2) = CBCOU
                                                                                    Cells(i + j, 3) = CBSALES
                                                                                    Cells(i + j, 4) = CBDI
                                                                                    Cells(i + j, 5) = SFDCtxt
                                                                                    Cells(i + j, 7) = CBCN
                                                                                    Cells(i + j, 8) = CusIDtxt
                                                                                    Cells(i + j, 9) = custpotxt
                                                                                    Cells(i + j, 10) = CBINCO
                                                                                    Cells(i + j, 11) = CBPTER
                                                                                    Cells(i + j, 12) = DTRectxt
                                                                                    Cells(i + j, 22) = oftxt
                                                                                    Cells(i + j, 44) = contemailtxt
                                                                                    Cells(i + j, 45) = contnametxt
                                                                                    Cells(i + j, 34) = CBIOF
                                                                                    Cells(i + j, 35) = CBPD
                                                                                    Cells(i + j, 37) = CBFDT
                                                                                    Cells(i + j, 27) = CBFAC
                                                                                    Cells(i + j, 24) = CBWAR
                                                                                    Cells(i + j, 25) = CBPEN
                                                                                    Cells(i + j, 32) = CBNAM
                                                                                    Cells(i + j, 33) = nampltxt
                                                                                    Cells(i + j, 30) = CBSNA
                                                                                  [color=#FFFF40]  Cells(i + j, 16) = PN_TXT & j[/color]
                                                                                    
                                                                    Next
                                                                        Call apagaLinha
                                                                        ActiveCell.Offset(1, 0).Select
                                                                        MsgBox "Informações foram salvas com sucesso", vbInformation
                                                                    Else
                                                                        MsgBox "Informações não foram salvas", vbInformation
                                                                    
                                                                End If
                                                                'Comando para salvar e fechar planilha
                                                                ActiveWorkbook.Save
                                                                Workbooks("BANCO DE DADOS SALES OPS ITRON.xlsx").Close SaveChanges:=True
                                                            End If
                                                        End If
                                                   End If
                                                End If
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If
            
        With Application
            .ScreenUpdating = True
            .DisplayAlerts = True
        End With
   
   End With
   
  
   
   'Limpando os campos após o cadastro
   'CBCOU = ""
   'CBSALES = ""
   'CBDI = ""
   'txt_cpf = ""
   'txt_nascime = ""
   'txt_ender = ""
   'txt_numer = ""
   'txt_bairro = ""
   'txt_muni = ""
   'txt_comple = ""
   'txt_telef = ""
   'txt_cel = ""
   'txt_email = ""
   'txt_cad_em = ""
   'ComboBoxOperador = ""
   'obs = ""
        
    'Call LimpaControles

End Sub

Não consegui anexar meu projeto na conversa

 
Postado : 11/11/2019 7:51 am