Close Form if Field is Blank and User Answers No (1 Viewer)

tjkalb

Registered User.
Local time
Today, 08:07
Joined
Jan 30, 2013
Messages
15
:banghead:On my form, I have a SSN and name field. If the SSN is blank, in the On Got Focus on the name field, it looks to see if SSN exists. If it's NULL, it tells the user they must enter, and asks do you want to enter now? Yes - goes back to SSN field (works fine). For No, I want it to close the form. Problem, I am getting a run-time error 2585. I've been searching for days on how to fix this error. From what I understand, a process on the form is still running. I can't figure out how to stop it. Here is my code (Yes - I have tried many variations without luck, but this is the last one). The code that's getting the 2585 error is DoCmd.Close acForm, "frmVetNewMainForm", acSaveNo.
If IsNull(Me.txtSSN) Then
strMsg = "Social Security Number Must Not Be Left Blank!" & vbCrLf
strMsg = strMsg & "Do you want to add new veteran's record?" & vbCrLf

If MsgBox(strMsg, vbQuestion + vbYesNo, "Go to Record?") = vbYes Then
Me.Undo
Me.Refresh
Me.txtSSN.SetFocus
Me.txtSSN.Value = Null
Exit Sub

Else
Me.Undo
Me.Refresh
Me.txtSSN.SetFocus
'Me.txtSSN.Value = Null

DoCmd.OpenForm "fmuMainMenu"

DoCmd.Close acForm, "frmVetNewMainForm", acSaveNo

End If

Else

... The rest works.
 

MarkK

bit cruncher
Local time
Today, 08:07
Joined
Mar 17, 2004
Messages
8,179
I would look at the BeforeUpdate event for this, not GotFocus on a single field. Your use can easily mouse around the form, and your GotFocus event may never fire. Then your validation will fail.
hth
Mark
 

tjkalb

Registered User.
Local time
Today, 08:07
Joined
Jan 30, 2013
Messages
15
It didn't work moving it to BeforeUpdate.:(
 

isladogs

MVP / VIP
Local time
Today, 16:07
Joined
Jan 14, 2017
Messages
18,209
You could try the Form_Close event instead though it will need modifying slightly.
 

tjkalb

Registered User.
Local time
Today, 08:07
Joined
Jan 30, 2013
Messages
15
Just to explain what I want this form to do - SSN is the first field. From other researching, I have it checking to see if the SSN exists already in the database when the user goes to the next field - so, the code runs On Got Focus on the next field. (Someone suggested checking on next field instead of SSN field.) If SSN exists, then it asks if you want to open record. If yes, it opens another form with that SSN. If no, it closes the current form. If the SSN is blank, it gives you an error that it is blank and asks if you want to enter new SSN. If yes, it takes you back to SSN field. If no, I want it to close the form. This is the step that I'm getting the error 2585 error on. I want it to check right away to see if the SSN exists, not when the user closes the form because they would be adding info that could already be in the system if the SSN already exists. So, I don't want to do AfterUpdate because if SSN already exists, it's unnecessary to file in first name. I don't want to do it on form Close because it may be unnecessary to file in all the fields on the form. Make sense?

I've read that there is a process that is still running when I try to run the Close command. How can I stop that process? It errors out on the DoCmd.Close command. Thanks for all your help! There has to be a way to do this!
 

MarkK

bit cruncher
Local time
Today, 08:07
Joined
Mar 17, 2004
Messages
8,179
I have it checking to see if the SSN exists already in the database when the user goes to the next field - so, the code runs On Got Focus on the next field.
What if a mouse user skips that control, and your GotFocus never runs?
Mark
 

tjkalb

Registered User.
Local time
Today, 08:07
Joined
Jan 30, 2013
Messages
15
I have it also checking when the user clicks a Close button, which works fine. The Close button says that it must be entered and goes to the SSN field. There's also a Cancel button that just closes the form without saving any data changes. I wanted to do the code in question so the user didn't have to add all the data before finding out it already exists. In my code, if SSN is blank, I ask the user if they want to add. I was anticipating what a user would do on the form, and this is one thing I could see them doing.
 
Last edited:

Users who are viewing this thread

Top Bottom