No Current Record. on Button Click

I am developing a Microsoft Access 2010 application for Windows 7 environment.

I have a form that has a 'New Record' button on it.  The form is bound to a table.  I set the form up so that in order to insert a new record the user has to click the 'New Record' button.  I do this because I want to prefill some fields and do other work before the user starts typing.

This works perfectly except when there are no records to begin with.  Then when the user clicks the 'New Record' button they see a 'No Current Record.' dialog.  If they click 'OK' it goes away and everything works fine.  It's just annoying and confusing to the users.

I have tried to trap this error, but it happens before the first line of code in my OnClick event.  My assumption is that because there are no records yet, MS is warning about that fact.  But I'm not trying to enter data at that point.  I'm just trying to click a button.

Any ideas how I can trap/hide the error dialog?
koughdurAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rey Obrero (Capricorn1)Commented:
post the codes behind the "New Record" button click event.
0
koughdurAuthor Commented:
The 'No Current Record.' dialog comes up before it gets to the first line of this event.  It also comes up before any code I have in the OnCurrent event.


Private Sub btnMtNew_Click()
  On Error Resume Next
 
  Me.AllowAdditions = True
  Me.DataEntry = True
  DoCmd.GoToRecord , , acNewRec
  Me.REQ_UID = Forms!FRM_MT_REQUEST.REQ_UID
  Me.MT_UID = getNextUID("MT_TRANSFER", "MT_UID")
  mtUidSaved = Me.MT_UID
  Me.MT_STATUS = "In Process"
  isSaveCancelled = False
  If hasNoValue(Me.LABOR_CHARGE_NO) Then
    Me.LABOR_CHARGE_NO = Forms!FRM_MT_REQUEST.LABOR_CHARGE_NO
    Call LABOR_CHARGE_NO_AfterUpdate
  End If
  Me.MT_TYPE.SetFocus
  Me.btnMtSave.Enabled = True
  Me.btnMtCancel.Enabled = True
  Me.btnMtNew.Enabled = False
  Me.btnMtDelete.Enabled = False
  Me.btnMtEmail.Enabled = False
End Sub
0
Rey Obrero (Capricorn1)Commented:
<The 'No Current Record.' dialog comes up before it gets to the first line of this event.  It also comes up before any code I have in the OnCurrent event.>

hmm, better upload a copy of the db
0
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
If you comment out the On Error Resume Next line ... what happens ?

Also .... your RecordSource may be suspicious.  It it a Table or Query (or SQL Statement). If SQL ... can you add a new record independently of the Form ... directly in the RecordSource ?

mx
0
Gustav BrockCIOCommented:
First, remove  Me.DataEntry = True

Then, when presetting the field values, set the DefaultValue first, then goto the new record, and don't set any values of the fields. This leaves the record as new until the user starts typing.

/gustav
0
koughdurAuthor Commented:
My database is quite intricate and contains company sensitive data that I can't give out and don't want to replace with fake data.

I'm going to create a really simple database with the same basic structure and see if I can recreate the problem then upload that db.

Thanks for the responses so far.
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
First ... did you investigate this I posted above:

Also .... your RecordSource may be suspicious.  If it a Table or Query (or SQL Statement). If SQL ... can you add a new record independently of the Form ... directly in the RecordSource ?

OR try what Gustav suggested ?

mx
0
koughdurAuthor Commented:
For errors, I tried three things:  No error trapping, the ON ERROR RESUME NEXT, and full error trapping.  In all three cases the error occurred and was not trapped.  Seemed to happen before it even got to the OnClick code.

The error is somewhat intermittent.  I have multiple forms that I navigate between by clicking on their tabs.  These are completely separate forms and not tab controls within a form.

The form is based on a simple table.  No a query or SQL.

One form has requests.  Each request can have multiple transfers.  The transfer form is set up to show only the transfers of the request that is currently selected.  If I click on the Request Form tab and move to a request that has no transfers, then click on the Transfers Form tab and click 'New' that's when I get the error.  If I create a new transfer, then delete it, then click 'New' I don't get the error.

I created a real simple database with just two form and two tables, but can't recreate the error.  That's all the more info I have for now.
0
Rey Obrero (Capricorn1)Commented:
<but can't recreate the error. >

just like I suspected, something wrong going on with your forms, but sorry I can't make guesses that is why I wanted to see your db.
0
koughdurAuthor Commented:
Gustav,

Thanks for the suggestions.

I got rid of the Me.DataEntry.  All that happened is that after I got the 'No Current Record' error and clicked through it, I got another error because my code was expecting the form to be in Data Entry mode.

As far as not setting values, the whole point of this button code is to preset values.  I am not using AutoNumbers for various good reasons.  I am using this 'New' button code to get the next available number and fill the corresponding field for the user along with prefilling other values.  What I'm doing works perfectly well except for the odd case I have tried to explain.
0
koughdurAuthor Commented:
The following code gives me the 'No Current Record' error when I click the 'New' button once, but not when I click it the second time:

Private Sub btnMtNew_Click()
  suppressCurrent = True
End Sub

I think the error is being generated because of the state the form is in.  Are there properties associated with the state of the form that I can query and modify if necessary?  Something like the Dirty property for records?
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
"Seemed to happen before it even got to the OnClick code."
Try trapping the Error in the Form Error Event ....


Private Sub btnMtNew_Click()
  If Me.Dirty = True Then
       ' do something - maybe a message to Save or Cancel
  Else
      suppressCurrent = True ' maybe this or whatever code

   End If
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
koughdurAuthor Commented:
Joe,

I added a Form_Error event.  The event fired before showing me the 'No Current Record' dialog.  In the Error Event I printed out the value of Me.Dirty which was False.  I also got Err.Number which was 0 and Err.Description which was blank.

So thanks very much for suggesting this.  I now know it is an error with this form and not the calling form, but I am still at a loss of how to handle this.
0
koughdurAuthor Commented:
Joe,

I did something odd and actually read the documentation on the Form_Error event.  I found that the DataErr value was 3021.  And when the error is 3021 I set Response to acDataErrContinue and the dialog goes away.

This solved my problem.

Thank you so much!
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
Good news, but we still don't know what caused the error :-(

Anyway ...
For future reference you can use AccessError (DataErr) to display the Description in a MsgBox
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.