Form controls not updating unless click outside control (1 Viewer)

helmerr

Registered User.
Local time
Today, 06:03
Joined
Nov 27, 2013
Messages
140
I figure there may be an easy solution for this one:

I'm trying to add new records using a form with text and combo boxes. On the form's On_Load event, I using the command DoCmd.GoToRecord , , acNewRec to prepare for new record entry first, thus clearing the controls.

Data is entered into the controls, then the user clicks the cmdSave button which initiates the code below. However, unless the user clicks elsewhere after filling in the last text box control, etc., the form does not recognize that data was actually entered into the final control. The user receives the message from the save code to "Please fill in all required fields."

Code:
'test required fields
If IsNull(Me.txtVehicleID.Value) _
Or IsNull(Me.cboVehicleNum.Value) _
Or IsNull(Me.txtODate.Value) _
Or IsNull(Me.txtFuel.Value) _
Or IsNull(Me.txtOdometer.Value) Then
    MsgBox "Please fill in all required fields."
Else

    MsgBox "Record saved."

    'Requery forms
    Me.Requery
    Me.Parent.frmMileageDetail.Form.Requery

    ' Prepare form for new record input
    DoCmd.GoToRecord , , acNewRec

End If
 

MarkK

Super Moderator
Local time
Today, 03:03
Joined
Mar 17, 2004
Messages
7,761
Are you aware the you can put a form in DataEntry mode? Then the behaviors you describe are automatic, and you don't need a save button. Just tab out of the last field and into the next record, which is new by default.
I think that would be a simpler solution that making this work.
hth
Mark
 

helmerr

Registered User.
Local time
Today, 06:03
Joined
Nov 27, 2013
Messages
140
Hi Mark,

My form is actually composed of one main form and two subforms. The top subform allows for new entries and edits, and has command buttons for adding a new record, resetting the form as an easy way to start the new record from scratch in case of error, a save button, a delete button, and a report button. The Add and Save buttons are there for the user's visual comfort.

The bottom sub form is a continuous form and if a user selects an item from the bottom sub form, it is populated to the top form for editing. I'm not allowing edits on the continuous form.

At any rate; I did try enabling data entry mode as suggested, but received the same error.
 

JHB

Have been here a while
Local time
Today, 12:03
Joined
Jun 17, 2012
Messages
7,720
I can't reproduce that error, could you post your database with some sample data + a description how to get that error?
 

isladogs

CID Moderator
Staff member
Local time
Today, 11:03
Joined
Jan 14, 2017
Messages
13,406
After each field is updated, you could set the focus to another control.
This will cause the change to be committed and should solve your issue
 

helmerr

Registered User.
Local time
Today, 06:03
Joined
Nov 27, 2013
Messages
140
Hey guys - I've attached a copy of the database.

Also, I set tried using me.txtVehicleID.SetFocus on the control txtOdometer's After Update event (since this is likely the last control to be typed in), but it didn't help. I haven't tried using it's On Change event, as I'm pretty sure this would move focus to the other control after each typed character.
 

Attachments

helmerr

Registered User.
Local time
Today, 06:03
Joined
Nov 27, 2013
Messages
140
I think I've got it:
I had needed to put the above command "me.txtVehicleID.SetFocus" as the first event in the cmdSave button.

Thanks for your help.
 

isladogs

CID Moderator
Staff member
Local time
Today, 11:03
Joined
Jan 14, 2017
Messages
13,406
Hi

Just got back & had a quick look at this

I had needed to put the above command "me.txtVehicleID.SetFocus" as the first event in the cmdSave button.
I don't see how you can do that as the control is hidden

Alternatively, you could also do this instead

Code:
Private Sub txtOdometer_AfterUpdate()
    If Me.Dirty Then Me.Dirty = False
End Sub
Two other suggestions:
1. Add a validation rule (>0 to the Fuel textbox to prevent invalid entries like -5
2. Similarly add a validation rule > previous odometer entry for similar reasons
AND/OR Pre-enter the previous odometer value for editing by the user

The first is easy - the second a bit more tricky
 

MarkK

Super Moderator
Local time
Today, 03:03
Joined
Mar 17, 2004
Messages
7,761
I downloaded your Db, and if I was doing what you are doing in that header form, I would not bind it to a record source at all. I would use it unbound, validate all the data entered in the controls prior to save, and then use an insert query to push the new data directly into the table.

You can do a similar thing if needs be to do updates. Load the unbound data in the subform and save the ID somewhere too, but hidden. If the ID is present, then hitting save is a update, not a insert.

But it doesn't make sense to me to have a bound form, AND a save button, because a bound form is designed to save automatically, so you are always going to have an uphill struggle with that design, IMO. Users will find a way--with a bound form--to save that record in ways that by-pass your save button, like hitting the pagedown key, or clicking in your detail section, and so if you want to have all the control a save button provides, then you have to (in my mind) disable ALL the automatic features of a bound form.

hth
Mark
 

helmerr

Registered User.
Local time
Today, 06:03
Joined
Nov 27, 2013
Messages
140
Hmm.. I never thought of that. Let met think about this again..
 

JHB

Have been here a while
Local time
Today, 12:03
Joined
Jun 17, 2012
Messages
7,720
The "problem" is that it is not a real button but a picture you are treated as a button, meaning that the focus does not leave the last control you're inputting data.
A solution could be to use a real button where you have the image shown, (if you want to keep your design).
 

MarkK

Super Moderator
Local time
Today, 03:03
Joined
Mar 17, 2004
Messages
7,761
A button also has a Transparent property, so you can put it on top of a picture, and still get all the functionality.
 

isladogs

CID Moderator
Staff member
Local time
Today, 11:03
Joined
Jan 14, 2017
Messages
13,406
I had assumed without checking that the OP had done just that.

As you can modify buttons to have any image you like and have both text and images on buttons, I no longer see any point using images instead of buttons.

Indeed when I look at forms which use images acting as buttons as well as other images such as logos, it's not always obvious what to click!
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top Bottom