No Current Record - Before Code Even Runs (1 Viewer)

andrewmrichards

Registered User.
Local time
Today, 16:53
Joined
Mar 15, 2012
Messages
18
Hi All

I have a problem that's been baffling me all afternoon.

The concept is that a customer may place a recurring order, such as 5 items per month every month. In addition, some months they may require additional items

I have a form - frmOrdersMain, bound to tblOrderGroups which has a PK field called lngOrderGroupID. The information here is about the "main" order, such as the related customer and the date the order was placed.

It contains a sub form control containing a form called fsubOrderItemsContainer, bound to tblOrders, containing a PK of lngOrderID and a FK of lngRelatedOrderGroupID. This form shows the date that this part of the order is due for despatch, along with any notes etc.

This form also has a subform, called fsubOrderItemsGrouped, which shows the items being despatched as part of the particular OrderID selected in fsubOrderItemsContainer. On this form is a button called cmdNewOrderItems which allows a user to add new items to this order.

If there are existing items on this order, the code behind this button works perfectly. If not, I get the "No current record" error. However, I don't get told "Runtime error 3021: No current record", I just get a dialog box stating "No current record" and just to add to my joy a link asking "Was this information helpful?" - presumably thanks to someone at Microsoft with a sense of humour :)

There are several weird things about this.

1. The error occurs before the code executes. This means that even if I try adding "On error resume next" to the code, I still get the error. If I add a breakpoint to the first line of code - the line beginning Private Sub - I STILL GET THE ERROR! I click OK on the error message (the only choice) and THEN the breakpoint stops execution.

2. After I click OK on the error message, the code then works fine - the form I want (frmOrderItemDetails) pops up, allows me to assign items to the order and works perfectly.

3. If I click OK on the error message, then wen frmOrderItemDetails appears I cancel it without adding any items, when I return to this form there are, of course, still no items. But now, if I click on Add Items again, it works perfectly, with no error! But if I navigate off to a different order, then return and click Add Items again, once again I get the No Current Record error.

Someone please save my sanity!!!!

Anyone?!

Thank you so much.

Here's the code:

Code:
Private Sub cmdNewOrderItems_Click()

'Note the current order num as this is used by frmOrderItemDetails
'If the order part has been created but there are no items on it,
' this form will have no order ID yet as there is no current record,
' but the parent (container) form will have the order ID, so use that.

If Me.Recordset.RecordCount > 0 Then
    'Used for filtering frmOrderItemDetails, and for allowing user to return
    'to this order if they navigate elsewhere.
    NoteLastID "CurrentOrderID", Me.lngOrderID
Else
    NoteLastID "CurrentOrderID", Me.Parent.lngOrderID
End If

DoCmd.OpenForm FormName:="frmOrderItemDetails", windowmode:=acDialog
'Requery list of items to reflect changes made in frmOrderItemDetails
Me.Requery

End Sub

And in case it's relevant (I think not, but you never know), here's the code for NoteLastID:

Code:
Sub NoteLastID(Optional TempVarName As String, Optional TempVarValue As Long)
    ' Comments:Called in the click events of Nav form buttons etc to note the last ID being unloaded
    ' Params  : TempVarName - the name of the temp var name to populate. If not provided, default for main loaded form is used
    '           TempVarValue  - the value to store. If not provided, the ID on the main loaded form is used

    On Error GoTo ErrHandle

If Len(TempVarName) > 0 Then
    TempVars.Item(TempVarName) = TempVarValue
Else
    Select Case Form_frmFrame.ChildForm.Form.Name
        Case "frmCustomersMain"
            TempVars.Item("LastCustomerID") = CLng(Form_frmCustomersMain.lngCustomerID)
        Case "frmContactsMain"
            TempVars.Item("LastContactID") = CLng(Form_frmContactsMain.lngContactID)
        Case "frmOrdersMain"
            TempVars.Item("CurrentOrderGroupID").Value = CLng(Form_frmOrdersMain.lngOrderGroupID)
        Case "frmQuotesMain"
            TempVars.Item("CurrentQuoteID").Value = CLng(Form_frmQuotesMain.lngQuoteID)
    End Select
End If
    


ExitHere:
    On Error Resume Next

    Exit Sub

ErrHandle:
    'Log the error
    Call LogError(ErrorNum:=Err.Number, ErrorMessage:=Err.Description, _
        ErrorSource:="modFormDisplay  - NoteLastID &  - Line: " & Erl, _
        DisplayMessage:=False, _
        ErrorNotes:="")
    
    Resume ExitHere


End Sub
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 16:53
Joined
Sep 12, 2006
Messages
15,710
out of interest, why would you expect to see the runtime error number?

MS error messages just show text. If you want the number, you have to use a error handler?

with regard to this code

Code:
Select Case Form_frmFrame.ChildForm.Form.Name
Case "frmCustomersMain"
TempVars.Item("LastCustomerID") = CLng(Form_frmCustomersMain.lngCustomerID)
Case "frmContactsMain"
TempVars.Item("LastContactID") = CLng(Form_frmContactsMain.lngContactID)
Case "frmOrdersMain"
TempVars.Item("CurrentOrderGroupID").Value = CLng(Form_frmOrdersMain.lngOrderGroupID)
Case "frmQuotesMain"
TempVars.Item("CurrentQuoteID").Value = CLng(Form_frmQuotesMain.lngQuoteID)
End Select

if there is no current record, then all of these clngs will fail. clng of a null will fail, I think

I would try some of these

a) put a error handler round this block (and indeed any other "sensitive" piece of code)
b) step through the code with a breakpoint
c) add nz to the clng statements, to set them to a default value


The other thing is - if you have a recordset with no items, and with no possibility of adding items (ie, non-updateable recordset, or allowadditions disabled) you get a curious state where there just is no current record, and lots of things work strangely. Maybe this is a possibility.
 

andrewmrichards

Registered User.
Local time
Today, 16:53
Joined
Mar 15, 2012
Messages
18
Hi

Thanks for the response.

I don't understand why you say that I wouldn't expect to see error numbers. Without error handling, I'd expect to see this: (sorry - won't let me post links or images, so imagine a dialog box with the title "Microsoft Visual Basic", then two lines of content, the first saying something like "Runtime error '13': and the second saying "Type mismatch")


With regard to your other points, you're right, I would expect to get an error (invalid use of null) if converting Null to Long, but this code isn't running - this code is called by the click event procedure, which itself won't run until the "No current record" error has been displayed.

With regard to stepping through with a break point, as I said in my OP, "If I add a breakpoint to the first line of code - the line beginning Private Sub - I STILL GET THE ERROR! I click OK on the error message (the only choice) and THEN the breakpoint stops execution."

So the error is somehow occurring before the code begins to execute - which also ties in with the error message not being displayed in the usual VBA error dialog box - in some way it's not a VBA error. It IS, in the sense that if I comment out all the code in the procedure, I no longer get the error, but it's not in the sense that the error is generated before the break point on the Private Sub... line even executes.

With regard to your error handling suggestions, I will add the error handling code wnen I know that the normal behaviour is for the code to work, so that in the meantime I get to see any issues. But I have tried putting error handling code in place as part of my tackling the problems, and it made no difference. Again, as I said in the original post: "The error occurs before the code executes. This means that even if I try adding "On error resume next" to the code, I still get the error."
:mad:

Andrew
 

smig

Registered User.
Local time
Today, 18:53
Joined
Nov 25, 2009
Messages
2,209
Do you realy open an Order before you add any item to it?
If you do it's stupid as you might end with an empty order

I suggest you put some stops and check your variables
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 16:53
Joined
Sep 12, 2006
Messages
15,710
the reason i said you would not expect to see an error number, is that MS runtime errors just say

"no current record" (for instance, but do not give an error number). You will only get the error number if you use a error handler code to specifically display the error number)

if you try clng(somecontrol), and there is no current record - you will get a "no current record".

you may be able to do

clng(nz(somecontrol,"0")) maybe, which will guard against nulls, but will still not manage a no current record error

I am not sure 100%, but I think that you will only get a "no current record" in a form with no records, and no "new record" row - which will only be a form with "allow additions" set to false, or with an non-updateable record source.

i am pretty sure you could detect the no current record

on error got to norecord

select case
etc

norecord:
msgbox("suitable error message")
 

Users who are viewing this thread

Top Bottom