Solved Add button produces No Current Record error! (1 Viewer)

Mike Krailo

Well-known member
Local time
Today, 10:43
Joined
Mar 28, 2020
Messages
1,032
This is probably an easy one for you but it is not making a lot of sense to me why this error is happening. The code on the ADD button is on subform of a main form and is obviously used to add records. The very first time the main form loads, I can press that ADD button on the subform and there is no error. If that new record is now deleted, and the ADD button is pressed again, that's when the "No Current Record" error occurs. Using the On Error Resume Next did not help at all either. So obviously, there are no records to start with. Going to a new record where there is none currently doesn't seem like it should generate that error. What's going on here?

Having stepped through the code, this error is generated prior to the On Click event firing (second time through after main form loads). No idea why this is happening!

Code:
Private Sub AddBtn_Click()
   If Nz(Me.Parent.JobDateClosed) <> 0 Then
      ' There is a closed date, so do not allow additions
      MsgBox "This Job is closed for adding records!"
      Exit Sub
   Else
      Me.AllowAdditions = True
'      On Error Resume Next
'      DoCmd.RunCommand acCmdRecordsGoToNew
      DoCmd.GoToRecord , , acNewRec
      Me!TaxTypeID = 1
      Me.AllowAdditions = False
   End If
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:43
Joined
Oct 29, 2018
Messages
21,358
Hi. Are you able to post a sample db?

Sent from phone...
 

Mike Krailo

Well-known member
Local time
Today, 10:43
Joined
Mar 28, 2020
Messages
1,032
No, not this one. I tried the decompile trick as well with no luck. I just made sure that there are error handlers for every routine on the form and subforms and nothing is catching this particular error which doesn't even have an error number displayed. It just looks like this:
1601239528569.png

I am assuming this is equivalent to Error 3201 since that is what is shown elsewhere on google searches about this but I'm not positive about it. It seems to be an error that cannot be trapped!o_O
Update: I just tried importing everything into a new database and still get that same error. Is this an Access Bug? I have M365 version of Access and just using the regular channel.
 
Last edited:

CJ_London

Super Moderator
Staff member
Local time
Today, 14:43
Joined
Feb 19, 2013
Messages
16,555
how are you deleting the 'new' record? After it has saved so exists in the table? or by hitting escape/undo, so the record has not actually been added to the table?

Also not sure whether going to a new record, then disabling allow additions is the right thing to do, since at that point, the record has not been saved.
 

Mike Krailo

Well-known member
Local time
Today, 10:43
Joined
Mar 28, 2020
Messages
1,032
CJ, thanks for responding. I am simply clicking on the record selector on the form and pressing delete. Let me examine the process in light of what you just mentioned though to be sure. So yes, the error happens whether the record is saved to the table or not. Just to take it out of the equation, I commented out the line that disables additions and the error went away!

The problem now is both the new record is visible along with the saved version of the record. When a new record is added, a field gets populated which essentially creates a new autonumber and the record is saved to the table (Me!TaxTypeID = 1). So your saying turning off AllowAdditions at that point is not a good idea, when is the best time to do so then?

This is what the subform looks like after the record is deleted. Notice that the record selector shows a triangle instead of the standard * symbol for a new record.

1601244926037.png


That looks like a bug to me. It should be showing a * instead of a triangle when there are no records and AllowAdditions is turned on. In any case, I do not want the AllowAdditions record to be visible on the form ever. It should write that one field, and allow the user to change other fields and delete the record if desired.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:43
Joined
Feb 19, 2013
Messages
16,555
The triangle just means that it is the currently selected record.

I'm pretty sure it is not a bug - just that you are expecting it to work in a particular way.

Suggest make the PK visible until you have this sorted - if it is a new record I would expect it to show '(new)'

if you don't want to see what has already been entered, suggest

a) set data entry =true
b) in your add button, use me.requery to hide the previous entry rather than the code you are using in the else part of your query
 

Mike Krailo

Well-known member
Local time
Today, 10:43
Joined
Mar 28, 2020
Messages
1,032
This might surprise you then, added the primary key in the upper left corner and it shows (New). But as you suggested, maybe the triangle is due to it being the only record left to select.
1601247479876.png


As far as using the Data Entry on the form, that won't work as seeing the previously entered records is part of the design. I know they may not be able to see all of the records if they enter more than three but showing none of them is definitely not an option.

I do think I see what is happening with the original error and original code. With the AllowAdditions turned off and no records in the form, there literally is No Current Record to select. So the question is, how do I go about handling that error without giving up on the way I want it to function? It would be nice to just trap the error in the handler and it would all work perfectly.

Here is some additional troubleshooting info on this:
If the ADD button on the form is Right-Clicked only, the error occurs! Then if I just left click normally on the ADD button, there is no error! That sure looks like a bug to me.

Another UPDATE on this:
I went to an older backup of the database that does not have this problem and it essentially uses the same code as above. The problem was one line of code that I added to turn off the scrollbars in the subform when the record count was greater than two. After commenting that code out, it all worked perfectly except now my scrollbar problem is back again. So if anyone knows a better way to handle the scrollbars problem, let me know.

Code:
Private Sub Form_Current()
   On Error GoTo ErrorHandler
 
   ' Prevent the scrollbars from being active when less than two records present
'   Me.ScrollBars = IIf(Me.Recordset.RecordCount > 2, 2, 0)
 
   ' Control Edits/Deletions via Job Closed Date
   If Nz(Me.Parent.JobDateClosed) = 0 Then
      Me.AllowEdits = True
      Me.AllowDeletions = True
   Else
      Me.AllowEdits = True
      If Not Me.NewRecord Then
         If DateDiff("d", Nz(Me.Parent.JobDateClosed), Date) > 1 Then
            Me.AllowEdits = False
            Me.AllowDeletions = False
         End If
      End If
   End If
   Exit Sub
 
ErrorHandler:
   Select Case Err.Number
      Case 3201 'No current record!
         Resume Next
      Case Else
         MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "Oops, we found an ERROR"
         Resume Next
   End Select
End Sub
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:43
Joined
May 7, 2009
Messages
19,175
just set the scrollbar to 0, when error occurs:
Code:
Private Sub Form_Current()
   On Error GoTo ErrorHandler
 
   ' Prevent the scrollbars from being active when less than two records present
'   Me.ScrollBars = IIf(Me.Recordset.RecordCount > 2, 2, 0)
 
   ' Control Edits/Deletions via Job Closed Date
   If Nz(Me.Parent.JobDateClosed) = 0 Then
      Me.AllowEdits = True
      Me.AllowDeletions = True
   Else
      Me.AllowEdits = True
      If Not Me.NewRecord Then
         If DateDiff("d", Nz(Me.Parent.JobDateClosed), Date) > 1 Then
            Me.AllowEdits = False
            Me.AllowDeletions = False
         End If
      End If
   End If
   Exit Sub
 
ErrorHandler:
   Select Case Err.Number
      Case 3201 'No current record!
          Me.Scrollbars = 0
         Resume Next
      Case Else
         MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "Oops, we found an ERROR"
         Resume Next
   End Select
End Sub
 

Mike Krailo

Well-known member
Local time
Today, 10:43
Joined
Mar 28, 2020
Messages
1,032
Sorry Arnel, that doesn't work. The error is not able to be trapped. Just so you know, the "No Current Record" error goes away also if I turn on the AllowAdditions as well. But having that on really makes the form look confusing to the users and they would rather see a new record that gets saved right away when they click ADD without the allowadditions showing that extra record with the * on the record selector after they start to type something in. Usually this isn't a problem on most forms, but it's a big problem on this one.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:43
Joined
May 7, 2009
Messages
19,175
maybe reinstate the AllowAdditions (Yes).
just Cancel the Addition of new record (based on the condition you set):

Private Sub Form_BeforeInsert(Cancel As Integer)
Cancel = 'What is Expression to cancel adding record
End Sub
 

Mike Krailo

Well-known member
Local time
Today, 10:43
Joined
Mar 28, 2020
Messages
1,032
The way they use this form is:
1. Click ADD to add a new record. The record gets saved right away so there is no reason to show the New Record along with the current record. Other wise the users get confused seeing what looks like two records. I have had the AllowAdditions turned off forever on this form and they are used to seeing just one record when they click on ADD. The scrollbar problem has been an issue forever as well which is why I am trying to fix that.
2. Then if they want to delete a record, they just click the record selector and press Delete key.

So the only way I can leave AllowAdditions on is if there is someway to hide the New record that shows when AllowAdditions is turned on. It feels like I'm in a catch 22 with this one.

Right now I am leaning towards disabling scrollbars altogether and just using a few buttons at the top for navigating the records. Maybe program the up/down arrow keys to move on record at a time.

I would rather have the scrollbars working if possible but it's not looking very promising.

BTW, I tried this and it did not work (still shows the New Record):
Code:
Private Sub Form_BeforeInsert(Cancel As Integer)
   Cancel = IIf(Me.AllowAdditions = True, Me.AllowAdditions = False, Me.AllowAdditions = False)
End Sub
 

Mike Krailo

Well-known member
Local time
Today, 10:43
Joined
Mar 28, 2020
Messages
1,032
Here is the final update on this for everyone else's benefit. The "No Current Record" error was happening at the form level which is outside of the regular event code. That's why all the error handling in each event was not working to trap this particular error. Placing the following code in each subform's On Error event traps the error and now everything works as I really needed it to. So relieved. Thank you all for your support on this.

Code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
   If DataErr = 3021 Then Response = 0
End Sub
 
Last edited:

Users who are viewing this thread

Top Bottom