开发者

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

I have a form (FORM-A) that requires the user to select a开发者_Python百科 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

  1. check if formB is still open. If not, it's been cancelled
  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜