Take action for each row in a SQL - VB search

1

I would like to know 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 to a panel.

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

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

    
asked by anonymous 05.01.2017 / 06:03

1 answer

0

There is a solution in VBA that I adapted below and that can be adapted 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 on 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 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

On the form put it :

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

Note: You should adapt this procedure to pass the X and Y position of each button you want to create by working on 'grid', here it creates a button below the other. In this example I asked for 4 buttons to be created.

This is the result :

So just have the X and Y coordinates of each position to put the button and adapt the routine to create them in these positions.

    
05.01.2017 / 16:28