Perform action for each line in an SQL search - VB

Asked

Viewed 102 times

2

I wonder if anyone can help me with the following question.

In a VB code, I want to perform the search for products in an SQL table. For each product found I would like to add a button in a panel.

If this is not possible, I can "customize" a datagrid(?) like the example below?

inserir a descrição da imagem aqui

Can someone help me? Thank you in advance for your attention.

1 answer

0


There is a VBA solution that I adapted below and that can be adapted also for your case, the reference comes from a question I asked and was answered by Luiz Vieira, see the link below.

How to create events dynamically in VBA/Excel

The idea is to dynamically create a button for each image or position in the grid, which will depend on how many results you will find in each search.

First create a class module that I named Dynamiccommandbutton (the codes below are made for Excel VBA)

Public WithEvents CommandButtonClick As msforms.CommandButton
'Declaração da propriedade/atributo da classe

Private Sub CommandButtonClick_Click()
'Será a propriedade/atributo de cada CommandButton criado
'Neste caso está associado ao evento Click

    ActionCommanButton CommandButtonClick.Name
    'Procedure que deseja executar para o botão acionado

End Sub

Private Sub ActionCommanButton(ButtonName As String)

    Beep

    MsgBox "Você clicou no Botão " & ButtonName & "!"

End Sub

Put it on the form like this:

Dim CommandButtons() As New DynamicCommandButton
'O módulo de Classe que contém o evento Click para o CommadButton

Private Sub CreateCommandButtons(ByVal NumberOfCommandButtons As Integer)
'Esta procedure cria quantos botões desejar

    Dim i As Integer

    Dim NewButton As Control

    ReDim CommandButtons(0 To NumberOfCommandButtons - 1)

    For i = 0 To NumberOfCommandButtons - 1

        Set NewButton = Me.Controls.Add( _
          "Forms.CommandButton.1", _
          "NewButton" & i)

        With NewButton
          .Caption = "NewButton" & i
          .Top = 50 * i + 30
          .Left = 30
        End With

        Set CommandButtons(i).CommandButtonClick = NewButton

    Next i

End Sub

Private Sub UserForm_Activate()

    CreateCommandButtons 4

End Sub

Observing: you must adapt this precedent to pass the position X and Y of each button you want to create by working on 'grid', here in case it creates a button below the other. In this example I requested 4 buttons to be created.

This is the result:

Resultado

Therefore, it is enough to have the X and Y coordinates of each position to place the button and adapt the routine to create them in these positions.

  • Thank you Leo! Sorry for the delay, some unforeseen events prevented me from answering, pardon. Your code helped me and I managed to accomplish what I needed thanks to your help, thank you!

  • You’re welcome, buddy, great that I helped you! You are a beginner here at SOPT, read Help, see that you can vote on questions and answers, this helps the community to assess their importance.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.