Solved Code to check if field on form is Null (1 Viewer)

Number11

Member
Local time
Today, 09:31
Joined
Jan 29, 2020
Messages
607
Hi,

So here is the code i am using to check if the field called Q1 has been completed, if not then it takes the user back to the form and highlights the field in red. however the code still runs the rest of the code and the form gets closed. so i need to add in a break point if Q1 is not completed allow user to update and then user clicks on the button again?

Private Sub Command372_Click()
On Error GoTo HandleError
Dim strSql As String
Dim ctl As Variant

strSql = Text4
SetClipboard strSql
Text4.value = ""

If IsNull(Me.Q1) Then

Dialog.Box "Please complete all questions in section A!", vbCritical, "Data entry error..."
Me.Q1.BorderColor = vbRed
Me.Q1.SetFocus
DoCmd.GoToControl "Me.Q1"


Else

Me.Recordset.MoveNext
Dialog.Box "Template has been copied to Clipboard"
Call Run

HandleExit:
Exit Sub
HandleError:
Dialog.Box "You have completed all selected outcomes", vbInformation, "Task Complete"

DoCmd.SetWarnings False
DoCmd.OpenQuery "Update Record Set"
DoCmd.SetWarnings False
DoCmd.Close
End If
End Sub
 
Last edited:

bob fitz

AWF VIP
Local time
Today, 09:31
Joined
May 23, 2011
Messages
4,719
Hi,

So here is the code i am using to check if the field called Q1 has been completed, if not then it takes the user back to the form and highlights the field in red. however the code still runs the rest of the code and the form gets closed. so i need to add in a break point if Q1 is not completed allow user to update and then user clicks on the button again?

Private Sub Command372_Click()
On Error GoTo HandleError
Dim strSql As String
Dim ctl As Variant

strSql = Text4
SetClipboard strSql
Text4.value = ""

If IsNull(Me.Q1) Then

Dialog.Box "Please complete all questions in section A!", vbCritical, "Data entry error..."
Me.Q1.BorderColor = vbRed
Me.Q1.SetFocus
DoCmd.GoToControl "Me.Q1"

Else

DoCmd.RunCommand (acCmdSaveRecord)
Me.Recordset.MoveNext
Dialog.Box "Template has been copied to Clipboard"
Call Run

HandleExit:
Exit Sub
HandleError:
Dialog.Box "You have completed all selected outcomes", vbInformation, "Task Complete"

DoCmd.SetWarnings False
DoCmd.OpenQuery "Update Record Set"
DoCmd.SetWarnings False
DoCmd.Close
End If
End Sub
Put your validation code in the forms Before Update event. If it fails you can cancel the event so the form stays open.
 

moke123

AWF VIP
Local time
Today, 04:31
Joined
Jan 11, 2013
Messages
3,912
Add an exit sub
 

moke123

AWF VIP
Local time
Today, 04:31
Joined
Jan 11, 2013
Messages
3,912
If IsNull(Me.Q1) Then

Dialog.Box "Please complete all questions in section A!", vbCritical, "Data entry error..."
Me.Q1.BorderColor = vbRed
Me.Q1.SetFocus
DoCmd.GoToControl "Me.Q1"

exit sub

Else


. . . .
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:31
Joined
May 7, 2009
Messages
19,232
what is Dialog.Box? is this on Access/Excel?

add Exit Sub as suggested:
Code:
If IsNull(Me.Q1) Then

Dialog.Box "Please complete all questions in section A!", vbCritical, "Data entry error..."
Me.Q1.BorderColor = vbRed
Me.Q1.SetFocus
Exit Sub
Else
...
...
 

Number11

Member
Local time
Today, 09:31
Joined
Jan 29, 2020
Messages
607
what is Dialog.Box? is this on Access/Excel?

add Exit Sub as suggested:
Code:
If IsNull(Me.Q1) Then

Dialog.Box "Please complete all questions in section A!", vbCritical, "Data entry error..."
Me.Q1.BorderColor = vbRed
Me.Q1.SetFocus
Exit Sub
Else
...
...

Many thanks that worked, so now the form has the field highlighted in red, but i can enter anything now :(

BTW Dialog.box is a replacement for Message box
 

Users who are viewing this thread

Top Bottom