473,387 Members | 3,684 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,387 software developers and data experts.

Using a combo box to switch to and add new records

I've read many forum posts and articles and haven't made any headway with this. Let me describe my tables and when I'm trying to do and see if any one can help me.

I have an items table, a vendors table, and a vendorItems table. The primary keys are ItemID, (ItemID, VendorID), and VendorID respectively. vendorItems is a join table, facilitating a many-to-many relationship between vendors and items. I've built a form for entering data on items. I want to include a subform on this form that includes information on the relevant vendorItems records. This is displayed as a single form, not a continuous form, because the data is complicated and best presented one at a time.

I would like a combo box (it can be in the items form or the vendorItems subform) to do the following:
- get it's rowsource from the vendors table, displaying the name and storing the vendorID
- after the combo box is updated, look for a record in vendorItems where the vendorID = the updated combo box's value and the itemID = the current itemID of the items form.
- If said record is found, move the subform to this record for editing
-If said record is not found, create a new record in vendorItems where vendorID = the updated combo box's value and itemID = the current itemID of the items form.

Here is a picture of the form. The combo box labeled vendor is the one I want to do all of this work. As I've said before if it makes more sense for it to be in the items form itself then that is no problem.



Thanks in advance for any help.
Jan 12 '07 #1
9 2409
NeoPa
32,556 Expert Mod 16PB
Let me first try to organise the metadata into understandable form :
Expand|Select|Wrap|Line Numbers
  1. Table Name=Items
  2. ItemID; Autonumber; PK
  3. Name; String
  4. Description; String
  5. SerialNo; String
  6. Location; String
Expand|Select|Wrap|Line Numbers
  1. Table Name=Vendors
  2. VendorID; Autonumber; PK
  3. Name; String
  4. StockNo; String
  5. ItemsPerUnit; Numeric
Expand|Select|Wrap|Line Numbers
  1. Table Name=VendorItems
  2. ItemID; Numeric; FK
  3. VendorID; Numeric; FK
Please repost with more accurate info if you can.
Jan 13 '07 #2
NeoPa
32,556 Expert Mod 16PB
You would need in the ComboBox RowSource something like :
Expand|Select|Wrap|Line Numbers
  1. SELECT V.VendorID,V.Name
  2. FROM VendorItems AS VI INNER JOIN Vendors AS V
  3. ON VI.VendorID=V.VendorID
  4. WHERE VI.ItemID=txtItemID
This could all be entered on a single line if preferred with spaces replacing new lines.
BTW this assumes the the name of the TextBox control holding the ItemID is txtItemID.
Test this out first as I'm not sure if the ComboBox being in the SubForm will cause any problems.
Jan 13 '07 #3
NeoPa
32,556 Expert Mod 16PB
There will be a procedure created for the AfterUpdate event of the Vendor ComboBox (We'll call cboVendor) but we don't know all the details of what's required in there until you post back what you find when testing out the earlier posts.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboVendor_AfterUpdate
  2. '... Some dependant code here
  3.     Call Me.SubFormControlName.Form.ReQuery
  4. End Sub
Jan 13 '07 #4
NeoPa,
Your summary of my schema is spot on. In the vendorItems table, the two ID domains are set as primary keys as well as serving as primary keys. I could just as easily have included a 'vendorItemID' and set that as the primary key, but the former solution seemed best.

The Row Source you've given seems like it will work but I'm unsure if a join is needed. I would like all records in the vendor table to be selectable in the combo box regardless of wether they have corresponding records already in the vendorItems table. So, for example, even if the item with itemID 1 has records only for vendors with vendorID 2, 4, and 5 (in the vendorItems table, this would be the primary key pairs (1,2), (1,4), and (1,5)), I would still like the combo box to have a row source with, say, 1,2,3,4 and 5 as available selections, assuming there are 5 vendors total.
Preferably, the afterUpdate event will then determine if the selected vendorID has an entry in the vendorItems table where itemID = 1 (in keeping with the previous example). Once determined it will switch to that record if it exists, or create it if it does not.

I'm assuming you already know all this and your reason for using the inner join will become apparent later on. Thanks for your interest and I look forward to your solution.
Jan 14 '07 #5
NeoPa
32,556 Expert Mod 16PB
NeoPa,
Your summary of my schema is spot on. In the vendorItems table, the two ID domains are set as primary keys as well as serving as primary keys. I could just as easily have included a 'vendorItemID' and set that as the primary key, but the former solution seemed best.
If you'd had a VendorItemID then you would still have needed the VendorID & ItemID fields as well.
The Row Source you've given seems like it will work but I'm unsure if a join is needed.
My reason for a join here is that I felt the Name field would be required to show in the ComboBox. If that is not required then, certainly, no JOIN would be. You would simply use the [VendorItems] table.
I would like all records in the vendor table to be selectable in the combo box regardless of wether they have corresponding records already in the vendorItems table. So, for example, even if the item with itemID 1 has records only for vendors with vendorID 2, 4, and 5 (in the vendorItems table, this would be the primary key pairs (1,2), (1,4), and (1,5)), I would still like the combo box to have a row source with, say, 1,2,3,4 and 5 as available selections, assuming there are 5 vendors total.
OK, so now I understand better ;) In that case :
Expand|Select|Wrap|Line Numbers
  1. SELECT VendorID,Name
  2. FROM Vendors
will do you.
Preferably, the afterUpdate event will then determine if the selected vendorID has an entry in the VendorItems table where itemID = 1 (in keeping with the previous example). Once determined it will switch to that record if it exists, or create it if it does not.
Does that mean that the data in the SubForm is from the VendorItems table as you haven't supplied me with any fields in there other than the PK itself. This implies it is simply a connecting table and not one for data. Please clarify here. If there is more info in the schema that is required please post it. I'm stuck at this point.
I'm assuming you already know all this and your reason for using the inner join will become apparent later on. Thanks for your interest and I look forward to your solution.
No, that wasn't clear before.
That reminds me - I'm not trying to post solutions - simply to help you to go in the right direction. I forget that sometimes :(
Jan 14 '07 #6
Let's use filler data instead of speaking in abstracts all the time. Here are some records for each of the tables:

Items table:
Expand|Select|Wrap|Line Numbers
  1. ItemID Name
  2. 1      lead pipe fitting
  3. 2      electrical tape (single roll)
  4. 3      measuring tape
  5. 4      12ft. ladder
Vendors table:
Expand|Select|Wrap|Line Numbers
  1. VendorID Name
  2. 1        Barett
  3. 2        Nolan
  4. 3        CostCo
VendorItems table:
Expand|Select|Wrap|Line Numbers
  1. ItemID VendorID Price
  2. 1      1        $1.67
  3. 2      3        $3.09
  4. 1      2        $1.42
  5. 1      3        $1.50
The rowsource for our special combo box will be
Expand|Select|Wrap|Line Numbers
  1. SELECT vendors.vendorID, vendors.name FROM vendors;
and some relevant access properties:
Expand|Select|Wrap|Line Numbers
  1. BOUND COLUMN = 1
  2. COLUMN WIDTHS = 0";2";
So, the cboVendor combo box will display the names of the vendors, and 'store' the selected vendorID.

If our items form were currently on the item with itemID=1, then cboVendor would simply act as the standard combo box that you can get from the wizard that lets you switch among records. This is mostly coincidental because item 1 happens to already have entries for all of the vendors.

If our items form were instead on the item with itemID=2, then cboVendor would act differntly. if the user selects 'CostCo', then '3' would be stored in cboVendor and code in the afterUpdate event would switch the VendorItems subform from whatever record it is currently displaying to the vendorItems record where itemID = 2 and vendorID = 3.

if the user alternatively selects 'Nolan' then something different should happen. a vendorItems record with itemID = 2 and vendorID = 2 will be searched for and not found. When this happens a new record is added to vendorItems (the same one we failed to find in our search: itemID=2 and vendorID=2). the subform will then display the new record.


If it helps to conceptiualize the problem, you can think of it this way. If not, please ignore it because it probably just confuses things. An alternative solution to this magic combo box, would be to automatically create a record for each vendor the vendors table in the vendorItems table whenever a new item is added to the items table, and automatically create a record for each item in the items table in the vendorItems table whenever a new vendor is added to the vendors table. If this were done then the combo box could be created using the access wizard for switching among records. This approach is of course impractical and creates a lot of empty records.

Thanks for your interest and I look forward to you teaching me how to fish ;)
Jan 14 '07 #7
NeoPa
32,556 Expert Mod 16PB
Example is good, but I find it easier to work from definitions.
Your examples are quite clear though and provide more info which is good.
To start then, I will repost the updated MetaData.
Expand|Select|Wrap|Line Numbers
  1. Table Name=Items
  2. ItemID; Autonumber; PK
  3. Name; String
  4. Description; String
  5. SerialNo; String
  6. Location; String
Expand|Select|Wrap|Line Numbers
  1. Table Name=Vendors
  2. VendorID; Autonumber; PK
  3. Name; String
  4. StockNo; String
  5. ItemsPerUnit; Numeric
Expand|Select|Wrap|Line Numbers
  1. Table Name=VendorItems
  2. ItemID; Numeric; FK
  3. VendorID; Numeric; FK
  4. Price; Currency
This will help anyone else reviewing the thread later. I did consider updating the earlier post but realised that would confuse someone reading it later as the conversation wouldn't have made proper sense.
Jan 14 '07 #8
NeoPa
32,556 Expert Mod 16PB
The rowsource for our special combo box will be
Expand|Select|Wrap|Line Numbers
  1. SELECT vendors.vendorID, vendors.name FROM vendors;
Personally, I don't bother with the table (RecordSet) qualification when only one RecordSet is specified in the FROM clause. It makes it longer and there's more to read and understand.
Expand|Select|Wrap|Line Numbers
  1. SELECT VendorID,Name FROM Vendors;
...Stuff about how it should work...
This bit I get. I'll share what I know but it's limited I'm afraid.
When the Vendor is selected from cboVendor, the code (In the AfterUpdate event procedure) should set the RecordSource of the SubForm's Form object to SQL querying the VendorItems table but matching only the record (if it exists) which matches both the ItemID & the VendorID selected. When this is done, like any updatable query, if no records match, then the query will select the New record at the end. The problem here is to ensure that, if data is entered and saved away, the ItemID & VendorID are correctly set even if there are no ItemID & VendorID controls on the form. Let's try doing it this way and see if Access fills the other data in automatically. If it doesn't we'll look again.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboVendor_AfterUpdate
  2.     With Me.SubFormControlName.Form
  3.         .RecordSource = _
  4.             "SELECT ItemID,VendorID,Price " & _
  5.             "FROM VendorItems " & _
  6.             "((ItemID=" & Me.txtItemID & ") AND " & _
  7.             "(VendorID=" & Me.cboVendor & "))"
  8.         Call .ReQuery
  9.     End With
  10. End Sub
We need to know the name of your SubForm control (as opposed to the name of the form used within that).
Thanks for your interest and I look forward to you teaching me how to fish ;)
I like the reference - very well put.
Jan 15 '07 #9
NeoPa
32,556 Expert Mod 16PB
Firstly, I updated your original post to show the image rather than to link to it.
Secondly, don't forget to read my other replies since you last posted. There may be questions and there will certainly things I need you to read.
Jan 15 '07 #10

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

Similar topics

1
by: Alex | last post by:
Acc97. I have a form which within a subform records machine down time. I have a total of 8 machines. (1-2-3 ect.) What I have been doing is using a combo box linked to the record table to...
4
by: Skully Matjas | last post by:
I am using the following code (created by the wizard) to allow to bring my form to a particular entery. But when I edit the entery (ex: put new information into a blank cell), it puts that record...
1
by: Robert Neville | last post by:
The solution to my dilemma seems straight-forward, yet my mind has not been forthcoming with a direct route. My Project form has a tab control with multiple sub-forms; these distinct sub-forms...
3
by: Afton | last post by:
I would like to make a form that filters a report by Supervisor and by starting and ending date. I have the supervisors in a combo box, and that works. However, I do not know how to code to let...
3
by: geribosi | last post by:
I have a question that may have been asked numerous times, however from the previous postings no one seems to have a plausible explanation. This is a performance issue that is very common: I...
5
by: Col | last post by:
I have a query with criteria that comes from a combo box on a form. I'd like to set the query up so the user can either limit the criteria to the selection in the combo box or the user can pull all...
2
by: kathat | last post by:
I have a form with a combo box that I want to use for navigating through the records. I have 10 records (for test purposes) in my table (mainly text boxes, but also a check box and an ole object for...
5
by: dgardner | last post by:
I have a combo box that lists street numbers. I also have a list box that lists street numbers and addresses (separately). I want to click on one of the street numbers and have the first item in...
4
by: novoselent | last post by:
This seems like it should be an easy thing, but I think I'm missing something simple here...or I'm just going about it all wrong... Using Access 2003 I have a form that lists vehicle service...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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...

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.