473,385 Members | 1,806 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,385 software developers and data experts.

Table Relationships for a Newbie!

dgaletar
72 64KB
Hello. I have encountered a problem that has frustrated me for going on two days now. And the worst part is that I KNOW the answer is so simple!

I have created two tables (see image). One contains the vehicles main details, and the other the list of service procedures that have been performed for that vehicle.



I am trying to open the "Service" form from the "Vehicle Details" form as a "New Record" relating to THAT SPECIFIC VEHICLE.

I have designed everything beautifully, but I can not seem to get it to work right!!!

Every time I click the button on the "Vehicle Details" form to open a new record specific to that vehicle, it gives me an error: Index or primary key cannot contain a Null value.

When I remove the Primary Key from the "Service" table, it works but then the "CUA#" field in the "Service" table is empty, so the records are not related.

WHAT AM I DOING WRONG???

Thanks,

dgaletar
Attached Images
File Type: jpg ScreenShot4.jpg (59.9 KB, 1481 views)
Jan 11 '13 #1
19 2785
Rabbit
12,516 Expert Mod 8TB
Please post the code that runs on that button click.
Jan 11 '13 #2
dgaletar
72 64KB
Expand|Select|Wrap|Line Numbers
  1. {Macro}
  2. OpenForm
  3.     Form Name...Service : Form
  4.         View...Form
  5.     Filter Name...
  6.     Where Condition...[CUA#]=[VEHICLES2 subform].[Form]![CUA#]
  7.     Data Mode...Add
  8.     Window Mode...Normal
Jan 11 '13 #3
Rabbit
12,516 Expert Mod 8TB
Oh, you're using a macro. I don't use macros but I can tell you what I would do in VBA code. I would open the form and pass the CUA# in the OpenArgs parameter. Then, in the on load event of the form, I would populate the foreign key field with that CUA#.
Jan 11 '13 #4
dgaletar
72 64KB
I'm sorry if I'm asking too much again, but I'm not even 100% sure what a macro is. Could u explain in more detail, &/or give me an example?
Jan 11 '13 #5
NeoPa
32,556 Expert Mod 16PB
Rabbit:
I would populate the foreign key field with that CUA#.
For this to work more seamlessly I would ensure that the foreign key control is set with the .DefaultValue, rather than the .Value, property set to the value passed. That way the buffer isn't dirtied and nothing goes awry if the operator decides to abort the process.

@Dgaletar.
I will try to give you some more detailed guidance later in the weekend but I'm just back from the pub now and I'm too tired :-(
One tip I'd leave you with. Try to break down the instructions into manageable chunks first before deciding it's all too complicated. You might surprise yourself.
Jan 12 '13 #6
TheSmileyCoder
2,322 Expert Mod 2GB
There are a few issues to address.
Relationship
I presume 1 vehicle can have more then service performed. If you try to relate 1 primary key to another primary key, you can only have a 1-1 relation, not a 1-many relations.
I advice you to look at this article we have on our site :
Datebase normalization and table structures
It explains it very well.

The approach is to have the service table have its own primary KEY, and a foreign key that relates the multiple service record details to the single vehicle. Again, read the article, for more details.



Macro/VBA
Macros are basicly bits of pre-defined codes that can exposes a limited set of functions in access. Macros can be slightly easier to learn, but I find that it can sometimes take longer to achieve the goal you want, due to their limits. VBA (Visual Basic for Applications) will give you alot more power and control, but can take a bit longer to learn. A benefit of using VBA over macros is that experts on sites like this likely use 100% VBA and thus can help you with VBA but not with macros. 'Some' Macros however can be used in web databases whereas VBA cannot.

If you want to try the VBA approach, say so, and we can try to work something out.
Jan 13 '13 #7
dgaletar
72 64KB
OK TheSmileyCoder, I'm at home for the weekend so I can't try this right now. But I'm working on the first part of your response first. I just read the article you posted and I think that I get it. I think what I need to do is to have the CUA# as the foreign key in the "Service" table. That should relate the multiple service record details to the single vehicle like you suggested.

Does that sound right?

And, if it does, wouldn't that rectify my "button" problem?
Jan 13 '13 #8
zmbd
5,501 Expert Mod 4TB
Before we go any further with this project it is very important that we know which version of Access you are working with! Things in V2003 been changed in V2007 and then again in V2010 - especially the macro language! There are things that you can now do (such as error trapping and use of the temporary variables in V2010) There are also some changes in the VBA and so forth.
Jan 14 '13 #9
dgaletar
72 64KB
My work establishment is currently using Access version 2010.
Jan 14 '13 #10
TheSmileyCoder
2,322 Expert Mod 2GB
dgaletar
I think what I need to do is to have the CUA# as the foreign key in the "Service" table. That should relate the multiple service record details to the single vehicle like you suggested.

Does that sound right?
Spot on, that is correct.

Now what your macro is CURRENTLY doing is opening the form, and filtering to only display service records matching the vehicle. However your missing the part where you make sure that any service records added gets linked to the vehicle. I.e. we need to fill in the CUA# automatically, by setting the defaultvalue each time the form is opened.



The following is the VBA code approach
I always prefer to name my controls so its obvious what kind of control I am dealing with. Personally I would use tb_CUA for the CONTROL bound to the FIELD CUA# (And I would not use # or other special charecters in field or control names). In this cause both the Vehicle details form and the service details form would have a CUA#, and thus a tb_CUA. Notice that the 2 fields should be the same type of field, unless the CUA# is a Autonumber field in the Vehicle Details form. In that case CUA# in the service details form should be a Number(Long) type field.

With your form, make sure that AutoResize and AutoCenter and PopUp are all true/Yes, and that modal and AllowFilters is false/no. Make sure the tb_CUA in the Service Details form is Locked (To prevent user manually changing the number).

Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_OpenServiceDetails_Click()
  2.    'Open Form (Notice the frm prefix to let the coder know that its a form)
  3.      'Notice that form is opened in ADD mode, and with a filter applied.
  4.      DoCmd.OpenForm "frm_ServiceDetails", acNormal, , "[CUA#]=" & Me.tb_CUA, acFormAdd
  5.  
  6.    'Manipulate the form
  7.      Dim f as Form
  8.      Set f=Forms("frm_ServiceDetails") 
  9.  
  10.      'Set defaultvalue so that all records automatically get assigned the new id
  11.        f.tb_CUA.DefaultValue = Me.tb_CUA
  12.  
  13.    'Set the form to be modal. This will prevent the user from clicking outside the form, until the form is closed again. 
  14.       f.Modal = True
  15.  
  16. End Sub
Now you may ask, where should this code go? Well I presumed you have a button called btn_OpenServiceDetails. Select your button, name it as you see fit (Give it a meaningfullname), select the Event tab, and select the On Click property. You might allready see your macro there, if you are using the old button. Remove the macro, click the 3 dots (builder icon) on the right hand side. That should open VBA editor and show you:
Expand|Select|Wrap|Line Numbers
  1. Private Sub YOURBUTTONNAME_Click()
  2.  
  3. End Sub
and that is where you put the code.

After you have pasted the code, I strongly urge you to try and highlight/select various bits of the code as the OpenForm, and press F1 for additional information.


I hope you found this helpful, and not to confusing.I am sure there is a macro way of accomplising something similar, but I prefer the more detailed control I get using VBA.
Jan 14 '13 #11
dgaletar
72 64KB
OK, TheSmileyCoder, I feel like I am right on the brink!!! But, as usual, I am lost. I'm getting stuck at this sentence:

"Personally I would use tb_CUA for the CONTROL bound to the FIELD CUA# (And I would not use # or other special charecters in field or control names)."


Are you saying any of the following:
  • that, in the "SERVICE : Table", instead of using CUA# I should use tb_CUA?
  • or that, in ALL tables do that?
  • or that I should name the command button tb_CUA?

I guess I am lost at "the CONTROL bound to the FIELD CUA#". What is the "control" and how is it "bound" to the Field CUA#?

Otherwise I think I'm there!
Jan 14 '13 #12
TheSmileyCoder
2,322 Expert Mod 2GB
Good to hear, and thank you for providing such a precise response as to the parts you are confused about. It makes it much easier to avoid mis-understandings.


In tables, we have fields (sometimes called columns). A form has controls, which can be named. One (of many) properties of a control is the Controlsource.

Now if form frm_ServiceDetails is based on the table tbl_ServiceDetails then we can specify that the control(*) tb_CUA's controlsource is CUA#. You will often hear that the form is BOUND to the table tbl_ServiceDetails and that the control is BOUND to the FIELD CUA#.

When a control is bound it means that if we make edits while in form view, those edits carry through to the table.

So to deliver a more precise reply to your question:
Your table has a field named CUA#. I would recommend that the TEXTBOX control on both your forms, is named tb_CUA (tb shorthand for TextBox).

I would recommend the button gets called either btn_ServiceDetails (btn short for button) or cmd_ServiceDetails (cmd short for command). Note that the button NAME is different from the buttons CAPTION. The caption is what the users see on top of the button. The name is what you see when you look at your code. If you don't give the button a name your code will look like
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.  
  3. End Sub





(*) A control is usually one of the following: TextBox,Combobox, ListBox, Checkbox,Option Group, Image control, Subform Control
Jan 14 '13 #13
dgaletar
72 64KB
OK, I changed the names of the CUA# Controls for both forms (thanks to your expert explanation), saved them, and tried the button again and got this error:

Compile Error
Syntax Error
f.Modal = TrueEnd Sub
Jan 14 '13 #14
dgaletar
72 64KB
I GOT IT!!! IT WORKED!!! I forgot to change the name of the form!

Thank you SOOOOO much for sticking with me on this one!
Jan 14 '13 #15
zmbd
5,501 Expert Mod 4TB
Sorry, I should have added the following link in my prior post too: Before Posting (VBA or SQL) Code

Of importance at this point is the first section "A"
By following the directions there, you will help to avoid some of the compile errors at run-time... such as the one you just encountered.
Jan 14 '13 #16
TheSmileyCoder
2,322 Expert Mod 2GB
Thats lovely to hear. Its alot easier to work with people(like you) who can properly explain their question, and provide relevant details.
Jan 14 '13 #17
dgaletar
72 64KB
I couldn't have done it without your help TheSmileyCoder! Thank you.

Now, are you any good with "Aggregate Queries"? NeoPa is driving me nuts with his advanced way of instructing! I know he's trying, but he just doesn't get me.

Here's the thread: http://bytes.com/topic/access/answer...-our-inventory

If not, it's OK. I'll eventually figure it out. :)
Jan 14 '13 #18
zmbd
5,501 Expert Mod 4TB
dgaletar:

You might look at cross tab query; however, you will need to start a new thread for that question. Both Rabbit and TheSmileyCoder are magicians with the SQL!

You might also take a look at my reply in the thread you note in post #18.
Jan 14 '13 #19
dgaletar
72 64KB
I found it! http://www.worldbestlearningcenter.c...e-function.htm worked perfectly! Thanks Bytes forum people. Hope to be back real soon!!!
Jan 14 '13 #20

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

Similar topics

7
by: Jeff | last post by:
When you connect to an Oracle database with Access and ADO, is there a way to get information on the Oracle table relationships? Jeff
1
by: abefuzzleduser2 | last post by:
What is the best way to setup relationships between one lookup table and many other tables. The tables did not have any lookup table relationships which I am adding. One lookup table is used for...
20
by: Ed | last post by:
I am running Access 2002 and just ran the built in Access wizard for splitting a database into a back end (with tables) and front end (with queries, forms, modules, etc.). After running the...
3
by: GGerard | last post by:
Hello Does anyone know if it is possible with Access 2000 to create relationships between tables using code? Thanks G.Gerard
1
by: Gandalf | last post by:
I'm creating relationships between tables using VBA and ADOX. I can create one-to-one relationships with an inner join, but I can't figure out how to create these relationships with an outer join...
6
by: GaryDave | last post by:
My school registration database has not been quite right after a recent compact and repair (done while I was away). Though most of the many forms and subforms are working normally, one form in...
2
by: Haas C | last post by:
Hi all, I'm trying to create a relationship between two tables with related fields (Employee), however when I try to Create the join in Relatioships, the 'Edit Relationships' box appears and it...
3
ebs57
by: ebs57 | last post by:
I am looking for some basic help in understanding and setting up table relationships in Access. I've created one table called PROJECTS and it has the field JobNo which I've declared as the key...
1
by: =?Utf-8?B?RU1hbm5pbmc=?= | last post by:
I'm a newbie to VB.Net so I hope I'm using the right terminology. I'm in the process of converting a faculty database from Access to VB.Net (2005). I have one table for general information and...
3
by: anjee | last post by:
Hello, Is it possible to create multiple foreign keys on a field in a table from values in two separate tables where the field value can be from one table OR the other? For example, I have an...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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
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...

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.