469,287 Members | 2,426 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,287 developers. It's quick & easy.

Pre populating a new form when it opens

I have a database which has a number of tables one of which holds patient details tbl-Patients. A second table tbl-Events with a many to one relationship with the patients table based on customer ID number and which holds data on events eg clinc visits or operations (each record is for a separate visit). The events are entered in a form called frm-Events New.

I often need to enter a new event planned for a future date once I have completed the event form for the current patient visit. I have a button which opens a new frm-Events New. I have a combi box which is used to enter the patients name and this populates other fields using the column method from the combi box. I wouls like to have the new form pre populated from the form for the current visit but cant work out how to do it.

Help pleaseeeeee its driving me bonkers lol.
Aug 24 '10 #1
9 8105
201 100+
Medlar, the problem you have is that when you open the form frm-Events on a new record you have lost the ability to transfer data from the controls on the form in its previous state.

I think there are two ways to get around this.

You could create global variable to hold the data in the controls so you can then transfer them back to the form frm-Events on a new record.

You could also use a Recordset to get data from the table by just using a primary key which you would have to store on a global variable.

However, someone else might have a better solution.
Aug 24 '10 #2
Thanks for your reply

I have not worked with this approach before and so not sure how to start. Can you help?

Also will this solution work across the database as I have other instances where I open a second form from another and would like it prepopulated with some data from the previous form (I cant do this using a field match with a button control as the new data to go in the new form and unique key for that data are not created until the form is open if you see what I mean?)

Aug 24 '10 #3
32,173 Expert Mod 16PB
Typically, you don't want two forms (or even two instances of the same form) open concurrently. This can cause all sorts of issues for you to have to deal with later on, especially if it gets used later in a multi-user environment.

An approximate alternative would be to enter the new data into the same form. This may be facilitated by setting the .DefaultValue properties of all (most) of your bound controls to the values you anticipate being required (Mainly the same as the current record but presumably with a different date). You may choose to reset these .defaultValue values in the Form_AfterInsert() event procedure, depending on your actual requirements.
Aug 24 '10 #4
201 100+
As I mentioned there maybe a better solution.

If you have a lot of controls on your form, storing all the controls as global variables would be very time consuming but would work. I think you might be able to get the same result with running an 'Append query'. You might want to look into that before following the information below. Depends really on how many controls you need to pre-populate from the data on the frm-Events in its previous state.

For this example lets take CustomerID which you mentioned above.

Go to the Visual Basic Editor and Click on 'Insert' and then click on 'Module'. Put in the following
Expand|Select|Wrap|Line Numbers
  1. Public CustomerIDHolder as Integer
For AfterUpdate Event on CustomerID control on your form frm-Events type in the code so it looks something like this.

Expand|Select|Wrap|Line Numbers
  1. Private Sub CustomerID _AfterUpdate()
  2. CustomerIDHolder = Me.CustomerID 
  3. End Sub
Then create a button to open the form frm-Events and use the CusterIDHolder data.

Expand|Select|Wrap|Line Numbers
  1. DoCmd.GoToRecord , , acNewRec
  2. Me.CustomerID  = CustomerIDHolder
Your need to do this for each control you want to transfer information for.

This method will for other instances, where you want to open a new record and populate it with data from global variables.
Aug 24 '10 #5
Thank you all for your replies

I shall give these a go later on and see which works best for my requirements.

I will post again tomorrow to let you know how I got on

Aug 24 '10 #6
32,173 Expert Mod 16PB
Please do, and good luck :)
Aug 24 '10 #7
Sorry I got quickly lost trying these as I have never used visual basic before (I am running Access 2003 btw)

Aug 24 '10 #8
32,173 Expert Mod 16PB
Fair enough, but if your skills are that basic, you will need to be very precise about communicating if you want more detailed help. We don't encourage any of our experts to do things for the members, but we're more than happy to lead you through as long as you make the effort to communicate clearly.
Aug 24 '10 #9
201 100+
Medlar, what Neo Pa is suggesting is a rather ingenious workaround if it works. Have a look at your controls properties, you should find the Default Value setting there. Simple change the value of the controls Default Value after the control has been updated using the Form_AfterInsert() event procedure if available.
Aug 24 '10 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Old Lady | last post: by
3 posts views Thread by Brad Allison | last post: by
7 posts views Thread by Dave | last post: by
5 posts views Thread by Michael.Suarez | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.