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?
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: - Private Sub Form_BeforeInsert()
-
Me.Parent.tb_Text="" 'Note that the forms dirty EVENT is not triggered when dirtyied by code
-
Me.Parent.tb_Text=Null
-
Me.Parent.Dirty=False 'Force a save
-
End Sub
Good luck.
5 3786
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: - Private Sub BeforeInsert(Cancel as Integer)
-
If me.Parent.NewRecord then
-
Cancel=True
-
MsgBox "Please save the record in the main form first"
-
End If
-
End Sub
Another possiblity is to use the main forms Current event in conjunction with the main forms AfterUpdate Event. - Private Sub Form_Current()
-
Me.SubFormControl.Enabled=not Me.NewRecord
-
End if
-
-
Private Sub Form_AfterUpdate()
-
Call Form_Current()
-
End Sub
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 - 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.
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: - Private Sub Form_BeforeInsert()
-
Me.Parent.tb_Text="" 'Note that the forms dirty EVENT is not triggered when dirtyied by code
-
Me.Parent.tb_Text=Null
-
Me.Parent.Dirty=False 'Force a save
-
End Sub
Good luck.
Perfect! Here is what I did: - Private Sub Form_BeforeInsert(Cancel As Integer)
-
If Me.Parent.NewRecord Then
-
Me.Parent.SaleDate = Date
-
Me.Parent.Dirty = False
-
-
End If
-
-
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.
Glad to hear it worked out for you :)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |