MS Access: How do forms communicate values to each other? - ms-access

I have a form (FORM-A) that requires the user to select a vehicle.
The user should click on a button on FORM-A that say's select vehicle.
A selection form (FORM-B) should open where the user can select a vehicle.
The selected value should be communicated back to FORM-A.
How would you accomplish this in MS Access 2010?
FORM-B is a continuous form that contains a picture of the vehicle and some other information.

From what I understand from your question, you want formB to open a kind of pop-up. When the pop-up is closed, its result is put somehere in the calling form.
Solution proposal:
a) open FormB using syntax docmd.openform "formB", windowmode:=acDialog.
This will prevent execution of the next lines until formB is closed or hidden.
b) in the OK button of FormB, just hide the form, do not close it.
c) when code resumes in formA, you can now
check if formB is still open. If not, it's been cancelled
read the value in hidden formB (still open), then close formB
Otherwise, you could also have formB to update a control in formA before closing. But I don't like that approach because then formB is not reusable, and it creates an unnecessary dependency between formB and formA.

I am not sure why you would need a separate form for this - just have the first textbox be a listing of all the records of vehicles in the database, where you would select one, and the rest of the vehicle information is auto-populated from the vehicle table, but not copied into your parent table. Of course, I am not sure of your table structure either, so there might be a reason for this method that isn't apparent to me.
The benefits of the method above is that if you add more vehicles, your selection box is automatically updated - and you keep the forms you have to load to a minimum (always a good performance move)

You can reference the forms in this manner forms!formName!controlName.
Once you see how this works you will be able to fool with it to get it working with your existing setup. Let’s use 3 controls a text box on Form-A, an image on Form-B and a text box on Form-B. The text box on Form-A will be named txtVehicle, the image on Form-B will be named imgVehicle and the text box on Form-B will be named txtVehicleName.
You can set the name of a control within properties. When you click on imgVehicle it will put the value from txtVehicleName into txtVehicle.
You will have to do a little coding - it's easy though if you have not done it before. Under properties for the image you will see events. If you click on the "On Click" event you will get a drop down list. One of the choices will be [Event Procedure] - choose that. A little button with 3 dots on it will also show up at the end of the row. Click it and you should be taken to a code window with some code like this in it.
Private Sub imgVehicle_Click()
End Sub
Here is where you put your code. Something like this should work. This is it in its most simplistic form.
Private Sub imgVehicle_Click()
Forms!Form-A!txtVehicle=forms!Form-B!txtVehicleName
End Sub
Now although that will work, there are a few things that we should be doing in this method that we are not. We should reference Form-B directly since we are in it, we should verify that Form-A is in fact open.
Private Sub imgVehicle_Click()
If currentproject.allforms(“Form-A”).isloaded then
Forms!Form-A!txtVehicle=me!txtVehicleName
End if
End Sub
Hope that helps

You can create an instance of formB within formA and control it. Below is the VBA code for formA. When clicking on a button on formA, you create a new instance of formB and give it focus. At the same time, you can set properties for its controls. You can use this approach to set the right picture in your control on form B. Hope this helps.
Example:
Option Compare Database
Dim fB As Form_FormB
Private Sub btnA_Click()
Set fB = New Form_FormB
fB.SetFocus
fB.tbxB.Text = "Some text sent from A to B!"
End Sub
If you want both forms to be visible all the time, I suggest using a subform with the list of all the vehicles or just details for the one that the user selected.

Related

MS Access: Easiest way to implement a "check all" box in a form with a datasheet subform

I have a form whose main purpose is to contain a datasheet subform (the form provides a common visual theme across the pages as well as navigation buttons and sometimes selection criteria for the subform).
One of my subforms has a check box.
The user behavior will be to:
Open this form
Filter the datasheet according to some specification
Do something off-line involving, e.g., the ten visible line items
Check the field (yes/no field) on those ten line items to indicate that it's been done
What I'd like is for the user to be able to check one box that then checks all the visible boxes.
The challenge is that it must only be the visible ones, i.e. dependent on what filters are in place.
I tried to create a checkbox in the main form labeled "Check All" which runs code something like:
me.sfrmReport.Control.reported.value = me.chkCheckAll.value
But unfortunately, this only checked the active row, not all of them. I know I could run a query on the underlying table, but it would need to replicate the user filters.
In fact, what might be even cooler would be a "Check highlighted" box that checks the highlighted rows only.
But I think "Check all" would be better.
I tried to find this, but my searching skills failed me. I'd have thought this was something somewhat common, but perhaps I'm thinking about this incorrectly. I do note that there's a sum function which automatically sums just the filtered/visible rows in a datasheet, so the notion of interacting with just the visible rows is at least somewhat standard.
Well, the check all box certainly should not be on a "row". So one would assume that say beside the combo box, you place a check all box. (can't imagine that the button or code would be place on the row(s)).
So, say beside the combo box in your screen shot, you have a button called check all.
The code behind that button would/could be:
dim rst as dao.RecordSet
set rst = me.MySubForm.Form.RecordSetClone
rst.movefirst
do while rst.EOF = false
rst.edit
rst!ResultReported = true
rst.update
rst.moveNext
loop
me.MySubForm.Form.Refresh
So above will operate directly against the sub forms data source (current filtered recordset).
I "think" from your screen shot, the top part of the form (with combo) is the main form, and the sub form you have displayed is the sub form.
So, of course you will have to replace the name of the controls I used above, and also the field name for the check box.
So the format to get that recordset is:
me.MySubFormControlName.Form.RecordSetClone
and to fire off a refresh of the sub form after the code runs is of course.
me.MySubFormControlName.Form.Refresh
Note that intel-sense should work and help you. if the code does not compile, then don't try and run it until it compiles.

Using AfterUpdate event on a control in one subform to requery a control on another subform

I have an Access 2010 database set up with using the native Navigation Form interface feature.
Using this very helpful table, I've been able to use vba on multiple ocassions to reference forms throughout my database. However I seem to be having difficulty in this one instance.
On the Navigation form I have a number of main tabs. Clicking on one of the main tabs brings up a number of sub-tabs that have various forms attached to them.
On one of these sub-tabs, I have a main form (main) attached to it. There is a subform (subform1) within that main form containing a control text box (control1). Within subform1 there is another form (subform2) containing an unbound text box (control2) displaying a value that is calculated using a TempVar.
I want to use the AfterUpdate event on control1 to requery subform2 in order to update the calculation in control2, or simply requery control2.
Using Refresh and/or Requery on the AfterUpdate event for control1 does not do anything to control2.
Currently I have a button on subform2 that users can click which refreshes the form and updates the value in control2. The user can also hit F5 and refresh all the forms with the same result.
My goal is to take away the manual aspect and have the AfterUpdate event for control1 to update control2.
Any help would be much appreciated as I haven't been able to find anything online that works and I've been kicking tires at the problem for over a month now.
**** UPDATE **** I did not correctly describe the layout of my forms. subform1 and subform2 are actually both nested under main. subform2 is NOT nested under subform1 as I originally described above.
Wow. Now I feel like an idiot. Tackling my problem again with the update I provided above allowed me to solve it.
All I needed to do was requery subform2 by referencing it this way:
[Forms]![HOME]![NavigationSubform].[Form]![subform2].[Form].Requery
Where "HOME" is the name of my Navigation Form.
For anyone with the same issue that hasn't renamed their Navigation Form I believe the syntax would be:
[Forms]![Navigation Form]![NavigationSubform].[Form]![subform2].[Form].Requery
Thank you to #Erik von Asmuth and #June7 for your patience and help.

Setting focus on a sub form

I have two sub forms embedded in control (the one that looks like a set of folders allowing you to change from tab to tab.. Sorry I don't know the name in English)..
Both sub forms are embedded on one sheet, I would like that immediately after executing a routine, I wrote on one of the subforms, that the set focus then goes to the other sub form.
For some strange reason I can't get it to work. The sub form and field I'm trying to go to are frmObjectives03 and Effective_date.
I tried:
Forms!frmObjectives03!Effective_date.Form.SetFocus
but it doesn't work.
I would appreciate any insight.
Thank you,
The control you speak of is a Tab control.
Because frmOjective03 is opened as a subform, it is not part of the Forms collection. Instead you can use Forms!MainForm!frmObjectives03.Form!Effective_date.SetFocus from anywhere, or Me!frmObjectives03.Form!Effective_date.SetFocus from within the main form or Me.Parent.Form!frmObjectives03.Form!Effective_date.SetFocus from the sibling subform.
If these are not working for you, please check the name of the subform control in your main form (because it may be Child17 instead of frmOjective03). Open the main form in design view, click on the subform ONCE and look in the Properties->Other->name.

Access form's Detail's event only triggers on first record

I'm designing a Form in Access 2003. My goal is that when the user double-clicks on a record (aka Detail), a second form opens. This is easy enough to do when I start from scratch.
But of course, I'm not starting from scratch. I've been working on this for a few hours now, have added some conditionally formatted textboxes, fields, etc, and don't want to start over if I can avoid it.
I don't know if I turned on/off some obscure property, but I've been working on this form for a few hours now and the Double-Click event of the Details section only triggers when I double-click on the first record or any other with conditional formatting.
To troubleshoot this, I've gone so far as to comment out all the other VBA script except this:
Private Sub Form_Load()
Me.Application.DoCmd.Maximize
End Sub
Sub GotoFrmDetails()
MsgBox "You Double Clicked Me!"
End Sub
Even with the VBA script reduced to just these lines, the DoubleClick event only triggers on the first record and no other. Can anyone tell me what wrong? I'd rather not have to rebuild this from scratch.
I find the click events of form sections to be more trouble than they are worth (click event doesn't pass through to the form if user clicks on a control or label, etc). What I use instead are transparent command buttons.
Just add a command button to your form and set its Transparent property equal to True. Then you can use the Z-order to make fine-grain changes if necessary (send to front/back, send forward/back).
They can be hard to find once you make them transparent, so be sure to give them descriptive names.
If you want the user to be able to double click anywhere in the detail section, just make the transparent command button's height/width match the section height/width.
Move your code from the Detail section DoubleClick event to the transparent command button's DoubleClick event and you should be good to go.
I'm wondering if the rows are getting their Locked property set to Yes, or their Enabled property set to No during your condition formatting.

MS Access linking combo box to form

I'm trying to link a field, which I have a drop down box in, to a form. I have a list of about 10 forms to pick from. I'm sure this is simple, but I'm just overlooking the obvious.
Simple example code for the opening from the EventName() event (change EventName based on what event you are using):
Private Sub Combo0_EventName()
If Combo0.Value = "Form1" Then
DoCmd.OpenForm "Form1", , , acFormAdd, , , stLinkCriteria
ElseIf Combo0.Value = "Form2" Then
DoCmd.OpenForm "Form2", , , acFormAdd, , , stLinkCriteria
End If
End Sub
Depending on what you are trying to do will determine the event to use, but running the open form command in that event based on the combo box value will get you where you need to go.
Let me understand this right, you have a form with a combo box. When the user selects an option from the combo box, one of 10 other forms open?
How about, in the On Change Of event of the combobox, open the second form?
To clarify:
You open up Access and are presented with a form that contains a combo box filled with some values. You select one of the values and want to bring up a different form and then fill that form out based on some criteria.
The sub routine Combo0_EventName() will take care of opening a form based on the field you select, just put the code in the Combo0_AfterUpdate() routine. To get to that right click on the combo box in design view and select Properties, then click in the "AfterUpdate" event to get [Event Procedure] to show up then click the ... button to get the VB editor.
If you want to fill out the form you open up automatically the method depends on what you are trying to do. You probably want to have another combo box on the first form or on the other forms you are opening that allows you to select your query parameters. Lets say you are opening a form that displays someone's address. You would want a combo box on that form to select from the names in your table and when you select a name it would update a textbox with the address information. Or you could put your query parameter selection on the main form and tie that data to all of the forms so you can go through the forms with the same entry in the table, useful for something like order entry.
If that sounds like what you are trying to do we can move forward down that path, or if it is not please clarify. Can you describe an example of what you are doing? That would be helpful.
I'm sure we can get you going with this.
Im trying to make local tax forms. We do several different local forms and just want the person perparing the return to be able to put in the basic information in a form, which will then automatically pop up which form they are to use, completely filled in. This way all they have to do is enter the basic info in a standard sheet and the local form automatically gets completed. I have made it so when i person picks the municipality from the drop down box it automatically list the form name it needs to go in. But i want it to be simpler than that for them. NE Suggestions?
That makes sense, each form probably has different tax codes, addresses, etc. on it.
I'll see what I can do over the next couple days and set you up with something to get you going.