Error message if field is empty access

Solved
Loic_7447 Posted messages 32 Registration date   Status Member Last intervention   -  
f894009 Posted messages 17417 Registration date   Status Member Last intervention   -
Hello,

I have a form that serves to add data to a table, using 4 text boxes and a final button.
The form works very well, but I would like it so that if a field is empty when validating the addition, the record is not created and an error message appears.

Here is my code:

Private Sub btn_ajouter_Click()

Set db = CurrentDb
Set rs = db.OpenRecordset("Stock_atelier")

rs.AddNew

rs!Référence = Me.txt_reference
rs!Fournisseur = Me.txt_fournisseur
rs!Quantité = Me.txt_quantité
rs!Type = Me.txt_outil

rs.Update

rs.Close
db.Close

MsgBox "Référence ajoutée avec succès "
txt_reference = ""
txt_fournisseur = ""
txt_quantité = ""
txt_outil = ""

Set rs = Nothing
Set db = Nothing

End Sub


I tried to add the following lines:

If IsNull(Me.txt_reference) Then
MsgBox ("Veuillez renseigner toutes les cases")
ElseIf IsNull(Me.txt_fournisseur) Then
MsgBox ("Veuillez renseigner toutes les cases")
ElseIf IsNull(Me.txt_quantité) Then
MsgBox ("Veuillez renseigner toutes les cases")
ElseIf IsNull(Me.txt_outil) Then
MsgBox ("Veuillez renseigner toutes les cases")
End If


But nothing works (maybe I didn't put them in the right place or who knows).

Could you help me??
Thanks in advance.

1 answer

  1. f894009 Posted messages 17417 Registration date   Status Member Last intervention   1 717
     
    Hello,

    A form field is not null but empty since it's a text format:
    If Me.txt_reference="" Then...........
    1
    1. Loic_7447 Posted messages 32 Registration date   Status Member Last intervention  
       
      It was just that, thank you!!
      0
    2. f894009 Posted messages 17417 Registration date   Status Member Last intervention   1 717
       
      Re,

      Mark your Access request as completed
      0