How to validate a field in form

Tragac011

Registered User.
Local time
Today, 10:47
Joined
Mar 8, 2014
Messages
56
I need to validate field in form so user cant not skip that data

How to do that?
 
You can do that in the Before Update event of the Form:
Code:
If Len(Me.[COLOR="blue"]FieldName[/COLOR]) & vbNullString) = 0 Then
    Cancel = True
    Msgbox Me.[COLOR="blue"]FieldName [/COLOR]& " is required"
    Me.[COLOR="Blue"]FieldName[/COLOR].SetFocus
End If
 
You can do that in the Before Update event of the Form:
Code:
If Len(Me.[COLOR=blue]FieldName[/COLOR]) & vbNullString) = 0 Then
    Cancel = True
    Msgbox Me.[COLOR=blue]FieldName [/COLOR]& " is required"
    Me.[COLOR=Blue]FieldName[/COLOR].SetFocus
End If

My code is like this
[FONT=&quot]With DoCmd[/FONT]
[FONT=&quot] .RunCommand acCmdSelectRecord[/FONT]
[FONT=&quot] .RunCommand acCmdCopy[/FONT]
[FONT=&quot] .GoToRecord , , acNewRec[/FONT]
[FONT=&quot] Me.AllowEdits = True[/FONT]
[FONT=&quot] .SetWarnings False[/FONT]
[FONT=&quot] .RunCommand acCmdPaste[/FONT]
[FONT=&quot] .SetWarnings True[/FONT]
[FONT=&quot] Me.AllowEdits = False[/FONT]
Me.NameOfTextbox1.Value = Null Me.NameOfTextbox2.Value = Null [FONT=&quot] [/FONT]
[FONT=&quot] End With[/FONT]


Where exactly do put that?
 
Good to know!

I'm sure you'll come back on this thread ;)
 
Good to know!

I'm sure you'll come back on this thread ;)

Unfortunately, You was right. I have problem with that code I can open next record never mind that I didn;t fill neccesery data
 
Remember that it needs to be written for every required field.

Let me see the full code that you wrote. And I hope you put it in code tags this time!
 
Remember that it needs to be written for every required field.

Let me see the full code that you wrote. And I hope you put it in code tags this time!

Code
Code:
If Len(Me.Datum_ugovora) & vbNullString = 0 Then
        Cancel = True
        MsgBox Me.Datum_ugovora & " is required"
        Me.Datum_ugovora.SetFocus
    End If
    If Len(Me.Od_kada_je_kod_nas) & vbNullString = 0 Then
        Cancel = True
        MsgBox Me.Od_kada_je_kod_nas & " is required"
        Me.Od_kada_je_kod_nas.SetFocus
    End If
    If Len(Me.Do_kada_je_kod_nas) & vbNullString = 0 Then
        Cancel = True
        MsgBox Me.Do_kada_je_kod_nas & " is required"
        Me.Do_kada_je_kod_nas.SetFocus
    End If
    If Len(Me.Ime_vlasnika_psa) & vbNullString = 0 Then
        Cancel = True
        MsgBox Me.Ime_vlasnika_psa & " is required"
        Me.Ime_vlasnika_psa.SetFocus
    End If
    If Len(Me.Prezime_vlasnika_psa) & vbNullString = 0 Then
        Cancel = True
        MsgBox Me.Prezime_vlasnika_psa & " is required"
        Me.Prezime_vlasnika_psa.SetFocus
    End If
    If Len(Me.Cena_po_danu) & vbNullString = 0 Then
        Cancel = True
        MsgBox Me.Cena_po_danu & " is required"
        Me.Cena_po_danu.SetFocus
    End If
  End Sub
[code\]

[code]
With DoCmd
        .RunCommand acCmdSelectRecord
        .RunCommand acCmdCopy
        .GoToRecord , , acNewRec
        Me.AllowEdits = True
        .SetWarnings False
        .RunCommand acCmdPaste
        .SetWarnings True
        Me.AllowEdits = False
        Me.Datum_ugovora.Value = Null
        Me.Od_kada_je_kod_nas.Value = Null
        Me.Do_kada_je_kod_nas.Value = Null
        Me.Cena_po_danu.Value = Null
        Me.[Placeno prilikom prijema].Value = Null
        Me.Placeno_prilikom_rezervacije.Value = Null
        Me.Osiguranje.Value = Null
        Me.Prijem_posle_18h.Value = Null
    End With
End Sub
[code\]
 
The end code tag is wrong, it's [/code]. Copy the code again (unless it won't work) and paste it here the correct way.
 
The end code tag is wrong, it's [/code]. Copy the code again (unless it won't work) and paste it here the correct way.

Code
Code:
If Len(Me.Datum_ugovora) & vbNullString = 0 Then
        Cancel = True
        MsgBox Me.Datum_ugovora & " is required"
        Me.Datum_ugovora.SetFocus
    End If
    If Len(Me.Od_kada_je_kod_nas) & vbNullString = 0 Then
        Cancel = True
        MsgBox Me.Od_kada_je_kod_nas & " is required"
        Me.Od_kada_je_kod_nas.SetFocus
    End If
    If Len(Me.Do_kada_je_kod_nas) & vbNullString = 0 Then
        Cancel = True
        MsgBox Me.Do_kada_je_kod_nas & " is required"
        Me.Do_kada_je_kod_nas.SetFocus
    End If
    If Len(Me.Ime_vlasnika_psa) & vbNullString = 0 Then
        Cancel = True
        MsgBox Me.Ime_vlasnika_psa & " is required"
        Me.Ime_vlasnika_psa.SetFocus
    End If
    If Len(Me.Prezime_vlasnika_psa) & vbNullString = 0 Then
        Cancel = True
        MsgBox Me.Prezime_vlasnika_psa & " is required"
        Me.Prezime_vlasnika_psa.SetFocus
    End If
    If Len(Me.Cena_po_danu) & vbNullString = 0 Then
        Cancel = True
        MsgBox Me.Cena_po_danu & " is required"
        Me.Cena_po_danu.SetFocus
    End If
  End Sub

Code:
With DoCmd
        .RunCommand acCmdSelectRecord
        .RunCommand acCmdCopy
        .GoToRecord , , acNewRec
        Me.AllowEdits = True
        .SetWarnings False
        .RunCommand acCmdPaste
        .SetWarnings True
        Me.AllowEdits = False
        Me.Datum_ugovora.Value = Null
        Me.Od_kada_je_kod_nas.Value = Null
        Me.Do_kada_je_kod_nas.Value = Null
        Me.Cena_po_danu.Value = Null
        Me.[Placeno prilikom prijema].Value = Null
        Me.Placeno_prilikom_rezervacije.Value = Null
        Me.Osiguranje.Value = Null
        Me.Prijem_posle_18h.Value = Null
    End With
End Sub
 
I just noticed there's a parentheses that shouldn't be there in my code. The Len part should be written like this:
Code:
Len(Me.Datum_ugovora & vbNullString)
I hope you noticed the change?
 

Users who are viewing this thread

Back
Top Bottom