If user inputs any information, then save and close, otherwise just close (1 Viewer)

cmray58

Registered User.
Local time
Today, 01:27
Joined
Mar 24, 2014
Messages
70
I have a command button that closes the user input form and returns back to the navigation form. My current code is as follows:

Code:
If IsNewRecord = "Yes" Then
AssessmentEndTime.Value = Time
IsNewRecord = "No"
DoCmd.RunCommand acCmdSaveRecord
Else
End If
If MsgBox("Would you like to save and close the current assessment?", vbYesNo + vbQuestion) = vbYes Then
DoCmd.Close
DoCmd.OpenForm "Navigation"
Else
End If

However, I want to first check whether the user input any data at all into the form. If they don't, I just want to close the form without saving the record. The way the code is written right now, if they just open the form and click the button, a record is saved with all data just being completely blank.
 

jdraw

Super Moderator
Staff member
Local time
Today, 04:27
Joined
Jan 23, 2006
Messages
15,379
What is the name of Control where the user could enter or select a data value?
 

cmray58

Registered User.
Local time
Today, 01:27
Joined
Mar 24, 2014
Messages
70
Sorry, I'm not sure I understand the question. I want to it to check whether the user input data into any field, not just one in particular.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:27
Joined
Oct 29, 2018
Messages
21,454
Hi. Without seeing your form, this is just a guess, but try checking the Dirty property first before checking IsNewRecord.
 

cmray58

Registered User.
Local time
Today, 01:27
Joined
Mar 24, 2014
Messages
70
Hi. Without seeing your form, this is just a guess, but try checking the Dirty property first before checking IsNewRecord.

Thanks, I think this is what I'm looking for. I'm just not sure how to write that in a command button.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:27
Joined
Oct 29, 2018
Messages
21,454
Thanks, I think this is what I'm looking for. I'm just not sure how to write that in a command button.
Try:
Code:
If Me.Dirty Then
    If IsNewRecord="Yes" Then
...
    End If

End If
No guarantees...
 

cmray58

Registered User.
Local time
Today, 01:27
Joined
Mar 24, 2014
Messages
70
I agree with dbGuy, here is a link to info re form dirty

Thanks, I checked out the link, but I'm still not clear how to use the Dirty property within a command button. The link you provided looks like it's using the Form dirty event. I want to assess the form's dirty property when the button is clicked and if it's dirty then X and if not, Y.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:27
Joined
Oct 29, 2018
Messages
21,454
Thanks, I checked out the link, but I'm still not clear how to use the Dirty property within a command button. The link you provided looks like it's using the Form dirty event. I want to assess the form's dirty property when the button is clicked and if it's dirty then X and if not, Y.
Hmm, did you try what I suggested and it didn't work? Just curious...
 

cmray58

Registered User.
Local time
Today, 01:27
Joined
Mar 24, 2014
Messages
70
Hmm, did you try what I suggested and it didn't work? Just curious...

Here's my current code. I'm getting tripped up in the amount of IF and ELSE clauses. I'm not clear on the order of operations.

Code:
If Me.Dirty Then
    DoCmd.RunCommand acCmdSaveRecord
    If IsNewRecord = "Yes" Then
    AssessmentEndTime.Value = Time
    IsNewRecord = "No"
    DoCmd.RunCommand acCmdSaveRecord
Else
DoCmd.Close
DoCmd.OpenForm "Navigation"
End If
If MsgBox("Would you like to save and close the current assessment?", vbYesNo + vbQuestion) = vbYes Then
DoCmd.Close
DoCmd.OpenForm "Navigation"
Else
DoCmd.Close
DoCmd.OpenForm "Navigation"
End If
End If

My desired outcome (in words) is:

If the nothing on the form changed:

1) Close the form and open Navigation form.

If anything changed:

1a) If IsNewRecord = Yes, change it to No and timestamp the end time
2a) Save record
3a) Close form and open Navigation

1b) If IsNewRecord = No, leave as is
2b) Save record
3b) Close form and open Navigation
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:27
Joined
Oct 29, 2018
Messages
21,454
Here's my current code. I'm getting tripped up in the amount of IF and ELSE clauses. I'm not clear on the order of operations.
...
My desired outcome (in words) is:

If the nothing on the form changed:

1) Close the form and open Navigation form.

If anything changed:

1a) If IsNewRecord = Yes, change it to No and timestamp the end time
2a) Save record
3a) Close form and open Navigation

1b) If IsNewRecord = No, leave as is
2b) Save record
3b) Close form and open Navigation
Hi. Let's backup a bit. Didn't you say you already have a working code where all (or most) of what you need already works? If so, all I was saying is wrap all that in one outer check where you either execute your code or not. For example, let's say you have the following already:
Code:
If IsNewRecord = Yes Then
    ... 'do all the stuff in 2a and 3a above
Else
    ... 'do all the stuff in 2b and 3b above
End If
Then, you said, with this code, you were getting empty records because the user didn't make any edits/changes to the form. So, all I'm saying is check first if the user made any changes. If they did, execute your code. If they didn't, then no need to do anything else, correct? So, in other words:
Code:
If Me.Dirty Then
    If IsNewRecord = Yes Then
        ... 'do all the stuff in 2a and 3a above 
    Else
        ... 'do all the stuff in 2b and 3b above
    End If
End If
Hope it makes sense...
 

cmray58

Registered User.
Local time
Today, 01:27
Joined
Mar 24, 2014
Messages
70
Hi. Let's backup a bit. Didn't you say you already have a working code where all (or most) of what you need already works? If so, all I was saying is wrap all that in one outer check where you either execute your code or not. For example, let's say you have the following already:
Code:
If IsNewRecord = Yes Then
    ... 'do all the stuff in 2a and 3a above
Else
    ... 'do all the stuff in 2b and 3b above
End If
Then, you said, with this code, you were getting empty records because the user didn't make any edits/changes to the form. So, all I'm saying is check first if the user made any changes. If they did, execute your code. If they didn't, then no need to do anything else, correct? So, in other words:
Code:
If Me.Dirty Then
    If IsNewRecord = Yes Then
        ... 'do all the stuff in 2a and 3a above 
    Else
        ... 'do all the stuff in 2b and 3b above
    End If
End If
Hope it makes sense...


Okay, backed up and took your advise. One step at a time, I arrived here:

Code:
If Me.Dirty Then
    If MsgBox("Would you like to save and close the current assessment?", vbYesNo + vbQuestion) = vbYes Then
        If IsNewRecord = "Yes" Then
            AssessmentEndTime.Value = Time
            IsNewRecord = "No"
            DoCmd.RunCommand acCmdSaveRecord
            DoCmd.Close
            DoCmd.OpenForm "Navigation"
        Else
            DoCmd.RunCommand acCmdSaveRecord
            DoCmd.Close
            DoCmd.OpenForm "Navigation"
        End If
    Else
    End If
Else
DoCmd.Close
DoCmd.OpenForm "Navigation"
End If
End Sub

However, Me.Dirty is not working properly. Even if I enter data into the form, it skips from Me.Dirty all the way down to the last else where it just closes the form and opens navigation. It's not recognizing the data input. Database is attached. We're talking about form CALS v2. From reading elsewhere, it sounds like Me.Dirty is if anything has changed since the record was last saved. But, the record is automatically saved with every keystroke. So wouldn't it always NOT be dirty?
 

Attachments

  • MHAW v6.2.zip
    983.7 KB · Views: 73
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 01:27
Joined
Oct 29, 2018
Messages
21,454
Okay, backed up and took your advise. One step at a time, I arrived here:
...
However, Me.Dirty is not working properly. Even if I enter data into the form, it skips from Me.Dirty all the way down to the last else where it just closes the form and opens navigation. It's not recognizing the data input. Database is attached. We're talking about form CALS v2. From reading elsewhere, it sounds like Me.Dirty is if anything has changed since the record was last saved. But, the record is automatically saved with every keystroke. So wouldn't it always NOT be dirty?
Hi. I downloaded your file and wasn't getting the same behavior as you described. When I open the file, I see the main form. If I click on "CALS Assessment" button, the form in question opens to a new record. If don't do anything and simply click on the "Close Assessment" button, then this form closes and the main form opens again. However, when I selected "Chris Ray" from the "Assesssor's Name" dropdown and then click on the "Close Assessment" button, I get the "Would you like to save and close..." prompt, which tells me the Dirty part is working as it should.
 

cmray58

Registered User.
Local time
Today, 01:27
Joined
Mar 24, 2014
Messages
70
Hi. I downloaded your file and wasn't getting the same behavior as you described. When I open the file, I see the main form. If I click on "CALS Assessment" button, the form in question opens to a new record. If don't do anything and simply click on the "Close Assessment" button, then this form closes and the main form opens again. However, when I selected "Chris Ray" from the "Assesssor's Name" dropdown and then click on the "Close Assessment" button, I get the "Would you like to save and close..." prompt, which tells me the Dirty part is working as it should.

Thanks...that is strange. It seems to be working now. Thank you for the help and patience.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:27
Joined
Oct 29, 2018
Messages
21,454
Thanks...that is strange. It seems to be working now. Thank you for the help and patience.
Hi. You're welcome. We're all happy to assist. Good luck with your project.
 

Users who are viewing this thread

Top Bottom