UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Run Time Errors, Access 2016    
 
   
ollyhutsy
post Nov 6 2019, 10:33 AM
Post#1



Posts: 64
Joined: 16-October 19
From: United Kingdom


At the moment I have got some code to stop a form opening if there are no records within that form. However i am receiving run time errors. In one case i am filtering a form with a button and if this is pressed with no control source in a text box i get a run time error and this shuts access down when it is used with a run time application. The errors I'm receiving are:

Run time error 2501
Run time error 2427

I am wanting to simply display a message when this happens.
Go to the top of the page
 
dmhzx
post Nov 6 2019, 10:46 AM
Post#2



Posts: 7,115
Joined: 22-December 10
From: England


Some thoughts.
1) Trap those specific errors and pop up a suitable message.
2) - And probably preferable, in the Form Current event , check the values in the text box and disable the button if it shouldn't be pressed.

In your case, form current may not be the right event to run the validation from.

In a runtime environment, Access will shut down if it finds an unhandled error, so you might well need a lot of error trapping.

Go to the top of the page
 
ollyhutsy
post Nov 6 2019, 10:58 AM
Post#3



Posts: 64
Joined: 16-October 19
From: United Kingdom


I have read into error trapping but I am not 100 percent sure the best way to go about it in my case. Have you got any suggestions/examples?
Go to the top of the page
 
dmhzx
post Nov 6 2019, 11:05 AM
Post#4



Posts: 7,115
Joined: 22-December 10
From: England


OK I see you are quite new to posting. Much of the stuff on line can be confusing
Here is what I do within most code section

Do the Dims

On Error Goto Trap:

Put all your cod in here.

Just above where you do any cleanup such as setting objects to nothing add a label
CODE
CleanExit:
On error resume next  (This is in case your cleanuo code treis to close something that isn't  open)


Just above End Sub
add

Exit Sub
    select case err.number

    Case 2501 , 2427
       display a message
      Resume Next ( or Cleanexit: depending on what you want to do)
    Case else
        msgbox err.number & err.description
        resume CleanExit
        Resume  (note: The code will never get here, but you can break into the code while developing, Ctrl F9 and the F8 to see the line with the problem)
    end select

And then u=you reach your end sub



Let me know if you need more guidance on this
This post has been edited by dmhzx: Nov 6 2019, 11:12 AM
Go to the top of the page
 
projecttoday
post Nov 6 2019, 11:37 AM
Post#5


UtterAccess VIP
Posts: 11,281
Joined: 10-February 04
From: South Charleston, WV


You can test with Dcount or use the on no data event to see if the form should be opened or not and avoid error-trapping code (which could cause you to trap a legitimate error).

--------------------
Robert Crouser
Go to the top of the page
 
ollyhutsy
post Nov 7 2019, 03:18 AM
Post#6



Posts: 64
Joined: 16-October 19
From: United Kingdom


I am confused to where this code will go exactly? and also would i use clean exit if i wished to just stop the script?
Go to the top of the page
 
ollyhutsy
post Nov 7 2019, 04:10 AM
Post#7



Posts: 64
Joined: 16-October 19
From: United Kingdom


Private Sub Command40_Click()
CleanExit:
On Error Resume Next
DoCmd.OpenForm "Milling Parts to Complete", , , "[WO NO]= '" & Me.WO_No & "' AND [Milled Part]= -1 Or [WO NO]= '" & Me.WO_No & "' AND [Further Milling] = -1 "

DoCmd.Close acForm, "Jobs to Complete"
End Sub

then on my milling parts to complete form in open i have this


Private Sub Form_Open(Cancel As Integer)
Me.TimerInterval = 15000
If Me.RecordsetClone.RecordCount = 0 Then
MsgBox "No Jobs Found"
Cancel = True
End If
End Sub

however this ends in both forms closing. I am wanting either my original form to stay open on error or re-open when no jobs found.
Go to the top of the page
 
dmhzx
post Nov 7 2019, 06:26 AM
Post#8



Posts: 7,115
Joined: 22-December 10
From: England


The ClaneExit: line is at the end of your active code, and just above any cleanup code you have. (I don't see any)

You really should give your controls a meaningful name. So for example command40 would be better if called cmdFrmMilling


If the button is on the Jobs to complete form, it's closing because you're telling it to.


Since you've moved on to the better solution of coding to avoid the error, you may well not need the method I proposed.

And On error resume next in essence says "Ignore all errors, and don't tell me about them, just move on to the next line.

Personally I would use .eof rather than record count. I only use dcount if I want to know how many records there are.

CODE
If Me.RecordsetClone.eof = 0 then
(or even me.recordset.eof)

But that is purely personal preference, and makes no actual difference to how the code runs
Go to the top of the page
 
ollyhutsy
post Nov 7 2019, 07:01 AM
Post#9



Posts: 64
Joined: 16-October 19
From: United Kingdom


yes it is on the jobs to complete form and i do want it to close the form if there is records in my milling jobs form, however i want it to stay open if record count = 0. I have tried all sorts but cant seem to get it to work. And yes i'm very new to visual basic and especially error handling.
Go to the top of the page
 
projecttoday
post Nov 7 2019, 07:15 AM
Post#10


UtterAccess VIP
Posts: 11,281
Joined: 10-February 04
From: South Charleston, WV


But error handling is out at this point, right?.

I'm not sure what it is you're doing exactly at this point but usually for data entry forms checking to see if things are correct is done in the before update event of the form. A form/subform setup may complicate the matter.

--------------------
Robert Crouser
Go to the top of the page
 
ollyhutsy
post Nov 7 2019, 07:28 AM
Post#11



Posts: 64
Joined: 16-October 19
From: United Kingdom


What i am trying to achieve is to close the jobs to complete form if the milling jobs form is opened. But if there is an error because there isn't any records on the form then leave the form open.
Go to the top of the page
 
BruceM
post Nov 7 2019, 07:48 AM
Post#12


UtterAccess VIP
Posts: 7,993
Joined: 24-May 10
From: Downeast Maine


For error handling, an excellent commercial option at a very good price is MZ Tools. Among other things, it adds customizable error handling with one click. I can't begin to guess how many hours of coding it has spared me over the years.

Some errors are irrelevant. For instance, in some coding situations you will get a notice that the Open Form action has been cancelled, so you can ignore that specific error number if the intent was simply not to open the form.

A general approach to error handling is:

CODE
Private Sub SubName()

On Error Go To ProcErr

' Code here

ProcExit:
  Exit Sub

ProcErr:
  MsgBox "Error " & Err.Number & " (" & Err.Description & ") " & _
         "in SubName , Form_frmMain"
  Resume ProcExit

End Sub

MZ Tools can add all of this in one click, including the procedure name and module (including form or report) name. To be clear, I have no stake in MZ Tools.

There are also homemade ways of doing something similar. The Articles section of UtterAccess Wiki has at least two articles about error handling. Another thing it can do is to add line numbers. The message box part of the error handling can then be like this"
CODE
  MsgBox "Error " & Err.Number & " (" & Err.Description & ") " & _
         "in SubName , Form_frmMain, Line " & Erl

Without line numbers it will show Line 0 for the error.

Error handling has an important runtime role, but is also a valuable debugging tool. I tend to use line numbers when I am having trouble identifying a problem. Knowing the specific line in a specific procedure can often show you the problem at a glance.

None of this is to dispute other things that have been written, but rather to provide a little more detail and make a recommendation.
Go to the top of the page
 
BruceM
post Nov 7 2019, 08:04 AM
Post#13


UtterAccess VIP
Posts: 7,993
Joined: 24-May 10
From: Downeast Maine


To test for an open form and take other action depending on whether that form has records, something like this:
CODE
If CurrentProjects.AllForms("frmMilling").IsLoaded Then
    If Forms("frmMilling").RecordsetClone.RecordCount > 0 Then
        DoCmd.Close acForm "frmJobsToComplete"
    End If
End If

I have lost track of where you would be running this code, but if it is in frmJobsToComplete you can just do this to close the current form:

DoCmd.Close acForm, Me.Name

Usually it is best to avoid generating the error rather than handling a specific error number, but both techniques have their place.
Go to the top of the page
 
ollyhutsy
post Nov 7 2019, 09:48 AM
Post#14



Posts: 64
Joined: 16-October 19
From: United Kingdom


tried using the code in the current of my jobs to complete form.

Private Sub Form_Current()
If CurrentProjects.AllForms("Milling Parts to Complete").IsLoaded Then
If Forms("Milling Parts to Complete").RecordsetClone.RecordCount > 0 Then
DoCmd.Close acForm, "Jobs to Complete"
End If
End If
End Sub

run time error 424.
Go to the top of the page
 
BruceM
post Nov 7 2019, 10:23 AM
Post#15


UtterAccess VIP
Posts: 7,993
Joined: 24-May 10
From: Downeast Maine


Error 424 is Object Required. Try putting square brackets around the form name. Spaces and special characters other than underscores in object names can cause problems. You can use Me.Name to close any current form, rather than naming the form.

Is it possible that the Milling form will be opened in between looking at jobs to complete records? I ask because I wonder if the Current event makes the most sense. Maybe just run it once in the Open event, which can be cancelled.
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    11th December 2019 - 02:55 AM