Suppress Error 3021: No Current Record in Access - vba

I have an Access form with an attached recordset. Controls in the header adjust the recordset filters. Sometimes those filters return an empty recordset, which is OK.
However when the recordset is empty and the user clicks on any control in the header (let's say to change the filter again), Access pops up an error box with 3021 - No Current Record.
I've been unable to find the source of this error in the code - however I've added an event trigger on the form - onError for the form itself.
Private Sub Form_Error(DataErr As Integer, Response As Integer)
MsgBox "caught error: " & DataErr
End Sub
That works! But I had expected this sub to replace the Access error. Instead my code is executed and the original error message pops up after!
I know there are many questions about getting rid of this error but most are related to taking a VBA action - this is about removing the error when no other VBA is triggered. Is there a way to get the Form_Error sub to not show the popup?

The simple solution is to use the Response parameter in the Form Error event. Setting it to acDataErrContinue tells it to continue and ignore the error.
You can see more examples on the doc page.

Related

Finding a syntax error in a large VBA/MS Access project

I have a MS Access database that displays an error message about a SQL syntax error on launch. "Syntax Error in query. Incomplete query clause." It also shows another error a few seconds after I hit "OK" on the first one.
Here's the two errors: https://imgur.com/a/PesjIFk
But it doesn't tell me where the syntax error is. There are SQL statements in a bunch of different places all over this project. This is a really large project and it wouldn't be practical to just look through all the code hoping that I notice an error someplace. How can I find out where this error is?
EDIT: Ok, so apparently you have to have a keyboard that has a "Break" key on it in order to even find where the error is. Wow. Fortunately I happen to have one. Here's the code that Access takes me to if I press break when I see the error message. This code is for a subform of another form. It highlights the first line (Private Sub Form_Current()).
Private Sub Form_Current()
If NumEnums > 0 Then
CurrentEnum = val(Nz(bit_value_edit.value)) 'Update CurrentEnum to the currently selected enum
Call UpdateEnumsLabel(Me.Parent![enums label]) 'Update label
End If
End Sub
...and here's UpdateEnumsLabel():
Public Sub UpdateEnumsLabel(ByRef label As Control)
If NumEnums > 0 Then
label.Caption = "Enums: " & CurrentEnum & "/" & NumEnums
Else
label.Caption = "Enums: 0"
End If
End Sub
The definition for CurrentEnum:
Public CurrentEnum, CurrentPort, CurrentFile, CurrentGroup As Long
I'm thinking that this error is unrelated to the code in Form_Current(), but Access is highlighting that line because the error happens when the form is opened. But the form doesn't contain anything that uses a query, so I'm confused as to what query Access has a problem with.
When the error Message pops up, Use Control+Break. It will take you to the line causes the issue.
You should also open a module and form the debug option in the VBA editor select "Compile All Modules"
And since it appears to happening on open/load, you can check both the macros and the main modules to find anything that triggers on AutoExec.
Often ctrl-break will hit the line where you errored out. However, in the case of multiple “events”, and code without error handling, then often the error occurs in the routine that called the code, not the actual code that caused the error.
What I would do launch the application (hold down the shift key to prevent any start up code, or forms running).
Now that you launched the application, but without forms or code running, then check for an autoexecc macro (if there is one, check what code it attempts to run).
If there not an autoexec macro in use, then check under file->options->current database. In this view, you can look/see/determine what the name of the start-up form is.
Once you determined the start-up form (or start up module/code) called from autoexec macro, then you can simply add a blank code module, and place in your code the SAME command that is used to start your application.
So let’s assume no autoexec macro, and you determine that the start-up form is frmMain.
So now, we can launch the application (hold down shift key to prevent any start up code from running). Now, type into a new “test” standard code module the following code:
Sub MyGo
Stop
Docmd.OpenForm "frmMain"
End sub
So you type in above code, hit ctrl-s to save the above code. Now with your cursor anyplace inside of the above code, you simply hit F5 to run.
At this point you hit/stop on the “stop” command in the debugger. At this point, you then can hit f8 to step to the next line of code. If the form in question calls other code etc., you still be able to single step, and “eventually” you hit the line which causes the error.
While the application may be large, a simple look at the start up forms (and huts then the start-up code) means that you ONLY really need to trace and look at the start up code – not the whole application.

microsoft access 2010: Private Sub Form_Load() not running, .setFocus not acknowleged.

So I am trying to create a login form for a database application and I used this article as my guide.
http://accesshosting.com/create-login-form-ms-access
I followed the steps to the best of my knowledge. However, in creating the vba code for the on click procedure for the "Ok" button for the login form. using the code in the webpage I receive an error on the Private Sub Load_Form part of the codeblock. Other users seemed to have issues with this part as well.
The code in which the error is thrown is:
Private Sub Form_Load()
Me.txtUserName.SetFocus
End Sub
.SetFocus is highlighted in this case and error message is shown below.
method or data member not found
I tried researching this question and I saw that there might be an issue with naming of the controls. However, the names all matched up. When i typed Me.txtUsername it popped up with intellisense. However the .setFocus didn't appear as I was typing it.
Not sure what's up. The datatypes for each field are as follows.
userName: text, userLogin: text, userID:number, password: text
Short Version: the form cant load and its because .setFocus isn't accepted as a method. Why?

How to check if clipboard is empty of text?

If I try to paste from an empty clipboard, I get an error. I would like to check if the clipboard is empty of text before pasting so that I can avoid this. How can this be accomplished? I'm aware it can be done through error handling, but I would prefer a method that avoids an error.
Edit -- Per request, adding code that creates the error and the error message:
Code that causes the problem:
Sub PasteFromEmptyClipBoard()
Selection.Paste
End Sub
Error message that I get:
"Run-time error '4605' This method or property is not available because the Clipboard is empty or is not valid."
Very important: You must first set a reference to the "Microsoft Forms 2.0 Object Library" (as in the attached screenshot below) before implementing this code. You may find that it is not an option when you scroll through the reference libraries. To make it show up, just add a form to the project (you can always delete the form later).
Sub CheckClipboard()
Dim myDataObject As DataObject
Set myDataObject = New DataObject
myDataObject.GetFromClipboard
If myDataObject.GetFormat(1) = True Then
'''There is text on clipboard, so it's safe to paste
Else
'''there is no text on the clipboard, so you may get error.
End If
End Sub

Weird Error: Hit ENTER key in listbox, get NULL error for bound recordset values

Access 2007 ADP/ADE.
I have a modal that is bound to a table. The modal has a single listbox control, and two buttons. One executes a subroutine of code that access values from the bound recordset, the other closes the modal.
Two methods of use:
Select a value in the listbox, and then click on the button to execute the subroutine, it works just fine.
Select a value in the listbox and then hit the ENTER
key on the keyboard twice, it attempts to execute the same
subroutine, but throws a NULL error the first time it tries to
access a value from the bound record.
So using method 1, a value like Me.RecordNumber will return 12345. Using Method 2, it will be NULL and error out. Interestingly enough, the selected value in the listbox itself is available using either method.
Not really a critical bug, but it is annoying. I have to teach folks to click buttons rather than just use the ENTER key on this particular modal. Has anyone seen this before? Any idea why values from a bound recordset are unavailable using method #2?
EDIT for clarification:
This error appears to be independent of any particular piece of VBA code. I've even created a new modal, bound a different table, and just tried to access a random datacolumn on that recordset. Same result.
Here is the snippet where the error appears. It errors out when attempting to grab the RecordType, a value that is never null.
Private Sub cmdAddFromTemplate_Click()
On Error GoTo Err_MyErrorHandler
Dim myTemplateName As String
Dim myDepartment As String
Dim i As Integer
'Did they provide a valid template name?
If Me.listTemplateToImport.ItemsSelected.Count > 0 Then
'Cycle through list and build criteria
For i = 0 To Me.listTemplateToImport.ListCount - 1
'Is this status selected?
If Me.listTemplateToImport.Selected(i) Then
'Grab selected template name
myTemplateName = Trim(Me.listTemplateToImport.ItemData(i))
End If
Next i
Else
'Warn and exit
MsgBox "You must select a valid template name.", vbOKOnly, "STEP: Action Canceled"
Exit Sub
End If
'Grab Bound Values
myDepartment = Me.RecordType 'NULL error here when using ENTER key.

Error on ActiveSelection.Tasks

Does anyone know what this means
Set oProjTasks = ActiveSelection.Tasks
I have a macro that generates status reports from MS project and exports them directly into MS Word. It is a slick tool when it works.
When I run it now it throws "runtime error '424': object required" at this point.
How do I fix this?
The code that you are displaying is a set statement, that is setting the object ProjTasks equal to the task that is selected in the message box. The ActiveSelection property returns a selection object that represents the active selection.
It could be that you are experiencing an issue where there are no items selected, in which case it will throw a trappable error code 424. There is a code snippet that you can modify from the MSDN that will work to prevent this type of error from occuring.
Here is the link to the MSDN article... just remember to not use this code verbatim, but modify it to work with your macro.
http://msdn.microsoft.com/en-us/library/aa169315%28v=office.11%29.aspx
You could try just wrapping the error check around the set statement. I've written a small macro on a non-empty project file:
Sub Testing()
On Error GoTo ActiveSelectionErrHandler:
Set oProjTasks = ActiveSelection.Tasks
If oProjTasks Is Nothing Then
MsgBox "No tasks in current project"
End If
ActiveSelectionErrHandler:
Set oProjTasks = ThisProject.Tasks 'or something like that
Resume Next
End Sub
This handles the error but as Steve has already expressed more work is required to integrate the code.
You will have to follow the code to make changes to handle oProjTasks being empty where it is expected to have some values. Otherwise you will see more errors perhaps where the oProjTasks is found to be empty.
Another alternative solution could be to launch the macro after selecting a project as the code you have quoted will work fine if something is selected.