dilluns, 24 de desembre del 2012

Formulari editable amb botons en VBA

Creem un formulari que llegeix una consulta que afecta a dues taules.
Es pot editar el contingut.
Presenta dos botons i gestiona quan arriba al principi i al final
Fa una comparativa marcant en vermell les diferencies entre la casella superior i la inferior.

Aspecte de les taules i la consulta. La consulta connecta pel nom de municipi i sols afecta als que tenen l'estat Activo. El recordset apunta a aquesta consulta.

Aspecte del formulari:


El codi (es pot veure amb colorins amb Notepad++):


Dim dbs As Database
Dim rs As Recordset

Private Sub cmdSortir_Click()
rs.Close
Set rs = Nothing
DoCmd.Close

End Sub

Private Sub Form_Load()
Set dbs = CurrentDb
Set rs = dbs.OpenRecordset("Qry01")
Correccio
End Sub
Private Sub cmdPrev_Click()
    rs.MovePrevious
        If rs.BOF Then
         rs.MoveFirst
        End If
    Correccio
End Sub
Private Sub cmdNext_Click()
    rs.MoveNext
        If rs.EOF Then
         rs.MoveLast
        End If
    Correccio
End Sub
Private Sub Correccio()
Me!txtM = rs![municipio]
Me!txtSA = rs![SuperficieA]
Me!txtSB = rs![SuperficieB]
Me!txtSC = rs![SuperficieC]
Me!txtTA = rs![TotalA]
Me!txtTB = rs![TotalB]
Me!txtTC = rs![TotalC]

Validacio
End Sub
Private Sub txtM_BeforeUpdate(Cancel As Integer)
rs.Edit
rs![municipio] = Me!txtM
rs.Update
End Sub
Private Sub txtSA_BeforeUpdate(Cancel As Integer)
rs.Edit
rs![SuperficieA] = Me!txtSA
rs.Update
End Sub
Private Sub txtSB_BeforeUpdate(Cancel As Integer)
rs.Edit
rs![SuperficieB] = Me!txtSB
rs.Update
End Sub
Private Sub txtSC_BeforeUpdate(Cancel As Integer)
rs.Edit
rs![SuperficieC] = Me!txtSC
rs.Update
End Sub
Private Sub txtTA_BeforeUpdate(Cancel As Integer)
rs.Edit
rs![TotalA] = Me!txtTA
rs.Update
End Sub
Private Sub txtTB_BeforeUpdate(Cancel As Integer)
rs.Edit
rs![TotalB] = Me!txtTB
rs.Update
End Sub
Private Sub txtTC_BeforeUpdate(Cancel As Integer)
rs.Edit
rs![TotalC] = Me!txtTC
rs.Update
End Sub
Private Sub Validacio()
    If rs![SuperficieA] = rs![TotalA] Then
    Me!txtValidA.ForeColor = RGB(0, 0, 0)
    Me!txtValidA = "OK"
    Else
    Me!txtValidA.ForeColor = RGB(255, 0, 0)
    Me!txtValidA = "Error"
    End If

End Sub

Cap comentari:

Publica un comentari a l'entrada