1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| Option Compare Database
Option Explicit
Dim Rs As DAO.Recordset
Dim Sql As String
Private Sub bc_enregistrer_Click()
On Error GoTo bc_enregistrer_Click_Err
'test du nom de client
If Me.tb_Nom_Client & "" = "" Then
MsgBox "Le nom du client est une information obligatoire.", vbCritical + vbOKOnly, "Enregistrement"
Me.tb_Nom_Client.SetFocus
Exit Sub
End If
If Me.Caption = "Saisie" Then
'nouveau client
If MsgBox("Voulez-vous enregistrer le nouveau client", vbQuestion + vbYesNo, "Enregistrement") = vbNo Then Exit Sub
'calcul du n°
Me.tb_N_Client = DLookup("max([N_Client])", "t_client") + 1
'enregistrement
Sql = "SELECT T_client.* FROM T_client"
Set Rs = CurrentDb.OpenRecordset(Sql, dbOpenDynaset)
Rs.AddNew
Rs("Adresse") = Me.tb_Adresse
Rs("Code_postale") = Me.tb_Code_postale
Rs("N_Client") = Me.tb_N_Client
Rs("Nom_Client") = Me.tb_Nom_Client
Rs("Nom_contact") = Me.tb_Nom_contact
Rs("Ville") = Me.tb_Ville
Rs.Update
'mode modif
Me.Caption = "Modification du clinet N° " & Me.tb_N_Client
Me.tb_N_Client.Visible = True
Me.tb_N_Client.Enabled = False
Else
'enregistrement
Sql = "SELECT T_client.* FROM T_client WHERE T_client.N_Client = " & Me.tb_N_Client & ";"
Set Rs = CurrentDb.OpenRecordset(Sql, dbOpenDynaset)
Rs.Edit
Rs("Adresse") = Me.tb_Adresse
Rs("Code_postale") = Me.tb_Code_postale
Rs("N_Client") = Me.tb_N_Client
Rs("Nom_Client") = Me.tb_Nom_Client
Rs("Nom_contact") = Me.tb_Nom_contact
Rs("Ville") = Me.tb_Ville
Rs.Update
End If
MsgBox "Enregistrement du client N° " & Me.tb_N_Client & " réussit", vbOKOnly, "Enregistrement"
Set Rs = Nothing
Exit Sub
bc_enregistrer_Click_Err:
MsgBox "Une erreur inatendue s'est produite lors de l'enregistrement (erreur N° " & Err.Number & " ) dont la description est : """ & Err.Description & """. L'enregistrement n'est peut-être pas réalisé! Contactez l'administrateur de la base !", vbCritical + vbOKOnly, "Erreur"
Set Rs = Nothing
Exit Sub
End Sub
Private Sub bc_exit_Click()
DoCmd.Close acForm, Me.Name
End Sub
Private Sub Form_Load()
On Error GoTo Form_Load_Err
If Me.OpenArgs = "S" Then
'saisie d'un nouveau client
Me.Caption = "Saisie"
Me.tb_N_Client.Visible = False
Else
'modif d'un client
Me.Caption = "Modification du clinet N° " & Me.OpenArgs
'selection des infos sur le client
Sql = "SELECT T_client.* FROM T_client WHERE T_client.N_Client = " & Me.OpenArgs & ";"
Set Rs = CurrentDb.OpenRecordset(Sql, dbOpenDynaset)
If Rs.BOF = True And Rs.EOF = True Then
MsgBox "Impossile de trouver le client sélectionné", vbCritical + vbOKOnly, "Modification impossible"
DoCmd.Close acForm, Me.Name
GoTo Form_Load_Exit
Else
Rs.MoveLast
Rs.MoveFirst
If Rs.RecordCount <> 1 Then
MsgBox "Le N° de client n'est pas unique ! Contactez l'administrateur de la base.", vbCritical + vbOKOnly, "Modification impossible"
DoCmd.Close acForm, Me.Name
GoTo Form_Load_Exit
End If
End If
'remplissage du form
Me.tb_Adresse = Rs("Adresse")
Me.tb_Code_postale = Rs("Code_postale")
Me.tb_N_Client = Rs("N_Client")
Me.tb_Nom_Client = Rs("Nom_Client")
Me.tb_Nom_contact = Rs("Nom_contact")
Me.tb_Ville = Rs("Ville")
'verrouillage du N° de client
Me.tb_N_Client.Enabled = False
End If
Form_Load_Exit:
Set Rs = Nothing
Exit Sub
Form_Load_Err:
MsgBox "Une erreur inatendue s'est produite (erreur N° " & Err.Number & " ) dont la description est : """ & Err.Description & """. Contactez l'administrateur de la base !", vbCritical + vbOKOnly, "Erreur"
Set Rs = Nothing
DoCmd.Close acForm, Me.Name
End Sub |
Partager