Link to home
Start Free TrialLog in
Avatar of Chi Is Current
Chi Is CurrentFlag 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
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

better if you can upload the db..
Avatar of 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

Avatar of thenelson
thenelson

Try this:
        Me![ProductDesc].Undo
        Me.Dirty = False
        Forms![frmNewProduct]![cmbVendor].SetFocus
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......
you are attempting to do a validation..why do you need to force saved the record when validation fails?
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
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
<Adding:   Me.Dirty = False   -> Run-time error #3058 Index or primary key cannot contain a null value.>

change
        Me.Dirty = False
to
        Me.Undo
me.undo will clear all the entry you made, are you sure you want to do this?
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!
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?
like what i said in my first post
<better if you can upload the db..>
ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
'Bout sez it all.  Thank you!  Jacob
"'Bout sez it all. Thank you! Jacob"

You are welcome ...

mx