Avatar of Chi Is Current
Chi Is Current
Flag for United States of America asked on

Runtime Error: 2108

The SetFocus command in this IF statement results in a Runtime error #2108 - must save the field.  The statement is in the BeforeUpdate event of the 'ProductDesc' field.

I'd appreciate knowing how to make this work.


    If IsNull(Forms![frmNewProduct]![cmbVendor].Value) Then
        MsgBox "First enter a VENDOR!", vbCritical, "No Vendor"
        Cancel = True
        Me![ProductDesc].Undo
>>>        Forms![frmNewProduct]![cmbVendor].SetFocus
        Exit Sub
    End If

Thank you, Jacob
Microsoft Access

Avatar of undefined
Last Comment
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

8/22/2022 - Mon
Rey Obrero (Capricorn1)

better if you can upload the db..
Chi Is Current

ASKER

Private Sub ProductDesc_BeforeUpdate(Cancel As Integer)
'On Error GoTo Err_ProductDesc_BeforeUpdate
 
    If IsNull(Forms![frmNewProduct]![cmbVendor].Value) Then
        MsgBox "First enter a VENDOR!", vbCritical, "No Vendor"
        Cancel = True
        Me![ProductDesc].Undo
        Forms![frmNewProduct]![cmbVendor].SetFocus
        Exit Sub
    End If
 
End Sub

Open in new window

thenelson

Try this:
        Me![ProductDesc].Undo
        Me.Dirty = False
        Forms![frmNewProduct]![cmbVendor].SetFocus
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Chi Is Current

ASKER
Adding:   Me.Dirty = False   -> Run-time error #3058 Index or primary key cannot contain a null value.

Hmmmmm  this is happening in a new record......
Rey Obrero (Capricorn1)

you are attempting to do a validation..why do you need to force saved the record when validation fails?
thenelson

You also need to set focus to the form before setting focus to the control:

If [frmNewProduct] is a subform, you can use:
        Me![ProductDesc].Undo
        Me.Dirty = False
        Me![frmNewProduct].SetFocus
        Me![frmNewProduct]![cmbVendor].SetFocus
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
thenelson

You also need to set focus to the form before setting focus to the control:
        Me![ProductDesc].Undo
        Me.Dirty = False
        Forms![frmNewProduct].SetFocus
        Forms![frmNewProduct]![cmbVendor].SetFocus

If [frmNewProduct] is a subform, you can use:
        Me![ProductDesc].Undo
        Me.Dirty = False
        Me![frmNewProduct].SetFocus
        Me![frmNewProduct]![cmbVendor].SetFocus
thenelson

<Adding:   Me.Dirty = False   -> Run-time error #3058 Index or primary key cannot contain a null value.>

change
        Me.Dirty = False
to
        Me.Undo
Rey Obrero (Capricorn1)

me.undo will clear all the entry you made, are you sure you want to do this?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Chi Is Current

ASKER
OK -  The statement now reads:

    If IsNull(Forms![frmNewProduct]![cmbVendor].Value) Then
        MsgBox "First enter a VENDOR!", vbCritical, "No Vendor"
        Cancel = True
        'Me![ProductDesc].Undo
        'Me.Dirty = False
        Me.Undo
        Forms![frmNewProduct].SetFocus
        Forms![frmNewProduct]![cmbVendor].SetFocus
        Exit Sub
    End If

And results in the Run-time error #2108 'Must first save the field...

This is not a subform, all the text boxes referenced are on this form.

Thank you for your assistance here!
Rey Obrero (Capricorn1)

how is the form "frmNewProduct" related to the form you are runnning the codes from?

or what is the purpose of the frmNewProduct in the current operation?
Rey Obrero (Capricorn1)

like what i said in my first post
<better if you can upload the db..>
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Chi Is Current

ASKER
'Bout sez it all.  Thank you!  Jacob
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

"'Bout sez it all. Thank you! Jacob"

You are welcome ...

mx