How to handle Access "Run-Time Error 3021 - No current record" after DoCmd.Requery? - vba

I have been having a quite annoying issue on my access project.
It is an Access 2007 - 2016 file.
My database has two queries that I intend to use to filter data to feed combo-boxes.
On one of my forms I have done that with success. The first list is fed from a table, and the succeeding ones are fed by filtering their respective table based on the previous list selection.
Here is a screenshot of my form that worked out.
And here is part of the code that goes with it:
Private Sub frm01ListSetor_Click()
DoCmd.Requery (qry01_frm01ListArea)
Me![frm01ListArea] = Null
DoCmd.Requery (qry02_frm01ListEquip)
Me![frm01ListEquip] = Null
Me![frm01TextID] = Null
End Sub
Private Sub frm01ListArea_Click()
DoCmd.Requery (qry02_frm01ListEquip)
Me![frm01ListEquip] = Null
Me![frm01TextID] = Null
End Sub
Here are the queries that goes along with the code above:
qry01_frm01ListArea
SELECT tblAreas.ID_Area, tblAreas.NomeArea
FROM tblAreas
WHERE (((tblAreas.SetorArea)=[Forms]![frm01_AddEquip]![frm01ListSetor]));
qry02_frm01ListEquip
SELECT tblEquipamentos.ID_Equipamento, tblEquipamentos.NomeEquipamento
FROM tblEquipamentos
WHERE (((tblEquipamentos.SetorEquipamento)=[Forms]![frm01_AddEquip]![frm01ListSetor]) AND ((tblEquipamentos.AreaEquipamento)=[Forms]![frm01_AddEquip]![frm01ListArea]))
ORDER BY tblEquipamentos.ID_Equipamento;
The code above works just fine. It filters data to previous selection accordingly and it also clears the data and selection of the "righter" lists every time another value is selected on the "lefter" ones.
So that was a form I created to help me out on populating my equipment database.
Let us get to the problematic twin now:
I created another form for Maintenance Orders of these equipment.
This one must be more user friendly since other people will use it. Therefore I used Combo-boxes for value selection instead of List Boxes. I say this before hand because it is the only thing I could notice that differs the new form from the one above.
Here I have this:
Screenshot of troubled form
After the expanded Combo-box on image 2 is selected I should run the following code:
Private Sub frm02SetorOM_Click()
DoCmd.Requery (qry04_frm02CBArea)
Me![frm02AreaOM] = Null
DoCmd.Requery (qry05_frm02CBEquip)
Me![frm02EquipamentoOM] = Null
End Sub
Private Sub frm02AreaOM_Change()
DoCmd.Requery (qry05_frm02CBEquip)
Me![frm02EquipamentoOM] = Null
End Sub
It is basically the very same thing I have done on the first form but with different variables.
But I get a "Run-time error 3021: No current record" every time I select a value on my "Setor" field.
The crash happens at the "DoCmd.Requery (qry04_frm02CBArea)" line.
Here is the query for this form's Area field (qry04_frm02CBArea):
SELECT tblAreas.ID_Area, tblAreas.NomeArea
FROM tblAreas
WHERE (((tblAreas.SetorArea)=[Forms]![frm02_SubmitOM]![frm02SetorOM]));
But the thing that is confusing me the most is that when I click "End" on the Run-time error dialog box, my Area combo-box - that was empty before - even with the error gets the proper options like if it actually "requeried".
How can I fix this issue?
Thank you in advance.
Btw, this is my first Access project so please consider that on the response.

Related

Button to check for missing values in a MainForm and Subforms in MS Access

New to Access (still), have only basic VBA skills.
I've got 3 subforms (subfrm_PackingSteps1 , subfrm_MetalDetection and subfrm_Weights - the first 2 are continuous and the other one is single form) within a main form (frm_daily_packing_record) that users go through and input data. The user should be able to input data in no particular order, and only at the end there would be a button to confirm that the user is ready to save this form.
I'd like to have this button on the main form that checks each control (in main form and subforms) for empty values. I found and adjusted a code to check the recordset of one of the continuous forms (see below), but I can't figure out:
how to include a code that checks each control instead of manually adding all of them (I've used a function before that utilises the Tag property, but can't add it to this)
how to keep the button in the main form while checking the controls/recordsets in the other subforms.
Thanks in advance.
Private Sub ConfirmBtn_Click()
Dim blnSuccess As Boolean
blnSuccess = True
Me.Recordset.MoveFirst
Do While Not Me.Recordset.EOF
If IsNull(Me.pc) Or IsNull(Me.InnerP) Then
blnSuccess = False
Exit Do
End If
Me.Recordset.MoveNext
Loop
If blnSuccess = True Then
MsgBox "You may proceed to save this record"
Else
MsgBox "You still have some empty fields to fill in!", vbCritical + vbOKOnly, "Empty Fields!"
End If
End Sub
I personally don't like this because it is too code-heavy and can easily break when the form is modified.
Instead may I suggest doing it slightly different, for each field have vba code .ondirty or .onupdate and do the validation checking right as the user is actually on that field.
This has 2 benefits, it is creating the validation when you are creating each form field and it STOPS the user right when their first mistake or bad data is entered. The last thing I want is to enter 50 fields, scroll to the bottom, submit fails then scroll back and try to find where the mistake was. If validation is done while the user it doing the actual data entry, when you get to the bottom you should have valid data and the submit should succeed without further testing.
Less code to debug and timely messages to the user if an error is caught!

Microsoft Access VBA If statement

I have a dropdown menu that has three options "Open, Assigned, Closed" I have three rows in a table in my database, one for openTime, assignedTime, and closedTime.
My dropdown menu is connected to a row in my table called Status which is swapped between the three options "Open, Assigned, Closed". When the ticket is created in my website, it creates an open time so I don't have to worry about that, but when the user selects assigned and closed I would like it to on change send data to the correct cell.
So if the drop down is swapped from open to assigned it already changes the status cell to "Assigned" in my database but I would also like it to change the assignedTime cell's data to the exact time and date. Same for closed.
I think this is simple I'm just a bit confused on the process. Im using this site for info on IF statements
I have my code that creates and adds my date
me.MyDateTimeColumn = now()
I have a few questions
1) In my IF statement how do I say the value of the selection for example if the user selects "Assigned' in the drop down menu how do I say thats the info I want my IF statement looking at?
On the site linked above this is there example for an if statement:
Sub AlertUser(value as Long)
If value = 0 Then
AlertLabel.ForeColor = "Red"
AlertLabel.Font.Bold = True
AlertLabel.Font.Italic = True
End If
End Sub
On this do I need to include the Sub AlertUser(value as Long)(using my own variables) If so what would my sub be?
and does the If value = 0 Then refer to the value of the selected comboBox?
2) Can I insert an else if as I would in other languages, do I need an end else if?
I am using a microsoft access form, and using the VBA code section for this. MY database is in a sql server connected to the form.

MS-Access Form shows Number, not Name on load

I got a Database, where I apply to a name a Main and Subgroup.
When I enter a MainGroup f.e. Granades, just subgroup elements like "attack-granades" etc. should be shown.
In genereal it works by writing in this into the MainGroup-Combobox at my Form.
Private Sub MunHauptgruppeRef_AfterUpdate()
Me.MunUntergruppeRef.Requery
Me.MunUntergruppeRef.RowSource = " SELECT UnterGrpNR, UnterGrpName FROM tbl_UnterGruppen WHERE UnterHauptGruppenNr = " & MunHauptgruppeRef.Value & " ORDER BY UnterGrpName ASC"
The Problem is, if I load the datas in my form again, it just shows the related Number to the "Sub-Combobox data" 1( f.e. 35 for Attack-Grenade ) , but not the Name itself. After I reselect the entry in my Main-Combobox(Grenade), it shows the right sub-data which was saved. 2
Tried Requery on Form_Load or Requery of the Combo-Boxes itself. nothing helped so far.
Made some Video3
You need to add the code from Private Sub MunHauptgruppeRef_AfterUpdate() to your Form_Current event, this will refresh your combo box row source to the current value of your MunHauptgruppeRef combobox as you cycle through your records. Also you need to call the Me.MunUntergruppeRef.Requery after you've set the Me.MunUntergruppeRef.RowSource.
You can also add a check when your in a New Record, for the code not to run on the Current Event. See here https://learn.microsoft.com/en-us/office/vba/api/access.form.newrecord

MS Access VBA - First Record In List is Overwritten on Form Close?

I just learning how to create forms in Microsoft Access 2013 and using VBA programming to control but I'm having an issue I don't quite understand.
I have created a form with a List Box where the source originates from a query that contains the following fields the Query Builder in order from left to right:
|ParentNumber|ParentName|ChildNumber|ChildName|
|------------|----------|-----------|---------|
|------------|----------|-----------|---------|
|------------|----------|-----------|---------|
Some fields from the Query are hidden with a Column Width of 0".
I also have 4 Text Box below the List Box corresponding to the appropriate ParentNumber, ParentName, ChildNumber, and ChildName values.
When I select a record in the List Box, it populates the data to the appropriate Text Box.
When the form first loads, the first row in the List Box is selected:
Private Sub Form_Load()
On Error Resume Next
DoCmd.GoToRecord , , acFirst
Me.List = Me.List.ItemData(0)
End Sub
The issue is that if I select a different row, then close the form, and reopen the form, the first row in the List Box is overwritten with the last selected row before the form is closed.
Even if I start out with any other row selected initially when the form opens, the first row is always overwritten by the last selected row when the form is closed.
The following subroutine handles the update of the Text Box data:
Private Sub List_AfterUpdate()
Dim rowIndex As Integer
rowIndex = Me.List.ListIndex
Me.textBox_ParentNumber = Me.List.Column(3, rowIndex)
Me.textBox_ParentName = Me.List.Column(4, rowIndex)
Me.textBox_ChildNumber = Me.List.Column(6, rowIndex)
Me.textBox_ChildName = Me.List.Column(7, rowIndex)
End Sub
I found a somewhat similar problem to mine, but I tried the suggested solution, which didn't seem to work: MS Access - First record in table is overwritten on form close
I'm completely baffled as to what could cause this based on the code above.
What's my issue?
Thanks.
Few things
Private Sub Form_Load()
'remove this so you can see errors.
'On Error Resume Next
' this goes to the first record of the *form*, not the list.
' you might want this, or not.
DoCmd.GoToRecord , , acFirst
' ItemData returns the data in the *bound column* of the list,
' the data from one specific column. the list is set to that
' data every time the form loads. not what you want; remove it.
'Me.List = Me.List.ItemData(0)
End Sub
This should get rid of the problem. What you want to do next is another question.
Your form should be bound to a record set (table or query).
I had same problem, After hours of verification -
In my case - the problem was that the MainForm , And SubForm (table) - was binded to the same recordset. I just Un-Binded data RS from MainForm.

How to allow a combobox to be left blank//skipped on a form

I am relatively new to Access and any SQL and VBA I have picked up is self-taught, so I would really appreciate answers that are not too heavily laden with technical terms...that said, I am having an issue with allowing comboboxes to be left blank on a form if the user chooses not to input data. Also, I am using Access 2016.
Initially the problem I ran into was that if a combobox was entirely skipped and left blank, or if the user selects the combobox and then tries to move on without making a selection from the list, they got the error "You tried to assign the Null value to a variable that is not a Variant data type," and were unable to move on to any other fields on the form or to save the record being entered.
I found this article that details a possible solution, but I can't seem to get it to work. I made an unbound combobox, set the Row Source to:
SELECT EmailID, PersonalEmail FROM EmailT UNION SELECT "<N/A>","<N/A>" FROM EmailT
ORDER BY PersonalEmail;
where PersonalEmail is a field of type short text and the EmailID is an autonumber. I also followed the article's steps for formatting the combobox (column width, etc.) and set it to Limit to List = Yes.
Here is my exact code:
Private Sub Combo62_AfterUpdate()
If Combo62 = "<N/A>" Then
EmailID = Null
Else
EmailID = Combo62
End If
End Sub
Private Sub Form_Current()
If IsNull(EmailID) Then
Combo62 = "<N/A>"
Else
Combo62 = EmailID
End If
End Sub
< N/A> now shows up on my list, but if it is selected I get the error: "The value you entered isn't valid for this field. For example, you may have entered text in a numeric field or a number that is larger than the FieldSize setting permits."
Access's debugger highlights the line:
EmailID = Null
but I am not sure what steps I should take now to try and fix this.
I am completely open to other methods of allowing the combobox to be left blank if someone knows of a better way to do this also. For all I know, I am missing something really obvious! Thanks in advance for any insight!
EDIT: Thanks for your help guys, I never did figure out what exactly the problem was, but I got some advice from another forum to rethink my database design so this ended up being a null issue--it's all totally different now!
If the recordsource for your form contains a query with one-to-many relationships this error may come up. Try to use only the base table as the recordsource for the form. Use subforms if you need to display related data. The rest of the code may then be unnecessary.