473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Is it possible to make the main form create a record when the subform is updated?

Seth Schrock
2,965 Expert 2GB
I have a form with a subform. To save time, I have set several of the main form controls to have default values so that I don't have to edit them most of the time. The subform is often the first thing that I go to when adding records. My main problem is that I will be adding records to the subform and then I realize that the main form is still on a new record. As soon as I change a value in the main form, it creates a record and then all of the data that I had put into the subform is lost. I have thought of two ways to prevent this, but I can't figure out how to get either one to work properly.

Idea 1: Use the OnCurrent event to check if the record is a new record - if so, lock the subform, else unlock it. Problem: Even if I get the main form to create a record number (hence, no longer a new record), it doesn't retrigger the OnCurrent event to unlock the subform. I tried using the form's OnDirty event to fix this, but that didn't work either.

Idea 2: Make it so that when the subform gets updated (probably in some BeforeUpdate event), the main form creates a record. Problem: Not sure how to do this either.

Any another ideas?
Oct 14 '12 #1

✓ answered by TheSmileyCoder

Me.Parent.Dirty will ALWAYS be false. The subform cannot gain focus, without the main form saving (if its dirty).

I don't actually know if its possible to save the parent record in any subform event, since there might be a conflict of events. Worth a try of course. I also don't know if the Master/Child relation between subform and master form will properly, I suggest you make sure that the subforms foreign id is filled out properly.

If you were to try this, I feel fairly sure you should try using the BeforeInsert first, if it can work there it would be the most logical choice. Another option could be to use the subforms Enter event. This would however cause a main form save regardless of whether you actually add a child record.

I assume you have some field you can freely "dirty"? Lets assume that tb_Text is a control that does NOT have a defaultvalue assigned and you can dirty at will, then try:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeInsert()
  2.   Me.Parent.tb_Text="" 'Note that the forms dirty EVENT is not triggered when dirtyied by code
  3.   Me.Parent.tb_Text=Null
  4.   Me.Parent.Dirty=False 'Force a save
  5. End Sub
Good luck.

5 3786
TheSmileyCoder
2,322 Expert Mod 2GB
Well there are probably alot of ways to do this. I believe the OnDirty ought to work to control whether the subform is enabled or not.
I don't think its the best suited event though.


In your subform, you could use the BeforeInsert event (This event occurs just as you start typing in the first field in a form.) to check whether the parent is a NewRecord.
Aircode below:
Expand|Select|Wrap|Line Numbers
  1. Private Sub BeforeInsert(Cancel as Integer)
  2.  If me.Parent.NewRecord then
  3.    Cancel=True
  4.    MsgBox "Please save the record in the main form first"
  5.  End If
  6. End Sub
Another possiblity is to use the main forms Current event in conjunction with the main forms AfterUpdate Event.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2.   Me.SubFormControl.Enabled=not Me.NewRecord
  3. End if
  4.  
  5. Private Sub Form_AfterUpdate()
  6.   Call Form_Current()
  7. End Sub
Oct 14 '12 #2
Seth Schrock
2,965 Expert 2GB
Thanks Smiley! I've decided to use your first option. However, would it be possible to make the main form become not a new record from within that If/Then statement? I tried
Expand|Select|Wrap|Line Numbers
  1. If Me.Parent.Dirty = True Then Me.Parent.Dirty = False
but that didn't work. Is there another way? If not, I can live with it.
Oct 14 '12 #3
TheSmileyCoder
2,322 Expert Mod 2GB
Me.Parent.Dirty will ALWAYS be false. The subform cannot gain focus, without the main form saving (if its dirty).

I don't actually know if its possible to save the parent record in any subform event, since there might be a conflict of events. Worth a try of course. I also don't know if the Master/Child relation between subform and master form will properly, I suggest you make sure that the subforms foreign id is filled out properly.

If you were to try this, I feel fairly sure you should try using the BeforeInsert first, if it can work there it would be the most logical choice. Another option could be to use the subforms Enter event. This would however cause a main form save regardless of whether you actually add a child record.

I assume you have some field you can freely "dirty"? Lets assume that tb_Text is a control that does NOT have a defaultvalue assigned and you can dirty at will, then try:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeInsert()
  2.   Me.Parent.tb_Text="" 'Note that the forms dirty EVENT is not triggered when dirtyied by code
  3.   Me.Parent.tb_Text=Null
  4.   Me.Parent.Dirty=False 'Force a save
  5. End Sub
Good luck.
Oct 14 '12 #4
Seth Schrock
2,965 Expert 2GB
Perfect! Here is what I did:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeInsert(Cancel As Integer)
  2. If Me.Parent.NewRecord Then
  3.     Me.Parent.SaleDate = Date
  4.     Me.Parent.Dirty = False
  5.  
  6. End If
  7.  
  8. End Sub
SaleDate was one of the controls that had the default date, but all I did was reset it to the default date again. This was enough to trigger the main form to be dirty and then saved. I don't have to cancel the insert or anything.
Oct 14 '12 #5
TheSmileyCoder
2,322 Expert Mod 2GB
Glad to hear it worked out for you :)
Oct 14 '12 #6

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

Similar topics

3
by: pepino13 | last post by:
Hello, I have a a form called frmMissyFact, I have copied this form and called it frmMissyFactBackup which reads data from a backup database of the week before. I brought frmMissyFactBackup into...
9
by: William Wisnieski | last post by:
Hello Everyone, Access 2000 I have a main form with a continuous subform. On the main form I have a text field called . It gets populated based on what the user selects in a field on the...
18
by: Darryl Kerkeslager | last post by:
I don't do much with subforms - in fact I've deliberately avoided them - but .... I have a tab control that will contain several subforms, each bound to a separate table, and each table/subform...
1
by: NBruch | last post by:
Ok let me explain what im trying to do: i need a combo box which needs to be linked to a listbox (or combo box doesnt matter which really) then the listbox should bring up the record in a...
12
by: swingingming | last post by:
Hi, in the NorthWind sample database, when clicking on the next navigation button on the new order record with nothing on the subform (order details), we got an order with nothing ordered. How can...
0
by: nospam | last post by:
I have a main form that has 3 fields. They are all locked fields, only to show the user the "person" record that they are looking at. They update to subforms on a tab. The first subform is...
1
by: solargovind | last post by:
Hi, In my main form, i have one Combo box called "Student_name" which displays Stud_id & Stud_name. When i select particular student name, i need to display their City in the Subform's text box. ...
6
by: 85ssp | last post by:
I have a server applicatoin that has a multitude of labels related to the clients connected to it. When I have a number near 50 clients and i recieve a updatated status from each of the clients at...
5
by: slenish | last post by:
Im having trouble trying to get a report to pull based on selections you make from 3 combo boxes. What I have are 3 combo boxes city, state, and zip code. What im trying to do is make is so you...
3
by: Gordon Padwick | last post by:
I have two tables in Access 2003. The first table, that contains common names of plants, has two columns: CommonNameID (primary key) and Common (common name). The second table, that contains...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.