Testo concatenato da più checkbox

di il
31 risposte

Testo concatenato da più checkbox

Nella mia maschera ho messo 4 caselle di controllo; ChkAllegatoA, ChkAllegatoB, ChkAllegatoC, ChkAllegatoD.

Mi piacerebbe che in base a quelle selezionate, sulla casella di testo "TxtAllegati" venga composta una frase in cui vengono elencati, separati da una virgola e per l'ultima invece che sia preceduta dalla congiunzione "e ".
Ad esempio se sono tutte selezionate, nella casella di testo viene riportato il testo:

Istanza, Allegato A, Allegato B, Allegato C e Allegato D

mentre se seleziono solo ChkAllegatoA e ChkAllegatoC, viene riportato:

Istanza, Allegato A e Allegato C

"Istanza" è un testo fisso che precede tutta la composizione, ovviamente se nessuna casella è selezionata non viene seguita da nessuna virgola.

31 Risposte

  • Re: Testo concatenato da più checkbox

    Su Evento AfterUpdate di tutte e 4 le CheckBox, richiami una Funzione(non si può usare una Sub in questo caso) unica nella Maschera che va a valorizzare una Label, non serve una TextBox in questo caso...!

    Quindi inserisci una LABEL chiamata [ckLabel]

    Nell'evento AfterUpdate delle CheckBox scriverai, esattamente come te lo scrivo sotto con anche [=] iniziale...:

    =CheckAttachments()

    La stessa cosa la devi richiamare su Current di Maschera

    Private Sub Form_Current()
    	CheckAttachments()
    End Sub

    La funzione invece la puoi scrivere così, poi se non hai bisogno di discriminare le 4 casistiche semplificalo):

    Public Function CheckAttachments()
    	Dim strLabel As String
    	If Me!ChkAllegatoA.Value Then strLabel = strLabel & "Allegato A e " & 
    	If Me!ChkAllegatoB.Value Then strLabel = strLabel & "Allegato B e " & 
    	If Me!ChkAllegatoC.Value Then strLabel = strLabel & "Allegato C e " & 
    	If Me!ChkAllegatoD.Value Then strLabel = strLabel & "Allegato D e " & 
    	If Len(strLabel)>0 Then strLabel=Mid$(strLabel,1,Len(strLabel-3))
    	Me!ckLabel.Caption=strLabel 
    End Function
  • Re: Testo concatenato da più checkbox

    Colgo l'occasione per ringraziare te e tutti i membri di questo forum sempre pronti a dare una mano a noi principianti, condividendo le proprie conoscenze per la comunità e credimi non è da tutti.

    Mi dà un errore di comunicazione con il server OLE o con il controllo ActiveX: l'espressione non corrisponde al nome di una Routine Evento ....

    nel codice mi segna in rosso ed andandolo a riscriverlo mi dice: Errore di compilazione: Previsto numero di riga oppure etichetta oppure istruzione oppure fine istruzione.

    Cosa sbaglio:

    Quindi inserisci una LABEL chiamata [ckLabel]

    credo intendessi strLabel , giusto?

    La stessa cosa la devi richiamare su Current di Maschera

    Private Sub Form_Current()
    	CheckAttachments()
    End Sub

    CheckAttachments() sempre preceduto dal simbolo "=", o volutamente così?

    La funzione invece la puoi scrivere così, poi se non hai bisogno di discriminare le 4 casistiche semplificalo):

    Public Function CheckAttachments()
    	Dim strLabel As String
    	If Me!ChkAllegatoA.Value Then strLabel = strLabel & "Allegato A e " & 
    	If Me!ChkAllegatoB.Value Then strLabel = strLabel & "Allegato B e " & 
    	If Me!ChkAllegatoC.Value Then strLabel = strLabel & "Allegato C e " & 
    	If Me!ChkAllegatoD.Value Then strLabel = strLabel & "Allegato D e " & 
    	If Len(strLabel)>0 Then strLabel=Mid$(strLabel,1,Len(strLabel-3))
    	Me!ckLabel.Caption=strLabel 
    End Function

    Questa funzione devo scriverla, in qualche evento particolare o in qualsiasi posto in quanto indipendente anche di seguito all'inserimento in Current di Maschera?

  • Re: Testo concatenato da più checkbox

    14/10/2025 - Azucarea ha scritto:

    Colgo l'occasione per ringraziare te e tutti i membri di questo forum sempre pronti a dare una mano a noi principianti, condividendo le proprie conoscenze per la comunità e credimi non è da tutti.

    Mi dà un errore di comunicazione con il server OLE o con il controllo ActiveX: l'espressione non corrisponde al nome di una Routine Evento ....

    nel codice mi segna in rosso ed andandolo a riscriverlo mi dice: Errore di compilazione: Previsto numero di riga oppure etichetta oppure istruzione oppure fine istruzione.

    Cosa sbaglio:

    Quindi inserisci una LABEL chiamata [ckLabel]

    credo intendessi strLabel , giusto?

    Sbagliato intendevo un oggetto LABEL con quel nome, strLabel è una variabile stringa usata nella Funzione non ha nulla a che vedere.

    La stessa cosa la devi richiamare su Current di Maschera

    Private Sub Form_Current()
    	CheckAttachments()
    End Sub

    CheckAttachments() sempre preceduto dal simbolo "=", o volutamente così?

    Scusa quì ho commesso un errore... la sintassi giusta è questa senza le parentesi:

    Private Sub Form_Current()
    	CheckAttachments
    End Sub

    La funzione invece la puoi scrivere così, poi se non hai bisogno di discriminare le 4 casistiche semplificalo):

    Public Function CheckAttachments()
    	Dim strLabel As String
    	If Me!ChkAllegatoA.Value Then strLabel = strLabel & "Allegato A e " 
    	If Me!ChkAllegatoB.Value Then strLabel = strLabel & "Allegato B e "
    	If Me!ChkAllegatoC.Value Then strLabel = strLabel & "Allegato C e "
    	If Me!ChkAllegatoD.Value Then strLabel = strLabel & "Allegato D e " 
    	If Len(strLabel)>0 Then strLabel=Mid$(strLabel,1,Len(strLabel-3))
    	Me!ckLabel.Caption=strLabel 
    End Function

    Questa funzione devo scriverla, in qualche evento particolare o in qualsiasi posto in quanto indipendente anche di seguito all'inserimento in Current di Maschera?

    La devi inserire nel Modulo della Maschera...

  • Re: Testo concatenato da più checkbox

    @Alex la function non andrebbe richiamata con :

    Private Sub Form_Current()
    	Call CheckAttachments
    End Sub

    ??

    Poi, se ho capito bene cio' che chiede Azucarea, vorrebbe scritto ad esempio: Istanza, Allegato A, Allegato B, Allegato C e Allegato D (se tutte le check sono spuntate), con la "e" solo sull'ultima check,

    oppure: Istanza, Allegato B e Allegato D

    oppure: Istanza, Allegato C

    La tua function invece scrive: Allegato A e Allegato B e Allegato C e Allegato D (comunque con le "e" dopo ogni allegato)...

    Secondo me andrebbero create le concatenazioni  di stringhe in funzione delle varie combinazioni con una select Case...

    Sbaglio?

  • Re: Testo concatenato da più checkbox

    Io ho testato questa function e funziona:

    Public Function CheckAttachments()
    On Error GoTo Err_handler
    
    Dim strLabel As String
        strLabel = "Istanza, "
        Select Case True
            Case Me!ChkAllegatoA And Not Me!ChkAllegatoB And Not Me!ChkAllegatoC And Not Me!ChkAllegatoD
                strLabel = strLabel & "Allegato A"
            Case Me!ChkAllegatoA And Me!ChkAllegatoB And Not Me!ChkAllegatoC And Not Me!ChkAllegatoD
                strLabel = strLabel & "Allegato A e Allegato B"
            Case Me!ChkAllegatoA And Me!ChkAllegatoB And Me!ChkAllegatoC And Not Me!ChkAllegatoD
                strLabel = strLabel & "Allegato A, Allegato B e Allegato C"
            Case Me!ChkAllegatoA And Me!ChkAllegatoB And Not Me!ChkAllegatoC And Me!ChkAllegatoD
                strLabel = strLabel & "Allegato A, Allegato B e Allegato D"   
            Case Me!ChkAllegatoA And Me!ChkAllegatoB And Me!ChkAllegatoC And Me!ChkAllegatoD
                strLabel = strLabel & "Allegato A, Allegato B, Allegato C e Allegato D"
            Case Me!ChkAllegatoA And Not Me!ChkAllegatoB And Me!ChkAllegatoC And Not Me!ChkAllegatoD
                strLabel = strLabel & "Allegato A e Allegato C"
            Case Me!ChkAllegatoA And Not Me!ChkAllegatoB And Me!ChkAllegatoC And Me!ChkAllegatoD
                strLabel = strLabel & "Allegato A, Allegato C e Allegato D"
            Case Me!ChkAllegatoA And Not Me!ChkAllegatoB And Not Me!ChkAllegatoC And Me!ChkAllegatoD
                strLabel = strLabel & "Allegato A e Allegato D"
            Case Not Me!ChkAllegatoA And Me!ChkAllegatoB And Not Me!ChkAllegatoC And Not Me!ChkAllegatoD
                strLabel = strLabel & "Allegato B"
            Case Not Me!ChkAllegatoA And Me!ChkAllegatoB And Me!ChkAllegatoC And Not Me!ChkAllegatoD
                strLabel = strLabel & "Allegato B e Allegato C"
            Case Not Me!ChkAllegatoA And Me!ChkAllegatoB And Me!ChkAllegatoC And Me!ChkAllegatoD
                strLabel = strLabel & "Allegato B, Allegato C e Allegato D"
            Case Not Me!ChkAllegatoA And Me!ChkAllegatoB And Not Me!ChkAllegatoC And Me!ChkAllegatoD
                strLabel = strLabel & "Allegato B e Allegato D"
            Case Not Me!ChkAllegatoA And Not Me!ChkAllegatoB And Me!ChkAllegatoC And Not Me!ChkAllegatoD
                strLabel = strLabel & "Allegato C"
            Case Not Me!ChkAllegatoA And Not Me!ChkAllegatoB And Me!ChkAllegatoC And Me!ChkAllegatoD
                strLabel = strLabel & "Allegato C e Allegato D"
            Case Not Me!ChkAllegatoA And Not Me!ChkAllegatoB And Not Me!ChkAllegatoC And Me!ChkAllegatoD
                strLabel = strLabel & "Allegato D"
                    
        End Select
        Me!ckLabel.Caption = strLabel
        
    Exit_Err_handler:
        Exit Function
        
    Err_handler:
        MsgBox Err.Number & " " & Err.Description
        Resume Exit_Err_handler
    End Function
    

    Premessa: nelle quattro checkbox  imposta valore predefinito a zero e State Triplo a NO.

    La function la chiami come ti ho scritto sopra (con Call) e la metti negli eventi che Ti ha gia'  spiegato @Alex

    Quando inserisci la Label nella form devi obbligatoriamente metterci del testo all'interno: mettigli una virgola, un punto e virgola o una lettera, non ha importanza, tanto poi viene modificato dalla function...

    Fai sapere...

  • Re: Testo concatenato da più checkbox

    14/10/2025 - Mailman ha scritto:

    @Alex la function non andrebbe richiamata con :

    Private Sub Form_Current()
    	Call CheckAttachments
    End Sub

    ??

    Non è indispensabile usare CALL... prova come ho scritto....

    Poi, se ho capito bene cio' che chiede Azucarea, vorrebbe scritto ad esempio: Istanza, Allegato A, Allegato B, Allegato C e Allegato D (se tutte le check sono spuntate), con la "e" solo sull'ultima check,

    oppure: Istanza, Allegato B e Allegato D

    oppure: Istanza, Allegato C

    La tua function invece scrive: Allegato A e Allegato B e Allegato C e Allegato D (comunque con le "e" dopo ogni allegato)...

    Secondo me andrebbero create le concatenazioni  di stringhe in funzione delle varie combinazioni con una select Case...

    Sbaglio?

    Non avevo interpretato ma se è corretta la tua interpretazione... preferirei questa cosa decisamente meno contorta... e mi sono accorto che nel codice precedente ho introdotto molti errori...

    Public Function CheckAttachments(b1 As Boolean, b2 As Boolean, b3 As Boolean, b4 As Boolean)
        Dim strLabel    As String
        Dim varLabels   As Variant
        Dim intCount    As Integer
        If b1 Then strLabel = strLabel & "Allegato A,"
        If b2 Then strLabel = strLabel & "Allegato B,"
        If b3 Then strLabel = strLabel & "Allegato C,"
        If b4 Then strLabel = strLabel & "Allegato D,"
        If Len(strLabel) > 0 Then
            strLabel = Mid$(strLabel, 1, Len(strLabel) - 1)
            varLabels = Split(strLabel, ",")
            strLabel = vbNullString
            For intCount = 0 To UBound(varLabels) - 1
                If intCount < UBound(varLabels) - 1 Then
                    strLabel = strLabel & varLabels(intCount) & ", "
                Else
                    strLabel = strLabel & varLabels(intCount) & " e " & varLabels(intCount + 1)
                End If
            Next
            Debug.Print strLabel
        End If
    End Function
  • Re: Testo concatenato da più checkbox

    Il mio approccio e' molto piu' terra-terra: sviluppando le 15 combinazioni possibili, per ognuna avevo predisposto la stringa corretta.

    La tua soluzione e' decisamente molto piu' snella: una lezione molto avanzata di manipolazione delle stringhe.

    Grazie per la lezione!!

    PS Per la Call avevo testato inizialmente con "=" come nel tuo primo suggerimento e dava errore...

  • Re: Testo concatenato da più checkbox

    14/10/2025 - @Alex ha scritto:

    Nell'evento AfterUpdate delle CheckBox scriverai, esattamente come te lo scrivo sotto con anche [=] iniziale...:

    =CheckAttachments()

    Risolto per il Current di Maschera, ma l'evento AfterUpdate delle CheckBox continua a darmi errore.

     di sintassi, io ho scritto questo:

    Private Sub ChkAllegatoA_AfterUpdate()
    =CheckAttachments()
    End Sub

    14/10/2025 - @Alex ha scritto:

    14/10/2025 - Azucarea ha scritto:

    Quindi inserisci una LABEL chiamata [ckLabel]

    credo intendessi strLabel , giusto?

    Sbagliato intendevo un oggetto LABEL con quel nome, strLabel è una variabile stringa usata nella Funzione non ha nulla a che vedere.

    Nella funzione non vedo nessun riferimento a questa Label, però. Come fa ad apparire la scritta nel controllo ckLabel

  • Re: Testo concatenato da più checkbox

    @Alex: Ho testato la Tua soluzione ed ho apportato  qualche ritocco per andare incontro a quanto ho inteso della richiesta di Azucarea:

    Public Function CheckAttachments(b1 As Boolean, b2 As Boolean, b3 As Boolean, b4 As Boolean)
    On Error GoTo Err_handler
    
    Dim varLabels   As Variant
    Dim intCount    As Integer
    Dim strLabel As String
        
        strLabel = "Istanza,"
        If b1 Then strLabel = strLabel & "Allegato A,"
        If b2 Then strLabel = strLabel & "Allegato B,"
        If b3 Then strLabel = strLabel & "Allegato C,"
        If b4 Then strLabel = strLabel & "Allegato D,"
        If Len(strLabel) > 8 Then
            strLabel = Mid$(strLabel, 1, Len(strLabel) - 1)
            varLabels = Split(strLabel, ",")
            strLabel = vbNullString
            For intCount = 0 To UBound(varLabels) - 1
                If intCount < UBound(varLabels) - 1 Then
                    strLabel = strLabel & varLabels(intCount) & ", "
                Else
                    strLabel = strLabel & varLabels(intCount) & " e " & varLabels(intCount + 1)
                End If
            Next
            'Debug.Print strLabel
        End If
        Me!ckLabel.Caption = strLabel
        
    Exit_Err_handler:
        Exit Function
        
    Err_handler:
        MsgBox Err.Number & " " & Err.Description
        Resume Exit_Err_handler
    End Function
    

    In pratica ho inteso che se nessuna check box viene selezionata, nella label deve comparire la scritta Istanza. In questo modo funziona

    La chiamata della function:

    Private Sub ChkAllegatoA_AfterUpdate()
    On Error GoTo Err_handler
        
        CheckAttachments ChkAllegatoA, ChkAllegatoB, ChkAllegatoC, ChkAllegatoD
    
    Exit_Err_handler:
        Exit Sub
    
    Err_handler:
        MsgBox Err.Number & " " & Err.Description
        Resume Exit_Err_handler
        
    End Sub
  • Re: Testo concatenato da più checkbox

    14/10/2025 - Mailman ha scritto:

    @Alex: Ho testato la Tua soluzione ed ho apportato  qualche ritocco per andare incontro a quanto ho inteso della richiesta di Azucarea:

    Public Function CheckAttachments(b1 As Boolean, b2 As Boolean, b3 As Boolean, b4 As Boolean)
    On Error GoTo Err_handler
    
    Dim varLabels   As Variant
    Dim intCount    As Integer
    Dim strLabel As String
        
        strLabel = "Istanza,"
        If b1 Then strLabel = strLabel & "Allegato A,"
        If b2 Then strLabel = strLabel & "Allegato B,"
        If b3 Then strLabel = strLabel & "Allegato C,"
        If b4 Then strLabel = strLabel & "Allegato D,"
        If Len(strLabel) > 8 Then
            strLabel = Mid$(strLabel, 1, Len(strLabel) - 1)
            varLabels = Split(strLabel, ",")
            strLabel = vbNullString
            For intCount = 0 To UBound(varLabels) - 1
                If intCount < UBound(varLabels) - 1 Then
                    strLabel = strLabel & varLabels(intCount) & ", "
                Else
                    strLabel = strLabel & varLabels(intCount) & " e " & varLabels(intCount + 1)
                End If
            Next
            'Debug.Print strLabel
        End If
        Me!ckLabel.Caption = strLabel
        
    Exit_Err_handler:
        Exit Function
        
    Err_handler:
        MsgBox Err.Number & " " & Err.Description
        Resume Exit_Err_handler
    End Function
    

    In pratica ho inteso che se nessuna check box viene selezionata, nella label deve comparire la scritta Istanza. In questo modo funziona

    La chiamata della function:

    Private Sub ChkAllegatoA_AfterUpdate()
    On Error GoTo Err_handler
        
        CheckAttachments ChkAllegatoA, ChkAllegatoB, ChkAllegatoC, ChkAllegatoD
    
    Exit_Err_handler:
        Exit Sub
    
    Err_handler:
        MsgBox Err.Number & " " & Err.Description
        Resume Exit_Err_handler
        
    End Sub

    Funziona! L'unica cosa che non va è che se non è selezionato nulla rimane la virgola dopo "Istanza" (Istanza,)

  • Re: Testo concatenato da più checkbox

    Ma no non va bene... il mio era un TEST per provarla da riga di comando in finestra immediata senza farmi una Form ma la struttura era quella di prima in quanto così non la puoi richiamare direttamente su Evento AfterUpdate... 

    Deve essere una Function senza parametri:

    Public Function CheckAttachments()
    On Error GoTo Err_handler
    
    Dim varLabels   As Variant
    Dim intCount    As Integer
    Dim strLabel As String
        
        If Me!ChkAllegatoA.Value Then strLabel = strLabel & "Allegato A,"
        If Me!ChkAllegatoB.Value Then strLabel = strLabel & "Allegato B,"
        If Me!ChkAllegatoC.Value Then strLabel = strLabel & "Allegato C,"
        If Me!ChkAllegatoD.Value Then strLabel = strLabel & "Allegato D,"
        If Len(strLabel) > 8 Then
            strLabel = Mid$(strLabel, 1, Len(strLabel) - 1)
            varLabels = Split(strLabel, ",")
            strLabel = vbNullString
            For intCount = 0 To UBound(varLabels) - 1
                If intCount < UBound(varLabels) - 1 Then
                    strLabel = strLabel & varLabels(intCount) & ", "
                Else
                    strLabel = strLabel & varLabels(intCount) & " e " & varLabels(intCount + 1)
                End If
            Next
        Else
        	strLabel = "Istanza"
        End If
        Me!ckLabel.Caption = strLabel
        
    Exit_Err_handler:
        Exit Function
        
    Err_handler:
        MsgBox Err.Number & " " & Err.Description
        Resume Exit_Err_handler
    End Function

    Così funziona e la si richiama su Current come ho indicato e su AfterUpdate come ho indicato.

  • Re: Testo concatenato da più checkbox

    Codice più scarno, ma 2 cose non vanno:

    1. La scritta "Istanza" deve comparire sempre seguita da una virgola se si seleziona almeno una checkbox, se non se ne selezionano va bene così funziona adesso (ovvero solo la scritta "Istanza" senza la virgola seguente)
    2. Se si seleziona solo una casella non appare nulla, invece dovrebbe apparire la scritta "Istanza e " e la scritta in base alla selezione tipo: "Istanza e Allegato A"
  • Re: Testo concatenato da più checkbox

    Prova cosi', Io l'ho testata in una form e funziona:

    Public Function CheckAttachments()
    On Error GoTo Err_handler
    
    Dim varLabels   As Variant
    Dim intCount    As Integer
    Dim strLabel As String
        
        strLabel = "Istanza,"
        If Me!ChkAllegatoA.Value Then strLabel = strLabel & "Allegato A,"
        If Me!ChkAllegatoB.Value Then strLabel = strLabel & "Allegato B,"
        If Me!ChkAllegatoC.Value Then strLabel = strLabel & "Allegato C,"
        If Me!ChkAllegatoD.Value Then strLabel = strLabel & "Allegato D,"
        If Len(strLabel) > 8 Then
            strLabel = Mid$(strLabel, 1, Len(strLabel) - 1)
            varLabels = Split(strLabel, ",")
            strLabel = vbNullString
            For intCount = 0 To UBound(varLabels) - 1
                If intCount < UBound(varLabels) - 1 Then
                    strLabel = strLabel & varLabels(intCount) & ", "
                Else
                    strLabel = strLabel & varLabels(intCount) & " e " & varLabels(intCount + 1)
                End If
            Next
        Else
            strLabel = Mid$(strLabel, 1, Len(strLabel) - 1)
        End If
           
        Me!ckLabel.Caption = strLabel
        
    Exit_Err_handler:
        Exit Function
        
    Err_handler:
        MsgBox Err.Number & " " & Err.Description
        Resume Exit_Err_handler
    End Function
    
  • Re: Testo concatenato da più checkbox

    14/10/2025 - @Alex ha scritto:

    Ma no non va bene... il mio era un TEST per provarla da riga di comando in finestra immediata senza farmi una Form ma la struttura era quella di prima in quanto così non la puoi richiamare direttamente su Evento AfterUpdate... 

    Deve essere una Function senza parametri:

    Anche con i parametri la si puo' richiamare sugli eventi AfterUpdate e Current, perche' dici che non si puo' fare? Io l'ho testata in una form e funziona correttamente.

    Pensavo che Tu l'avessi resa parametrica per poterla salvare su un modulo standard e poterla richiamare da qualsiasi form; perche' salvata nel modulo della form non serve renderla Public, poteva essere anche Private...

    Sbaglio?

Devi accedere o registrarti per scrivere nel forum
31 risposte