"The data has been changed" error when editing underlying record in Access VBA - forms

I have a form in Access where I have 2 unbound multi-select listboxes, with some code to move items between them.
Each of the fields in the table which are shown in the listboxes are boolean values - if the value is true then the name of that field shows up in lstSelected, and if false shows up in lstUnselected.
The listboxes have a RowSourceType of Value List, and the value list is generated programatically by looking at the underlying record and constructing a string with the field names where the boolean values are true for lstSelected and False for lstUnselected.
On the form I have two buttons, cmdMoveToSelected and cmdMoveToUnselected. When I click on cmdMoveToSelected it changes the boolean value of the underlying field for any selected items in the lstUnselected listbox from false to true by executing an SQL string, then rebuilds the value lists for both of the listboxes.
I have all of this working just fine. If I do a me.lstUnwanted.requery and a me.lstwanted.requery then everything moves and shows up correctly, and the underlying fields are edited correctly, BUT when I click on anything else on the form I get the error:
The data has been changed.
Another user edited this record and saved the changes before you attempted to save your changes.
Re-edit the record.
Now I've found a way around this (jobDetailsID is the primary key of the record being dealt with):
Dim intCurID as Integer
intCurID = Me.JobDetailsID
Me.Form.Requery
Me.Recordset.FindFirst "JobDetailsID = " & curID
This requeries the form and then moves back to the current record, and this gets rid of the error, however it causes there to be a delay and the form to flicker while it opens back at the first record, changes back to the correct record and repopulates the list boxes.
Is there a way to do away with this error, or get it to trigger programmatically so I can catch it by turning the warnings off via vba?
Thanks in advance.

Maybe it helps not to bind the form to the table being altered by cmdMoveToSelected, but to a query that doesn't contain all the boolean fields. If cmdMoveToSelected alters one or more boolean fields, the record is changed, but the query result isn't. Not sure if it's sound though.
It also sounds a bit like a design problem rather than a form problem, storing options in boolean fields instead of into a related table.

Probably the best solution would be to not directly update the current record in the table while the Form is dirty. Instead, update the values of the fields within the form itself (Me!FieldName) as the items are moved from one List Box to the other, and let the form write those values back to the table as usual.

I seem to have fixed it, though the fix doesn't make a great deal of sense to me.
I added in a Me.Refresh to the button click code, after I had requeried the two listboxes and it appears to have stopped the message from coming up. However this only works when I have the JobDetailsID textbox visible on the form (though I expect this is arbitrary and any field-linked textbox would work).
Can anybody explain to me why this works? I'd like to understand fully when to use requery, refresh etc

I've had this sort of thing happen when I've left the form RowSource query hanging in place after converting the controls to unbound textboxes, etc. The general Form rowsource query (to bring in all fields I might possibly end up using) provides me with a query-list identical to the table fieldnames, making it simple to select them for control-names as needed. Works fine, but you have to remove the form rowsource query after all the names are matched-up. (After which DLookup and BeforeUpdate works for getting and storing values and changes.)

Related

Access Table Update from Form

I have a form linked to a table. I am trying to use the me.dirty function to see if the user changed anything. For now I put the code msgbox(me.dirty) in the form close button to determine what is happening. When some fields are changed i get true others i get false. For now I am only changing one field at a time. I have determined that if I look at the table before I close the form, if the table matches the form me.dirty is false. if not then me.dirty is true. this makes sense I dirty=true when the form does not match the table.
What I cannot figure out is why some field match the table and others do not until the form is closed. For example I have two check boxes one is always matching the table as soon as i check or uncheck it and dirty = false. The other one does not change the table until I close the form and dirty=true. Iv'e looked at all the properties of the two check boxes and they are the same.
I also have two drop downs that give me dirty=true and two text boxes that always give me dirty = false. (both cases when the field is changed.)
Any help would be appreciated as I am stumped right now.
OK I figured it out on my own. The ones where me.dirty=false had an event after update that put focus on a subform, this updated the table and then when clicked the button to run me.dirty, it was false.

Run VBA code after form load

I have this continuous form "Results" built on a "Search" query in Microsoft Access and I want to show a hidden text field "Number" (with its Visible property initially set to False) depending on the value of one query result written in a "Type" combo box. My code is currently as follows:
Private Sub Form_Load()
If Me.Type="Reclamation" Then
Me.Number.Visible=True
Else: Me.Number.Visible=False
End If
End Sub
This does not seem to work, however the code does fine when triggered by a change event. Adding, changing and deleting records are also disabled in "Results".
Should I run code from a macro, on the query or try another event (I've tried many events though with no success)?
Thanks for the help, and sorry for the noob question!
Continuous Forms behave differently than what you think. It cannot work with UnBound Controls. If the control Number is bound it will take effect on only the Record that has foucs. Using Form Current combined with Load should/might work, but again it will not be the best solution. Because it will be applied to all other records in the SubForm.
Example of what I mean is, if the form has three records. First Record has Type (a terrible field/control name) "Reclamation" then the "current" record's Number (again a very bad field/control name) will be hidden along with all the other record's Number, even if they have other Type's in their record.
I would suggest you to go with Single Form.

Access Form won't accept input in text boxes

So I have a form in Access.![acessform1][1]
Here are form properties
I got this database and have been making adjustments to the other forms. I gave the database back to the user and they reported an issue that this form is not acting properly, and the add order record function is not working anymore.
There's an Add Order Record button on the right side of the form that would usually add the record if the user manually entered input into some of the boxes. The old form you would have control of the values 1/0 for yes/no. You can enter text into the notes boxes.
This functionality doesn't work anymore, and I can't seem to figure out why. The form is identical to the original and no adjustments have been made except to the table that it pulls data from.
So the symptoms are:
User input is not accepted in text boxes.
The find CP functionality works with the drop down and reflects the records accurately but I cannot update the fields.
Also, the fields CP_Ref and Invoice date don't let me enter data anymore.
Also the Add Order Record was giving me an error that The it could not find the record specified, but I since copied the original form back into place and that error went away.
I think functionality wise this button should save the record after a field is updated and should add a new record.
Seems like all the controls are locked on the page.
Here is the query that pulls the data when I click the button that takes me to the Order form.
SELECT DISTINCTROW CPOrders.Cust, Customer.NAME, CPOrders.CP_Ref, CPOrders.Slsman,
CPOrders.Date_opn, CPOrders.CPSmall, CPOrders.InvIssu, CPOrders.InvNo,
CPOrders.InvDate, CPOrders.DueDate, CPOrders.ETADate, CPOrders.Closed,
CPOrders.Cust, CPOrders.Name, CPOrders.BuyerRef, CPOrders.ToCity,
CPOrders.ToState, CPOrders.ToCtry, CPOrders.ToPort, CPOrders.Supplier,
CPOrders.Origin, CPOrders.Product, CPOrders.GradeType, CPOrders.NoUnits,
CPOrders.Pkg, CPOrders.Qty, CPOrders.TotSale, CPOrders.TotCost,
CPOrders.GrMargin, CPOrders.[Sale$/Unit], CPOrders.[Cost$/Unit],
CPOrders.OceanCost, CPOrders.OceanNotes, CPOrders.BLadingDate,
CPOrders.USAPort, CPOrders.FOBCost, CPOrders.FASExportVal,
CPOrders.InlandFrt, CPOrders.CommodCode, CPOrders.Notes,
CPOrders.ProjCust, CPOrders.ProjValue, CPOrders.ContainerNumber,
CPOrders.Vessel, Customer.TERMS
FROM Customer INNER JOIN CPOrders ON Customer.[CUST_#] = CPOrders.Cust
ORDER BY CPOrders.CP_Ref;
I ended up checking several locations for the answer to this.
What it ended up being was a primary key issue.
Essentially when I got the updated database I put them into new tables, effectively destroying the dependencies, relationships, and established keys.
I reverted back to the old tables and found out the form worked properly.
The issue ultimately was that the primary keys were not defined as needed for the table to be updated.

Form visible in Design View, but blank in Form View

My form is showing up in design, but not form view. There are 700+ records.
In Design View -> Data, the Record Source is:
SELECT act.* FROM act;
This query shows all records in Query view.
There are no filters, and Allow Form View is set to Yes.
What is causing my form not to show?
EDIT: I'm noticing that the form is extremely wide. There isn't a width setting, but in Design view with window maximized I have to scroll way over to see the right edge. Would this have anything to do with it?
This link might help you. I remember having this issue and it was because of a read-only query.
Why does my form go completely blank?
Here is an excerpt that lists the main conditions why this could happen:
Condition (a) can be triggered in several ways. Examples:
- The form's Data Entry property is set to Yes. (This means the form shows no existing records, i.e. it is for entering new ones only.)
- The form has a Filter applied (or is opened with a WhereCondition) that yields no records.
- The form is based on a query where the criteria yield no records.
- The form is based on a table that has no records.
Condition (b) can be also be triggered by several things:
- The form's Allow Additions property is set to No.
- The form's Recordset Type property is set to something other than Dynaset.
- The form is based on a read-only query. (If you cannot add a record directly to your query, see Why is my query read-only?)
Auto_title_0 was set to 21" wide. Resizing that to the actual form width solved the problem.
On Design View of the Form click on "Details" which is just below the ruler and make sure that Visible is set to Yes on the Property Sheet.
Try this step.
Click to select Form.
Click on Auto Resize.
Change value to Yes.
Look into this
I also had the issue with a Form I used to input the strings for a query. The problem was that there were no records to answer query as I had changed it to an "Select Distinct" query to get rid of duplicate answers, but it also eliminated new record sets. Deleting the "Distinct" in my SQL solved the problem. Thanks for your help!
For me - the Form.Data Entry property was set to Yes.
Changing it to No shows the form again.
As for my case, I had open two forms.
Form B is link to Form A.
When Form B is opened, Form A is blank because it does not allow for data to be added.
When I close Form B and re-open (or refresh) Form A, the form fields appear.
Hope someone finds this helpful.. I faced this problem and solved by changing Pop On property (under the other tab) to No, and also changing the border style to "Dialog" in design view mode.
I also had this problem which wasted lots of time, but I was able to fix it.
The reason for me was that my target table was empty and I had primary keys set on it and had my controls in my form related to those fields.
So, Access was unable to show blank primary keys and was not displaying the form at all.
If you fill up your target table with some data, save and close the form and open it again the controls will be displayed.

Access comboxbox selection needs to change value in label control

On a form, I have a combobox, with the RowSource coming from a query (specifically, a calculated field in the query). When the user makes a selection, I want to update a label on the same form with a different column from that same query, but of course associated to the selection.
I'm fine with VBA and writing queries and whatnot, but I am not very familiar with Access forms.
By the way, I tried searching for an answer to this, but it was quite difficult because I don't know what this thing is really called that I am trying to do. A good link to a site explaining this would be perfectly fine (no need to write a bunch of stuff here if it already exists elsewhere).
In the After Update event of my combo box, cboUserID, I can set a label control, lblFoo, to the value of the second column in the selected row of the combo.
Me.lblFoo.Caption = Me.cboUserID.Column(1)
If your combo box is bound to a field in the form's record source, you may want to do that same operation from the form's On Current event also.
You can use the column property to refer to anything other than the bound column of a combo.
Rowsource: SELECT ID, SName, FName FROM Table
Me.MyCombo.Column(2)
This would return FName.
-- http://msdn.microsoft.com/en-us/library/aa224084(v=office.11).aspx
Me.MyLabel.Caption = Me.MyCombo.Column(2)