Buona sera a tutti, ho bisogno di chiedervi un piacere su un problema che chiaramente non so risolvere.
Tempo fa un amico ha realizzato un database per la gestione dell'ufficio dove lavoro. Quindi il codice che ho postato non l'ho scritto io, anche se qualcosina lo mastico.
Arrivo al problema:
Ho una tabella chiamata "elenco_speciale" con vari campi. Questa tabella racchiude una serie di nomi di uffici con i vari numeri di telefono, indirizzi e-mail, ecc....
Poi c'è una maschera legata a questa tabella. Infine su una maschera principale ho una serie di pulsanti legati a questa maschera. Ovvero, in base al pulsante che premo, la maschera si adatta. Cioè fa un confronta tra il CAPITOLO del pulsante e il nome dell'ufficio presente in tabella. Faccio un esempio: 
Se clicco sul pulsante UFFICI DI MILANO la maschera mostra solo i numeri di milano, la sessa cosa avviene se clicco sul pulsante NUMERI VARI, cioè mi verranno mostrati solo i numeri vari.
Non solo addirittura vengono modificati i colori di fondo e l'inibizione o meno di alcuni pulsanti presenti in maschera.
Cmq il mio problema è che ho la necessità di far in modo che in base al pulsante che clicco, la maschera filtri in ordine alfabetico i campi che a me interessano. Ovvero per esempio se clicco sul pulsante NUMERI DI MILANO vorrei che venissero mostrati i NUMERI DI MILANO in ordine alfabetico in base al campo 2, mentre se clicco sul pulsante NUMERI VARI vorrei che venissero mostrati i NUMERI VARI in ordine alfabetico in base al campo 3, e così via. Cioè ci vorrebbe un codice che poi vado io a modificare.
Non saprei né come scriverlo né tanto meno dove scriverlo.
Di seguito vi posto il codice relativo alla maschera.
Spero che sia stato esaustivo e che il problema sia risolvibile....grazie
Option Explicit
Option Compare Database
Private elenco_speciale As Integer
Private active_field As Control
'
Private Sub Form_Activate()
DoCmd.Maximize
End Sub
Private Sub Form_Open(Cancel As Integer)
Dim s As String, t As String, i As Single
    'tappo di sicurezza
    
    elenco_speciale = Nz(OpenArgs, 0)
    If elenco_speciale = 0 Then
        MsgBox "Cannot open this form directly.", vbCritical                '<<< per test: commentare
        Cancel = True                                                       '<<< per test: commentare
        Exit Sub                                                            '<<< per test: commentare
        'elenco_speciale = InputBox("Inserisci un numero tra 1 e 6")          '<<< per test: decommentare
    End If
    
    s = Choose(elenco_speciale, "MINISTERO  DELL'INTERNO", "QUESTURE", "NUMERI  INTERNI", "REPARTI MOBILI", "NUMERI  VARI", "UFFICI  DI  MILANO")
    For i = 1 To Len(s)
        t = t & Mid(s, i, 1) & " "
    Next
    titolo.Caption = Trim(t)
    
    Filter = "TIPOLOGIA = '" & Choose(elenco_speciale, "MINISTERO", "QUESTURE", "NUMERI_INTERNI", "REPARTI", "NUMERI_VARI", "UFFICI_MILANO") & "'"
    FilterOn = True
    
    With Me
        .Section("MaskHeader").BackColor = Choose(elenco_speciale, &HE2D5A4, &H91CFBA, &HF1D9C6, &HF1D9C6, &HAACDF9, &HBDFBD8)
        .Section("MaskFooter").Visible = (elenco_speciale = 6)
        .Section("MaskFooter").BackColor = &HBDFBD8
    End With
    
    enable_fields elenco_speciale
    
    btnEdit.Tag = False
    editable False
    btnTUTTI_Click  'serve per mostrare corretttamente il piè di pagina con i pulsanti filtro
    search_box.SetFocus
End Sub
Private Sub enable_fields(i As Integer)
'abilita le caselle di testo necessarie alla maschera in uso
'le caselle "denominazione", "ufficio", "utenza1", "utenza2" e "annotazioni" sono sempre abilitate per tutti
Dim ctl As Variant, v_enable_true As Variant, v_enable_false As Variant
    txtAnnotazioni.Enabled = True
    txtDenominazione.Enabled = True
    txtUfficio.Enabled = True
    txtUtenza1.Enabled = True
    txtUtenza2.Enabled = True
    
    Select Case i
    'MINISTERO DELL'INTERNO
    Case 1
        v_enable_true = Array(txtFax)
        v_enable_false = Array(txtEnti, txtEmail)
        
    'QUESTURE
    Case 2
        v_enable_true = Array(txtFax, txtEmail)
        v_enable_false = Array(txtEnti)
        eti2.Caption = "QUESTURA"
        eti3.Caption = "INDIRIZZO"
        eti4.Caption = "PONTE RADIO"
        eti5.Caption = "TELEFONO"
        
    'INTERNI
    Case 3
        v_enable_true = Array(txtEnti, txtFax)
        v_enable_false = Array(txtUfficio, txtUtenza2, txtEmail)
        eti1.Caption = "UFFICIO"
        eti2.Caption = "UBICAZIONE"
        eti4.Caption = "INTERNO 1"
        eti5.Caption = "INTERNO 2"
    
    'REPARTI
    Case 4
        v_enable_true = Array(txtFax, txtEmail)
        v_enable_false = Array(txtEnti)
        eti2.Caption = "REPARTO"
        eti3.Caption = "INDIRIZZO"
        eti4.Caption = "UTENZA 1"
        eti5.Caption = "UTENZA 2"
    
        
    'NUMERI VARI
    Case 5
        v_enable_true = Array(txtEnti, txtFax, txtEmail)
        v_enable_false = Array()
        
    'NUMERI UTILI
    Case 6
        v_enable_true = Array(txtEnti, txtFax, txtEmail)
        v_enable_false = Array()
    End Select
    
    For Each ctl In v_enable_true
        ctl.Enabled = True
    Next
    For Each ctl In v_enable_false
        ctl.Enabled = False
    Next
End Sub
Private Sub Form_Current()
    Evidenzia "[ID]", [ID], Me
    If btnEdit.Tag <> "" Then Call editable(CBool(btnEdit.Tag))
    If NewRecord Then
        [TIPOLOGIA] = Choose(elenco_speciale, "MINISTERO", "QUESTURE", "NUMERI_INTERNI", "REPARTI", "NUMERI_VARI", "UFFICI_MILANO")
    End If
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'intercetta la pressione del tasto "+" sul tastierino numerico
'per aprire al volo la maschera "m_componi_numero"
    If KeyCode = vbKeyAdd Then DoCmd.OpenForm "m_componi_numero"
End Sub
Private Sub btnEdit_Click()
'PULSANTE CONSENTI/NON CONSENTI MODIFICHE
    Set active_field = Screen.PreviousControl
    If active_field = Controls("search_box") Then active_field.SetFocus
    editable Not CBool(btnEdit.Tag)
    btnEdit.Tag = Not CBool(btnEdit.Tag)
    If CBool(btnEdit.Tag) = True Then
        etiInfo.Caption = "Modifica una cella e poi clicca su Conferma o Annulla"
        btnEdit.FontBold = True
        btnEdit.Caption = "Conferma modifiche"
        btnAnnulla.Enabled = True
        btnEsci.Enabled = False
        btnQuestura.Enabled = False
        btnCommissariato.Enabled = False
        btnPoliziaLocale.Enabled = False
        btnPrefettura.Enabled = False
        btnCFF.Enabled = False
        btnPolfer.Enabled = False
        btnPolaria.Enabled = False
        btnPoliziaPostale.Enabled = False
        btnNUE.Enabled = False
        btnAutocentro.Enabled = False
        btnSantAmbrogio.Enabled = False
        btnPoliziaStradale.Enabled = False
        btnTribunale.Enabled = False
        btnCMO.Enabled = False
        btnDivisionePAS.Enabled = False
        btnTecnicoLogistico.Enabled = False
        btnTutti.Enabled = False
        btnSearch.Enabled = False
        active_field.SetFocus
    Else
        etiInfo.Caption = "Le modifiche attualmente non sono consentite."
        btnEdit.FontBold = False
        btnEdit.Caption = "Consenti modifiche"
        btnAnnulla.Enabled = False
        btnEsci.Enabled = True
        btnQuestura.Enabled = True
        btnCommissariato.Enabled = True
        btnPoliziaLocale.Enabled = True
        btnPrefettura.Enabled = True
        btnCFF.Enabled = True
        btnPolfer.Enabled = True
        btnPolaria.Enabled = True
        btnPoliziaPostale.Enabled = True
        btnNUE.Enabled = True
        btnAutocentro.Enabled = True
        btnSantAmbrogio.Enabled = True
        btnPoliziaStradale.Enabled = True
        btnTribunale.Enabled = True
        btnCMO.Enabled = True
        btnDivisionePAS.Enabled = True
        btnTecnicoLogistico.Enabled = True
        btnTutti.Enabled = True
        btnSearch.Enabled = True
    End If
End Sub
Private Sub btnAnnulla_Click()
'PULSANTE ANNULLA MODIFICHE
    Undo
    Set active_field = Screen.PreviousControl
    etiInfo.Caption = "Le modifiche attualmente non sono consentite."
    btnEdit.Caption = "Consenti modifiche"
    active_field.SetFocus
    btnAnnulla.Enabled = False
    btnEsci.Enabled = True
    editable Not CBool(btnEdit.Tag)
    btnEdit.Tag = Not CBool(btnEdit.Tag)
End Sub
Private Sub btnEsci_Click()
'PULSANTE ESCI
    DoCmd.Close acForm, Me.name
End Sub
Private Sub btnTUTTI_Click()
    DoCmd.Echo False
    search_box = ""
    FilterOn = False
    Filter = "[TIPOLOGIA] = '" & Choose(elenco_speciale, "MINISTERO", "QUESTURE", "NUMERI_INTERNI", "REPARTI", "NUMERI_VARI", "UFFICI_MILANO") & "'"
    FilterOn = True
    DoCmd.Echo True
End Sub
Private Sub search_box_AfterUpdate()
'CASELLA DI TESTO CERCA
Dim s As String
    FilterOn = False
    s = "[TIPOLOGIA] = '" & Choose(elenco_speciale, "MINISTERO", "QUESTURE", "NUMERI_INTERNI", "REPARTI", "NUMERI_VARI", "UFFICI_MILANO") & "' " & _
             "And ([ENTI] Like %1 Or [DENOMINAZIONE] Like %1 Or [UFFICIO] Like %1 Or [UTENZA 1] Like %1 Or [UTENZA 2] Like %1 Or [FAX] Like %1 " & _
             "Or [EMAIL] Like %1 Or [ANNOTAZIONI] Like %1)"
             
    Filter = Replace(s, "%1", Chr$(34) & "*" & search_box & "*" & Chr$(34))
    FilterOn = True
End Sub
Private Sub btnSearch_Click()
'CERCA NOMINATIVO SPECIFICATO (STESSO EFFETTO DI TASTO INVIO)
Dim s As String
    FilterOn = False
    If search_box <> "" Then
        s = "[TIPOLOGIA] = '" & Choose(elenco_speciale, "MINISTERO", "QUESTURE", "NUMERI_INTERNI", "REPARTI", "NUMERI_VARI", "UFFICI_MILANO") & "' " & _
             "And ([ENTI] Like %1 Or [DENOMINAZIONE] Like %1 Or [UFFICIO] Like %1 Or [UTENZA 1] Like %1 Or [UTENZA 2] Like %1 Or [FAX] Like %1 " & _
             "Or [EMAIL] Like %1 Or [ANNOTAZIONI] Like %1)"
             
        Filter = Replace(s, "%1", Chr$(34) & "*" & search_box & "*" & Chr$(34))
        FilterOn = True
    End If
End Sub
Private Sub search_box_Enter()
'la search box deve essere sempre abilitata quindi forza AllowEdits a True se si entra nel campo
'all'uscita ne ripristina il valore attuale (determinato da btnEdit)
    Me.AllowEdits = True
End Sub
Private Sub search_box_Exit(Cancel As Integer)
'la search box deve essere sempre abilitata quindi forza AllowEdits a True se si entra nel campo
'all'uscita ne ripristina il valore attuale (determinato da btnEdit)
    Me.AllowEdits = CBool(btnEdit.Tag)
End Sub
Private Sub search_box_KeyPress(KeyAscii As Integer)
'CASELLA TESTO CERCA
'converte in maiuscolo la digitazione
    If (KeyAscii >= 97 And KeyAscii <= 122) Then KeyAscii = KeyAscii - 32
End Sub
Private Function filter_by_office()
    search_box = ""
    Filter = "[TIPOLOGIA] = '" & Choose(elenco_speciale, "MINISTERO", "QUESTURE", "NUMERI_INTERNI", "REPARTI", "NUMERI_VARI", "UFFICI_MILANO") & "' " & _
             "And [ENTI] Like '" & ActiveControl.Caption & "'"
    FilterOn = True
End Function
'---------------------------- di servizio
Private Sub editable(block As Boolean)
    AllowEdits = block
    AllowDeletions = block
    AllowAdditions = block
    Refresh
End Sub
Private Sub txtEnti_KeyPress(KeyAscii As Integer)
'converte in maiuscolo la digitazione
    If (KeyAscii >= 97 And KeyAscii <= 122) Then KeyAscii = KeyAscii - 32
End Sub
Private Sub txtDenominazione_KeyPress(KeyAscii As Integer)
'converte in maiuscolo la digitazione
    If (KeyAscii >= 97 And KeyAscii <= 122) Then KeyAscii = KeyAscii - 32
End Sub
Private Sub txtUfficio_KeyPress(KeyAscii As Integer)
'converte in maiuscolo la digitazione
    If (KeyAscii >= 97 And KeyAscii <= 122) Then KeyAscii = KeyAscii - 32
End Sub
Private Sub txtUtenza1_KeyPress(KeyAscii As Integer)
'solo numeri
    If KeyAscii = 8 Or (KeyAscii >= 48 And KeyAscii <= 57) Then Exit Sub
    KeyAscii = 0
End Sub
Private Sub txtUtenza2_KeyPress(KeyAscii As Integer)
'solo numeri
    If KeyAscii = 8 Or (KeyAscii >= 48 And KeyAscii <= 57) Then Exit Sub
    KeyAscii = 0
End Sub
Private Sub txtFax_KeyPress(KeyAscii As Integer)
'solo numeri
    If KeyAscii = 8 Or (KeyAscii >= 48 And KeyAscii <= 57) Then Exit Sub
    KeyAscii = 0
End Sub