Run VBA code after form load - forms

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.

Related

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.

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

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.

Query subform, not working when used through navigation form

I'll keep this as clear as I can:
My database has two tables:
Clients
Contacts
They have a relationship: For each client (Which is a company, for your understanding) there can be any number of related contacts.
The database has three relevant forms:
SearchFrm
NavigationFrm
ContactsSubFrm
SearchFrm is a form that reads from the Clients table, and has a combo box that, according to chosen record (name), displays all it's other fields in their respective text boxes.
Inside SearchFrm, ContactsSubFrm appears as a datasheet subform, that displays all contacts related to the chosen record in the form, (using a query of Contacts.[Workplace ID])=[Forms]![SearchFrm]![ID]
NavigationFrm serves as a means of navigation between forms. For now, it's only for SearchFrm, but more will come.
And now, the problem: The query that ContactsSubFrm runs doesn't work inside NavigationFrm, when run, I get an input window for [Forms]![SearchFrm]![ID] every time it's suppose to run. This only happens inside navigation.
I'm pretty sure this is because the SearchFrm form itself is closed, and has a problem working through a navigation form, but I can't think of a solution.
Thank you very much.
I was looking for the answer to the same question, so I thought I'd put my solution here in case anyone else comes looking for it...
As stated above, you have to reference the field within your form within your navigation form in your query (as opposed to just the field within your originally created form.)
The best way to do this is:
Open your navigation form.
Open any other form.
Access the expression builder from the other form.
Find the field in your navigation form that you need to reference. Mine looked something like this:
Forms![NavigationForm]![NavigationSubform].Form![Field]
Copy and paste into your query
Since your form moved location (now inside a navigation form), You could try to modify your query from: [Forms]![SearchFrm]![ID] to: [Forms]![NavigationFrm]![SearchFrm]![ID]
I solved this by changing the reference in the query itself to
[forms]![frmNavigationForm]![NavigationSubform].[Form]![ID]

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

Autofill a subform field from a main form field

I have a form with two subforms, both of which are continuous. Each form has a field to accept the name of the person who created the record. Almost every time, the same person will be creating all of the records, so it would be really convenient if the fields would autofill once the main record has been set.
I've tried several approaches to this, but none seem to work quite right (e.g., the first of the continuous forms won't autofill because it came into existence alongside the main record). This is Access 2003.
OnCurrent, OnClick, etc...
If IsNull(Me.MyField) or Me.MyField = "" Then
Me.MyField = Me.Parent.MyRelatedField
End If
It may be best to set the default value of the control in the after update event for the control. This means that it will fill with whatever the previous value was.
Me.SomeText.DefaultValue= """" & Me.SomeText & """" ''Text