Required Entry

  • Thread starter Thread starter Rpt_Me4NotBeingSmart
  • Start date Start date
R

Rpt_Me4NotBeingSmart

on a dataentry datasheet, how can I make field2 required to be filled out if
field1 = "I"?
 
If you are using a form to collect the data, you can validate entry
using the form BeforeUpdate event

'~~~~~~~~~~~~~~~~
if nz(me.controlname1,"") = "I" then
if IsNull(me.controlname2) then
me.controlname2.SetFocus
msgbox "You must enter <something>",,"Cannot update record"
CANCEL = true
end if
end if
'~~~~~~~~~~~~~~~~

WHERE
controlname1 is the Name of the first control
controlname2 is the Name of the second control


Warm Regards,
Crystal

remote programming and training

Access Basics
8-part free tutorial that covers essentials in Access
http://www.AccessMVP.com/strive4peace

*
(: have an awesome day :)
*
 
I used the code below as the eventprocedure for the form but I am not getting
an error message when VarianceCode = I and OtherNotes is blank/null. Did I
do something wrong here?



Private Sub Form_BeforeUpdate(Cancel As Integer)
If Nz(Me.VarianceCode, "") = "I" Then
If IsNull(Me.OtherNotes) Then
Me.OtherNotes.SetFocus
MsgBox "You must enter notes if the variance code is I", , "Cannot
update record"
Cancel = True
End If
End If
End Sub
 
perhaps the code ends or begins with a space... test the TRIMmed value

If Trim(Nz(Me.VarianceCode, "")) = "I" Then

on the same token, perhaps there is a space or an empty string in
OtherNotes...

If Len(Trim(nz(Me.OtherNotes,""))) = 0 then

also, it would be good to add an error handler to the code

'~~~~~~~~~~~~~~~~~~
'put this at the top of your program, right after the procedure
'declaration (skip a line first for better readability)

'set up Error Handler
On Error GoTo Proc_Err

'-----------
'other statements
'-----------

Proc_Exit:
Exit Sub

Proc_Err:
'NOTE: replace ProcedureName with the name of your procedure
MsgBox Err.Description, , _
"ERROR " & Err.Number _
& " ProcedureName"

Resume Proc_Exit

'if you want to single-step code to find error, CTRL-Break at MsgBox
'then set this to be the next statement
Resume

End Sub
'~~~~~~~~~~~~~~~~~~

and compile...

'~~~~~~~~~ Compile ~~~~~~~~~

Whenever you change code, references, or switch versions, you should
always compile before executing.

from the menu in a VBE (module) window: Debug, Compile

fix any errors on the yellow highlighted lines

keep compiling until nothing happens (this is good!)

~~
if you run code without compiling it, you risk corrupting your database

~~~~~ also be sure to use Option Explicit at the top of each module so
variables that are not declared or are misspelled will be picked up



Warm Regards,
Crystal

remote programming and training

Access Basics
8-part free tutorial that covers essentials in Access
http://www.AccessMVP.com/strive4peace

*
(: have an awesome day :)
*
 
Back
Top