473,404 Members | 2,178 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,404 software developers and data experts.

pass a value from a main form to a subform

14
Hi There,

I'm a beginer in Access and VB.
In my Database, I've got a form with a subform.
the subform is a query from a table different than the table of the main form. those tables don't have a link yet.

the main form is being use to create a new entry/ a new row in the table.

What I want to do is use the primery key from the main form and copy the value in a field in the subform. In the subform, I first need to select some rows. The value has to be copied when I select the item, there is a field beside where the value should be.

I try different thing but I haven't find a solution that work.
one of the solution, the most logical for me, is to add an event 'on click' when I choose which element I want in the list. I used the expression builder but it doesn't work. one of the expression was something like this:
Expand|Select|Wrap|Line Numbers
  1.  =([Transaction]=[Forms]![Kit Transactions Form]![ID])
This is not working, I'm not sure why, and I haven't find any function or expression that could do the trick.

This will be used later one to link the two tables and create reports.

I hope anyone understood what I want to do, it is quite complicated to describe everything in words. I attach a print screen of the form and subform.

Any sugestion is welcome.

Thanks in advance
Attached Images
File Type: jpg Image1.jpg (11.8 KB, 1597 views)
May 11 '10 #1
27 29173
tragaz
14
Hi again,

I just try to put this code in the 'on click' event

Expand|Select|Wrap|Line Numbers
  1. Private Sub Used_Click()
  2.     Forms![Kit Transactions Form]![Courier Bag Inventory Query subform].Form!Transaction.Value = Forms![Kit Transactions Form]!ID.Value
  3. End Sub
It still doesn't work

Thanks for any help
May 11 '10 #2
MMcCarthy
14,534 Expert Mod 8TB
You actually have to take this back to the relationship between the two tables.

If we call Table1 the name of the table behind the main form and Table2 the name of the table behind the subform. Now Table1 has an ID field called Table1_ID and Table2 has an ID field called Table2_ID and a foreign key called Table1_ID.

Now it's important that Table2_ID has a data type of autonumber. There are other ways of doing it but this is the simplest. Set the default value of of the Table1_ID field in Table2 to ...
Expand|Select|Wrap|Line Numbers
  1. =Forms!MainFormName!Table1_ID
Now set the parent child relationship between the main form and the subform to be based on Table1_ID.

You will find that as you start to add value to any of the other fields in the subform, that Table2_ID will generate automatically as it is an autonumber and the value in Table1_ID in the subform will reflect the value of Table1_ID in the main form.

I hope I've explained this clearly. Let me know if you can't follow.

Mary
May 11 '10 #3
tragaz
14
Thanks for this,

the thing is that in Table2, there are already some existing field, I don't want o create new ones, at least not in this case.

In fact it is in Table1 that a new row is created by filing up the main form.
I actually want to copy the value of the Table1 primery key newly created to some field in Table2, by selecting in the subform which field to copy.

You're solution is sort of work since in the subform, if i wanted to create a new row, then the Primery key would be copied, but it does't copy in field that I'm selecteing afterthat...

Hope it is clear.
Philippe
May 11 '10 #4
MMcCarthy
14,534 Expert Mod 8TB
@tragaz
I don't understand your reply. I think it's best if you use the table names and field names you are actually using so I can better follow what you are saying.
Also please give the metadata of the tables so I can see the existing structure.

Here is an example of how to post table MetaData :
Table Name=tblBookings
Expand|Select|Wrap|Line Numbers
  1. Field; Type; IndexInfo
  2. StudentID; Autonumber; PK
  3. Family; String; FK
  4. Name; String
  5. University; String; FK
  6. Mark; Numeric
  7. LastAttendance; Date/Time
May 11 '10 #5
tragaz
14
Hi msquared

Sorry for the confusion, I though that using the name that you used in your example would be easier for you to follow.

Ok so I've got 2 tables:
Table Name = Kit Transactions
Expand|Select|Wrap|Line Numbers
  1. ID; AutoNumber; Pk
  2. Kit Barcode; Text; Fk
  3. Courier Bags; Number
  4. Courier Bag Barcodes; Text
  5. Cotton Rolls; Number
  6. Vials; Number
  7. Forceps; Number
  8. Dispatched; Date/Time
  9. Client; Number; Fk
  10. Received; Date/Time
  11. Chilly Bag Returned;T ext
  12. Courier Bags Returned; Number
  13. Cotton Rolls Returned; Number
  14. Vials Returned; Number
  15. Forceps Returned; Number
  16. Received By; Number; Fk
  17. Invoiced; Date/Time
  18. Notes; Text
Table name: Courier Bag Inventory
Expand|Select|Wrap|Line Numbers
  1. ID; AutoNumber; Pk
  2. Track n Trace number; Text
  3. Used; Yes/No
  4. Transaction; Number; Fk
Alright, so my main form is based on the table Kit Transactions
the subform is Courier Bag Inventory based on a query that shows only the data where Used='No'
In the subform, Used is represented by a checkbox.

What I want to do is when the user click on the check box, the value of Kit Transactions.ID is copied in the Courier Bag Inventory.Transaction

Also, the main form is used to create a new data in the table Kit Transactions and update data in other tables links to it. One of the update that has to be done is copying the value of the new ID into the selected items of the Courier Bag Inventory

Hope you understand
May 11 '10 #6
NeoPa
32,556 Expert Mod 16PB
Expand|Select|Wrap|Line Numbers
  1. Private Sub Used_Click()
  2.     Forms![Kit Transactions Form]![Courier Bag Inventory Query subform].Form!Transaction.Value = Forms![Kit Transactions Form]!ID.Value
  3. End Sub
Taking a step back for a second...
This appears to me to be workable. It matches the format described in Referring to Items on a Sub-Form.

I suspect that one of these forms (main or sub) actually contains the control Used. This form can be referred to more simply as Me.

Can you say exactly what is failing here? I would certainly consider progressing on this front if possible, as it is more precisely related to your problem.
May 12 '10 #7
tragaz
14
Hi Neopa

the control Used is in the subform. I know that I can refer to the subform as Me but I wanted to be sure of what I was writing.
but just to be sure, if I want to use Me, should it be like this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Used_Click() 
  2.      Forms![Kit Transactions Form]!Me.Form!Transaction.Value = Forms![Kit Transactions Form]!ID.Value 
  3. End Sub 
Or like that:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Used_Click() 
  2.      Me.Transaction.Value = Forms![Kit Transactions Form]!ID.Value 
  3. End Sub 
I'm not sure what is going wrong because when click on the control Used it appear that the value hasn't been copied. well at least the value is not showing in the field Transaction as well as in the Table Courier Bag Inventory where this field is... Any idea?

I also tried to add Me.Transaction.Requery after the precedent code, thinking that it might actually refresh the value of Transaction but it's not. the code looked like this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Used_Click() 
  2.      Forms![Kit Transactions Form]![Courier Bag Inventory Query subform].Form!Transaction.Value = Forms![Kit Transactions Form]!ID.Value
  3.      Forms![Kit Transactions Form]![Courier Bag Inventory Query subform].Form!Transaction.Requery 
  4. End Sub 
With Me:
(sorry for the repition I'm just not sure if I'm using Me correctly or not)
Expand|Select|Wrap|Line Numbers
  1. Private Sub Used_Click() 
  2.      Me.Transaction.Value = Forms![Kit Transactions Form]!ID.Value
  3.      Me.Transaction.Requery 
  4. End Sub 
Thanks in advance for any help
May 12 '10 #8
NeoPa
32,556 Expert Mod 16PB
tragaz: should it be like this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Used_Click() 
  2.      Forms![Kit Transactions Form]!Me.Form!Transaction.Value = Forms![Kit Transactions Form]!ID.Value 
  3. End Sub 
Or like that:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Used_Click() 
  2.      Me.Transaction.Value = Forms![Kit Transactions Form]!ID.Value 
  3. End Sub 
.Value is the default property of most controls so is generally redundant.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Used_Click()
  2.      Me.Transaction = Forms![Kit Transactions Form].ID
  3. End Sub
You could also say :
Expand|Select|Wrap|Line Numbers
  1. Me.Transaction = Me.Parent.ID
This works better if you ever need the subform to be contained on any other form than [Kit Transactions Form].
tragaz: I also tried to add Me.Transaction.Requery after the precedent code, thinking that it might actually refresh the value of Transaction but it's not.
The format would be :
Expand|Select|Wrap|Line Numbers
  1. Call Me.Requery
but it should not be necessary in this case.
May 13 '10 #9
tragaz
14
Neopa,

I wrote the command as you said, for the correct way:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Used_Click() 
  2.      Me.Transaction = Forms![Kit Transactions Form].ID
  3. End Sub 
Unfortunately, it doesn't work. When I click on the Control Used, the value is not copied, well at least I don't see the value in the Transaction control and in the field in the table.

Any idea how I could do that?
May 13 '10 #10
NeoPa
32,556 Expert Mod 16PB
Normally, when something as simple as this is nmot working, it indicates that the OP (Original Poster - You) has overlooked some important information. Obviously there is very little chance that I could determine what that might be, so I suggest you post a (possibly sanitised) copy of your database for me to look at. I would hope that I could determine the missing factor with all the details in front of me.

When attaching your work please follow the following steps first :
  1. Remove anything not relevant to the problem. This is not necessary in all circumstances but some databases can be very bulky and some things do not effect the actual problem at all.
  2. Likewise, not entirely necessary in all cases, but consider saving your database in a version not later than 2003 as many of our experts don't use Access 2007. Largely they don't want to, but some also don't have access to it. Personally I will wait until I'm forced to before using it.
  3. If the process depends on any linked tables then make local copies in your database to replace the linked tables.
  4. If you've done anything in steps 1 to 3 then make sure that the problem you're experiencing is still evident in the updated version.
  5. Compile the database (From the Visual Basic Editor select Debug / Compile {Project Name}).
  6. Compact the database (Tools / Database Utilities / Compact and Repair Database...).
  7. Compress the database into a ZIP file.
  8. When posting, scroll down the page and select Manage Attachments (Pressing on that leads you to a page where you can add or remove your attachments. It also lists the maximum file sizes for each of the allowed file types.) and add this new ZIP file.
It's also a good idea to include some instructions that enable us to find the issue you'd like help with. Maybe some instructions of what to select, click on, enter etc that ensures we'll see what you see and have the same problems.
May 14 '10 #11
tragaz
14
Neopa,

I'd like to do that but the database is kind of confidential and I don't know if people outside of my firm are able to access it.
The database has been created before I arrive in the firm and they asked me to help resolve the problem...

Is there anyway that I can give a sample of the databse only to you?

Thanks for your understanding
May 17 '10 #12
NeoPa
32,556 Expert Mod 16PB
That's exactly why I mentioned sanitised. I'm very careful about giving out my private details and giving my email address would not be careful.

Is there any reason you could not create a sanitised version that would be fit for public consumption? I don't see much of the data being relevant to the question at hand. I'm sure if you follow the posted instructions carefully you could provide something that works for all of us.

PS. Alternatively, it may be easier to create a whole new database with just this issue within it and attach that. Importing any relevant objects across is pretty straightforward.
May 17 '10 #13
tragaz
14
I clean the database so all the data inside are irevelant, they have no meaning.

I try and the problem is still here.

what you need to do when you open the database is:
first say no to the switchboard thing cause I deleted it...

everything is happening with the form Kit Transactions
in a first place you have to select a client number from the list, then you can select a kit bar code from the list too. a new ID number will be created then

What I want to do is in the subform, there is a list of items called courier bag, choose a maximum of 5 courier bag by clicking on the check box corresponding to the courier bag. And when you click, the ID newly created should be copied in the blank field called Transaction

And that is where I got my problem. If you can find a solution for that, that would be awesome.

Cheers
Attached Files
File Type: zip copy Inventory Database cleaned for bytes.zip (73.3 KB, 340 views)
May 18 '10 #14
NeoPa
32,556 Expert Mod 16PB
Good job!

I cannot download during the day, I'm afraid. I will take a look at this later on my home PC and I'll let you know anything I come up with.
May 18 '10 #15
tragaz
14
Great, Hope to hear from you soon
May 18 '10 #16
NeoPa
32,556 Expert Mod 16PB
I do apologise for not getting this last night. I'm afraid I had another visit to the dentist and I had an attempted easy evening to deal with some pain issues. I'll send myself a note home to make sure I do it tonight.
May 19 '10 #17
NeoPa
32,556 Expert Mod 16PB
Having spent the last 1/4 hour trying to follow your instructions using the form [Kit Transactions], I got thoroughly confused and found that [Kit Transactions] is simply a table (not a table format form as I'd assumed - led by your instructions).

I'm now working on the form called [Kit Transactions Form] (Don't blame me. You gave it the name). I'll post back if I manage to discover anything tonight.
May 19 '10 #18
NeoPa
32,556 Expert Mod 16PB
I managed to follow the procedure as you indicated. I must say that I saw no problem with how the form was working. Can I just confirm that you see no [Transaction ID] (from the main form) populated into the [Transaction] control on the subform of the record just selected?

The only problem I noticed was that it allows you to deselct an item, but the [Transaction ID] is never cleared in that case.
May 19 '10 #19
tragaz
14
Hi Neopa,

My apologise for the incomplete name of the form. Yes it is on the form Kit Transaction From.
I confirm that I see no Transaction ID from the main form into the Transaction control of the subform. That is what I want to do.

The problem you saw is right. I just haven't done anything on it yet, I wanted in a first time to see the transaction ID populated into Transaction. Do you have any idea why it's not working?
May 19 '10 #20
NeoPa
32,556 Expert Mod 16PB
I'm afraid it works perfectly for me.

This unfortunately indicates something outside of Access as being the issue. Maybe a setting somewhere that effects how your code is run. That's quite unusual, at least from my experience.
May 19 '10 #21
tragaz
14
So you can see the Transaction ID from the main form populated into Transaction control of the subform; and it is shown in the table Courier Bag Inventory too?
May 19 '10 #22
tragaz
14
it's weird cause it's not working for me... Could it be cause I'm on Access2007?
May 19 '10 #23
NeoPa
32,556 Expert Mod 16PB
As a bonus though, try this :
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub Used_Click()
  5.     With Me
  6.         .Transaction = IIf(.Used, .Parent.ID, Null)
  7.         If .Dirty Then .Dirty = False
  8.     End With
  9. End Sub
If you like it, study the differences. See what each line does. Why it's there. Etc. Particularly Option Explicit. This should always be set in all your modules (See Require Variable Declaration).
May 19 '10 #24
tragaz
14
ok thanks I will try that and see are the lines doing

Thanks
May 19 '10 #25
NeoPa
32,556 Expert Mod 16PB
tragaz: So you can see the Transaction ID from the main form populated into Transaction control of the subform; and it is shown in the table Courier Bag Inventory too?
A definite confirmation of the first bit.

The second bit I can now confirm (having checked) but I'd have been extremely surprised if I'd found otherwise as it's already shown on the form.

Having said that of course, I now see your own earlier attempts with blanks in the [Transaction] field.

To answer your later question :
Yes. As far as my limited understanding goes, I see the 2007 version thing as a possibility. In as much as I don't know it couldn't have that effect. I know of no reason it would mind, but you have a problem. You are using 2007. I cannot say there is no correlation.
May 19 '10 #26
tragaz
14
alright,

well just try with the code you gave me and it stil doesn't work. I will look at the code you used and understand it...
I don't really know what I should do to fix this prob since we all using access2007 here...

Thanks a lot for your help though I really apreciate
May 19 '10 #27
NeoPa
32,556 Expert Mod 16PB
I suggest trying to trace through your code (See Debugging in VBA). Just to confirm that any code is actually running. I've heard there are situations in A2007 where the code is disabled automatically. Check this isn't the case first. That is probably fixable (though not with my help as I still use A2003 and know very little of A2007).
May 19 '10 #28

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

Similar topics

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...
1
by: Bill Strass | last post by:
Problem: Access main form/subform as front end to SQL Server backend. Add/edit via subforms work fine. Not so with main form. Set up a master-detail form-subform using two views linked from SQL...
0
by: Deano | last post by:
I have a main/subform arrangement which works well. The main form's parent records are about employees. The subform calculates their salary. I have now decided that the user can enter some...
4
by: Dave Boyd | last post by:
Hi, I have two very similar forms each with a subform. The main form gets a few fields from the user and passes this back to a query that the subform is bound to. The requery is done when 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...
5
by: Ivan | last post by:
I am used to VB6 and am not sure how to do this in Vstudio .NET. I have a main form which calls other forms. I want to disable that main form while other ones are called. I tried hiding it and...
3
by: Steve | last post by:
Is there a way to put a main form/subform in a continuous form so I can scroll through all the records in the main form? Thanks!
1
JustRun
by: JustRun | last post by:
hi, I have a problem with passing the value from a Form that "user should choose a start date and end date" to a Form that display the result in a Crystal Report Form Actually it gives this...
0
by: farid121 | last post by:
I am trying to update the main form with each record in the subform. I have two tables Invoice and Payment table. The main form has the payment details which I need to update for each invoice...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.