MS Access 2003 findfirst method doesnt work in Load() event of form - forms

After a few days of frustration, I have decided to present this issue here for resolution. Searches through countless forums and help sites have not presented any similar issue. The following details the problem with a FindFirst method in MS Access 2003:
Tables and Forms: Customer Info Table > Service Details Table as a one-to-many relationship with referential integrity and cascading updates enabled (there may be more than one service detail for each customer). A search form allows user to select ‘customer’ and ‘service provider’ (a field in table Service Details) sending that information to open a main form/subform to enter service data related to ‘customer’ using ‘service provider’ (text string) as criteria. There may be more than one ‘service provider’ per ‘customer’ (i.e. each Service Detail entry may have a different ‘service provider’ for the same ‘customer’).
Objective: The main form (with subform, both based on select queries) should open to the specified record/subrecord.
Current Method: A combo box on the main form (not the search form) allows user to select appropriate ‘service provider’ using the FindFirst method (combo box created by wizard). This works but not what is intended. The ‘service provider’ criteria is initially selected in the search form and passed into the main form (as a global variable for now), so the combo box selection on the main form becomes redundant and could cause confusion for the user (they could accidentally enter data for the wrong service details).
Problem: I assumed copying the code from the AfterUpdate() event of the combo box to the Load() event of the main form would be adequate, but it does nothing, no errors, just loads the first available Service Details record instead of the selected one using ‘service provider’ as criteria.
Tests: I have inserted message boxes to check the criteria value for the ‘service provider’ selected, and everything seems ok, both in and out of the FindFirst method inserted in the Load() event of the main form. I have tried a DLookup(), DoCmd.Findrecord and setting the default of the combo box to the criteria passed from the search form, but still only the first ‘service provider’ record is displayed, instead of the one selected in the search form.
Code:
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[Service Provider] = '" & [g_serviceProvider] & "'"
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
This produces no errors, and hovering over the g_serviceProvider variable with the mouse in debug mode shows the correct string value. This code works perfectly in the AfterUpdate() event of the combo box, but does nothing when inserted into the Load() event of the form, even though the values are indeed passed (checked using message boxes to display data at each stage of the execution).
I am at a loss of why this only works for the combo box but not in the Load() event of the form. I suspect the problem is in the recordsource of the form (select query), as the recordsource of the combo box uses a SELECT expression based on the same query as the form. I have had no success with coding the recordsource to use with the FindFirst method within the Load() event, as that produces errors (cannot find any relevant examples to work from). Any ideas? Thanks in advance.

Ok, I finally figured it out. The actual problem was the form loading the Current() event multiple times when opening the form to a specific record. This was happening on pretty much every form where an existing record is being displayed. It seems to be an artifact of the form opening and record selection of MS Access 2003 (don't know about newer versions, but I have read several other posts concerning this issue).
There is a dirty work around, by using a count of the number of times the Current() event runs when a form is opened. I have found that for a "virgin" form (blank form ready to accept input with blank record source (table)) the Current() only runs one time. If there is any existing records, but the form is blank ready to input a new record, the Current() will run twice. When displaying a record that already exists that must populate a form, the Current() will run three times before all of the data loads enough to make the selection through coding rather than through a combo box (which loads with the control). Subform Current() seems to run one more time than the parent form.
To check to see if and how many times any event occurs, insert a simple message box in the suspected block of the form's VBA code.
Creating a simple counter, coded in the Current() event block of the main form, solved the problem.

Related

Update form in access when form is not open using VBA

I developed a form in Access that allows users to search for records that match their parameters. The form has about five combo boxes that contain values which a user can search. A query is set up that pulls the values from the combo boxes. An associated form, "F_FilterResults", lists the results of the query as a datasheet that display the results of the search. I created an AfterUpdate event on each combo box on "F_FilterResults" so that after I update the combo box with a search parameter the query and form would refresh itself. Here's some sample code from the AfterUpdate event in the combo boxes on "F_FilterResults".
Private Sub CompanyState_AfterUpdate()
Forms![F_FilterResults].Requery
Forms![F_FilterResults].Refresh
End Sub
This only works however if the "F_FilterResults" form is open. When it is closed I get Run-time error '2450' message that indicates that the "Microsoft Access cannot find the referenced form 'F_Filter.' How can I have this code run when the "F_Filter" form is not open? Note I do not want the form to open right after you select a search parameter from a combo box since the user may want to add more search parameters to the other combo boxes. If I program "F_FilterResults" to open on loading the search form the user may X out it and would not know what the error message means.
You can open a form hidden. That is the "easy" answer. However the need to do this means that the way you go about achieving your result is not the best.
The way I would do it is that instead of updating a form that pulls values off the combo boxes I would generate a string that can be applied as a filter to the form when it is opened.

Open form to record seleced from double click on same record in datasheet form

I've got a pair of forms that are both based on the same data table. One is a single form with a better layout of all the onfo, the other is a datasheet. I want to be able to double click on the project number in the datasheet form to open the details single form to that same record.
I've done it on a simplified practice db, but when I try it in my current db it opens the second form, but to a new record entry. This form is one that was a template that someone else downloaded and modified for our purpose, so maybe there is something stored somewhere else that is interrupting the filtering.
I deleted a couple of macros that I thought might be interfering, made sure that the form properties are cycling all records and that on form load it isn't directed to a new record. I can't figure out where else there is something preventing the form from being filtered for the selected record. Here's the code I've used in the datasheet form:
Private Sub combined_DblClick(Cancel As Integer)
DoCmd.OpenForm "frmTaskDetailsExisting", acNormal, , "SLRtaskID = " & Me.SLRtaskID
End Sub
Anyone have an idea what I'm missing here.
Have you considered doing this with a split Form? You will have the Detailed view per record and the dataseet.
If you really want two independent forms due to your needs, your code looks ok. But two things may be happening:
The value in Me.SLRtaskID is not correct. Put a breakpoint in the code and check it.
There is code in frmTaskDetailsExisting that moves it to a new record when it is opened, regardless to the filter.

Combobox governing subform possibly updating tables

I have a form, the form is bound to a query which returns only one field. On that form is one combo box, bound to the field from the query, a subform object and some other unimportant stuff. The Master-Child relationship between the Form and Subform is set to that same field. The intent is that dropping down the combo box allows me to add information on the subform pertaining to that record in the combobox (it's a list of classes in a school, for context). This is done through a form and subform as I have various different tasks that need to be done for each class - the intent is to be able to select a class on the main form and then use command buttons on it to select which subform is opened depending on which task I wish to perform.
However, the Combo box appears to be trying to update a table - I'm not sure which one as the error message isn't specific:
The changes you requested to the table were not successful because
they would create duplicate values in the index, primary key or
relationship. Change the data in the field or fields that contain
duplicate data, remove the index or redefine the index to allow
duplicate values and try again
appears if I select any value other than the first one from the combo box and then click in the first text box on the subform. However, I can click in the subform with the first entry in the combo box selected and add data successfully, I've checked and it is appearing in the underlying tables.
It seems to me, as a relative novice in Access, that the combo box is attempting to update the underlying data source when it is changed, though it has no macros. I would assume there are items in the properties of the form or the combobox that prevent that from happening but I can't find them. That is just a guess as to what's happening, though, and I could be wrong.
It's possible that this is related to this question but I could be mistaken there as well. Regardless, the Form shouldn't be able to update/edit/add records but if I set Allow Edits in its properties to "No" I am unable to actually select a value from the combobox - I have set the other "Allow" properties to "No" without a problem.
If you change anything in the main form and then click on the subform then Access will try to save the data in the main form automatically. Maybe you can try to temporally exchange the combo box with a text field for testing. That should help you to clarify the problem.

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.

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

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.)