Testo concatenato da più checkbox

di il
5 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.

5 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...

Devi accedere o registrarti per scrivere nel forum
5 risposte