Set Two Foreign Keys, That Refer To The Same Table/field

Feb 22, 2011

I have an Access database where I want to store the contacts in a company: In this database I have a table named "users" where I keep the users which will have access to the database. In the main table, named "contacts", two of my fields "insert_user" and "contact_user" refer to the user who entered the contact information and the user who actually made the contact. The way I am thinking it, I will have to set both of the fields as foreign keys to the same table/field, namely "users"/"username".

So:
I set two foreign key constrains in two different fields ("insert_user", "contact_user"), which refer to the same table/field ("users"/"username") in an
Access database, using VB.Net OleDB (See the SQL Statement below)

ALTER TABLE contacts ADD CONSTRAINT insert_user_foreignkey FOREIGN KEY (insert_user) REFERENCES users (username)
ALTER TABLE contacts ADD CONSTRAINT contact_user_foreignkey FOREIGN KEY (contact_user) REFERENCES users (username)

I open the database with MS-Access. I see that all the tables are fine (as they should be).

However when I click on relationships at Access I get all my tables plus a table named users_1 connected to "contacts"/"contact_user" (the table users is only connected to "contacts"/"insert_user")

It is very strange that in the Table View of Access I don't see this table, but only in the Relationships View.

Questions

Is my thinking right (to set 2 foreign keys, that refer to the same table/field)? What is this table "users_1"? Will my database work or am I doing something wrong?

View 1 Replies


ADVERTISEMENT

VS 2008 Getting Field Values From A Table Using A Foreign Key?

Jul 13, 2011

For simplicity, I have a database with two tables, as follows:

/---------------------------------------
| Table Name: GRADES |
|---------------+---------+-------------|
| Column | Type | Comments |
|---------------|---------|-------------|

[code]....

On my form, I'm trying to display information about a grade. I'm using a DataSet with BindingSources and TableAdapters. Using the tip from here, I'm populating the selected grade's ID, HazardType_ID and Code. Unfortunately, I want to display the the HazardType, and not the foreign key index. I've accomplished this with the following

' get the data
Dim data As ProjectDataSet.GradesRow = DirectCast(GradesBindingSource.Current, DataRowView).Row
' fill in form fields

[code]....

Note, I'm using VB Express 2008 (.NET 3.5) and connecting to a MS Access database.

View 6 Replies

Displaying Text Fields Linked To By Foreign Keys?

May 8, 2009

I have a three-tier Windows Forms DB application in VB.NET. I'm using VS 2005. I'd like to display records in a table in a DataGridView. I can already display the records as is by binding the DataSource to the business class that talks to the DB class:
Dim assetList as List(Of Asset)
assetList = DB_Asset.GetAssetListOrderByID_Asset
AssetDataGridView.DataSource = assetList

"Asset" is my business class, and "DB_Asset" is my DB class that queries the DB to return assetList. Now, Asset has members something like this:
Private m_ID_Asset As Integer
Private m_CategoryID As Integer
Private m_CustodianID As Integer
Private m_ManufacturerID As Integer
Private m_SignedOutToID As Integer
Private m_DefaultLocationID As Integer
Private m_CurrentLocationID As Integer
Private m_DateAcquired As Date
Private m_DateEntered As Date
Private m_EnteredByID As Integer

m _ ID _ Asset contains the primary key for the Asset table in the DB, and everything else of the form m_XXXXXXXXXID contains a foreign key to another table in the DB. So basically what I get now is rows with a whole lot of numbers. It's exactly what's in the Assets table:

ID_Asset CategoryID CustodianID ManufacturerID SignedOutToID
1 17 23 14 5

What I'd like to know is if there's an easy way to display the text fields that I've linked to with all of those foreign keys:

ID_Asset CategoryName CustodianName Manufacturer SignedOutTo
1 Soda - Diet John Coca-Cola Fred

View 1 Replies

C# - Joint Query In Linq To SQL To Select All Records Even There Are Some Null In The Columns Of Foreign Keys?

Feb 2, 2011

I'd like to take all suggestion either in C# or VB.NET.I have a DB diagram like the image below. I also include the database script here [URL]In the Students table, CountryId and RoomId column are allowed null. Because some records do not have info about room and country yet.Also, some students do not have essays.I'm doing a joint query with all tables. I want to select all students to project the result like this:Wanted query result.

Here's my current query that gives the result like the image below:

Dim db As New DBDataContext
Dim query = From st In db.Students _
Join c In db.Countries On c.Id Equals st.Id _
Join r In db.Rooms On r.Id Equals st.RoomId _

[code].....

current query result I got only one result back because I have (2) William NoMan record in every table. But I don't get anything about others, like (3) Sync Master who has everything but RoomId.What do I need to modify the query above so it will give me all students like in the wanted query image above?

View 2 Replies

Identify Multiple Rows With The Same Value In A Foreign Key Field?

Oct 8, 2010

I have two tables:

*Transaction Record

*Account Details

Account details table has the field "Account Number" as its primary key.

Transaction record has the field "transaction number" as its primary key and "account number" as its foreign key. This is a table created in ms access. I am creating a program in vb.net to maintain this database.

When i try to find rows using an oledb_data_adapter, an error occurs stating that there are muliple values.

i think this is due to the fact that the records in the transaction details table are being selected by the account number. Many records in the transaction details table can have the same account number.

i need a coding to help me display the most recent 10 transaction records of one account number, one after the other.

P.S: transaction record table also contains "transaction date" field.

View 1 Replies

Refer To A Database Field Insted Of A Texbox Or Label Text?

Feb 22, 2010

How can I Refer to a Database Field insted of a Texbox or Label text?

I have tried somting like this, but it does not work. Can some one point me in the wright way?

If TableName,FieldName <> Nul Then
. . . . . . . . . . . . . . .
End If

View 3 Replies

Replace Foreign Key With Value In Table?

Dec 30, 2010

Is there a simple way that I can replace a foreign key in a datatable, with a corresponding value from the table that the key is referring to, without having to create a new table or use tableadapter? Ive got a table that has a foreign key to a child table, and I need to get a column from that table to fill a dataview?

View 4 Replies

Searching Another Table Not Using Foreign Key?

Nov 12, 2009

I have an application where I need to be able to search for the workers related to a company from a contacts list. I am able to do this by entering the company number but I am unsure how I can set the query to allow to search for the information in the other table.

[Code]...

SELECT RecordNo, CompanyID, AccntRep, CustNumber, Person, SubjectID, StatusID FROM dbo.Contact
WHERE CompanyID = @companyNo I think that I need to create a join in the SQL statement to the company table but am unsure how to go about this and I would be grateful if someone would give me a point in the right direction so that i can search for the company name instead of the companyID.

View 2 Replies

.net - Getting The Foreign Key Constraints Of A Table Programmatically?

May 17, 2012

I'm trying to use a DataTable to get the schema of a SQL Server DB.But, when try to detect the ForeignKeys, the constraints collection only brings the UNIQUE constraints.

Private Sub ShowConstraints(ByVal tableName As String)
Dim table As DataTable = New DataTable(tableName)
Using connection As SqlConnection = New SqlConnection(GetConnectionString)

[code]....

How can I get the ForeignKey Constraints?

View 2 Replies

VS 2008 Insert Autonumber In Foreign Table

Aug 17, 2011

I want to insert data into my tables. Table 1 (Recipes1) has a primary key (Autonumber) Table 2 (Ingredients) has a Foreign key with the same name as the primary key in Table2 ("RecipeNumber) this is a normal number field. The problem is that when i update the tables, it does not even make it past table1 , which should be ok.

[Code]...

View 6 Replies

Asp.net - Select Query In LINQ Based On Foreign Table

Mar 2, 2011

I have 2 Tables , OrderDetails and Requests In my LINQ to SQL dbml file. OrderDetailsID is a foreign key in Requests Table.

I want to write the query to get the sum of UnitCost from OrderDetails based on OrderId. And If there is a row in Requests Table for each OrderDetailsID, and the Requests.Last.RequestType="Refund" I want to reduce the total refund amount from the main sum otherwise If there is no row based on OrderDetailsID, add to sum.

Here is the way I implement that. I am looking to prevent using "For each".

iRefund = (From od1 In dc.OrderDetails _
Where od1.OrderID =1 _
Select od1.UnitCost).Sum

[Code]....

View 1 Replies

Select Query In LINQ Based On Foreign Table?

Mar 4, 2009

I have 2 Tables , OrderDetails and Requests In my LINQ to SQL dbml file.OrderDetailsID is a foreign key in Requests Table.I want to write the query to get the sum of UnitCost from OrderDetails based on OrderId.And If there is a row in Requests Table for each rderDetailsID, and the Requests.Last.RequestType="Refund" I want to reduce the total refund amount from the main sum otherwise If there is no row based on OrderDetailsID, add to sum

View 3 Replies

Sql Server - SQLException Foreign Key Error Getting Child Table

Jun 14, 2012

I'm currently trying to create a function to parse error messages to make them more helpful for the end user. Currently I'm working with SQLServer and VB.NET.

Right now I'm raising error 547 which looks like this:

DELETE statement conflicted with COLUMN REFERENCE
constraint Constraint Name. The conflict occurred
in database 'Database Name', table 'Table Name',

[Code].....

I'm able to pull every piece of information I need from the error message except the name of the parentTable. I've already determined that the SqlException doesn't store the statement that caused the exception, and as far as I can tell it doesn't store information about the name of the parent table, only the table that is trying to reference it.

Is there an easy way to get the name of the parentTable?

View 1 Replies

Foreign Key Constraint Not Updating Child Table On Primary Key Change?

Jul 20, 2009

I am using a number of tables on my application and two of which have a foreign key constraint I have set between them. When I change the account number on the main table via a combo box all of the relevant information for that table on the form is updated accordingly. However, the child table which has a foreign key reference to the Account Number of the main table is not updated when the acocunt number is changed. I have set the update to cascade in the references but still am having issues.

View 5 Replies

Warning Messagebox Appears When Deleting A Record Which Has A Foreign Key To Another Table?

Apr 24, 2009

I would like to have a pop up messagebox that asks users for confirmation before deleting a record in datagridview and if the record is a foreign key to another table, another messagebox will appear and warns users saying" before you can delete this record, make sure you remove all the related records" I have tried but I don't know how to complete it.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.StaffTableAdapter.Fill(Me.StaffDBDataSet.Staff)
BindingNavigator1.DeleteItem = Nothing
End Sub

[code]....

View 6 Replies

How To Refer To Text In MS Access Table

Apr 19, 2010

I want to refer to a data cell, which if it is equals to some string, it will do something.
The codes:
If ds.Tables(0).Rows(i)("Status") = "Reserved" Then
MessageBox.Show("Can't reserve")
End If
Is this the correct way to do this? Because I failed doing so..

View 2 Replies

Check If A Field In A Data Table Is Null Before Creating A Textbox Bound To That Field?

Feb 24, 2010

I am building a data based application using VB 2008 an SQL Express. I need to create textboxes on my form using code, (With & End With) method. I need a simple code string that will allow the app to check if the field to wich the textbox wil be databound is Nul, If so the textbox will not be created.

View 8 Replies

Transferring Data From One Field In A Table In A Database To Another Field In A Different Table But Same Database In .NET?

Jul 22, 2010

I am creating a database in VB.NET for a movie rental place. I currently have three forms;

Member Information
DVD Information
Borrow DVDs

What I would like to do is when I am viewing a member's details, if I click a button 'Borrow DVD for Member', the member's ID number transfers over to the Borrow DVDs table in the Member ID which also would hold some information from the DVD Information table, but I'm sure if i can figure out how to do this firstly, I will be able to apply the same rule and work it out myself.I have been trying to use a query statement like;

INSERT INTO [Borrow DVDs].[Member ID] [IN goodstuffvideos.mdb]
SELECT [Member Information].[Member ID]
FROM [Member Information]

but that has been coming up with error codes and completely not working.

Borrow DVDs table fields are: Borrow ID, Member ID, DVD ID Number, Hiring Fee, Borrowing Limit?

DVD Information table fields are: DVD ID Number, Title, Rating, Hiring Fee, Borrowing Limit Member?

Information table fields are: Member ID, Family Name, Given Name, Address, Town/Suburb, Postcode?

The error coming up is; Error in INSERT statement. Unable to parse query text.And under that it says The query cannot be represented graphically in the Diagram and Criteria Pane.

View 1 Replies

Display Data Stored In A Sql Sever DB Table, The Table Has A Field With 6 Values?

Jul 8, 2009

I currently have this working already for one of my combo box's, but when i try to use the same code i get this error ''There is no row at position 6.'' . The are 2 fields in the table asset_type_id, asset_type_name', im trying to dispaly the values in a combo box for the field 'asset_type_name.

the code for ' Friend Sub RetrieveCustomerInformation2()' works but the other friend sub throws the error.
Imports System.Data.SqlClient
Imports System.Data
Imports System
Imports System.Data.OleDb

[Code]...

View 3 Replies

SQL Server Query Combination - Get Rows From A Table Based On The Value Of A Field In Another Table

Feb 23, 2011

I have a query that I have to run that requires me to get rows from a table based on the value of a field in another table (which I have gotten earlier). I currently have it done like this (simplified).

[Code]...

View 1 Replies

Game Programming :: Detecting Whether One Of The Arrow Keys Is Pressed, All The Other Keys Seem To Be Detected Apart From The Arrow Keys?

Oct 21, 2008

I have a problem detecting whether one of the arrow keys is pressed, all the other keys seem to be detected apart from the arrow keys??? I have set key preview to True........ It detects the arrrow keys BUT ONLY if I have NO other controls on the form??? example..

Me.Text = e.keycode

It works, but then if I add a button for example, it stops.

View 3 Replies

Sql Server :: Display In A Bounded Datagridview A Field From Another Table Related To The Bounded Table?

Jan 20, 2012

i two tables named tblaccess(nID,nRoleID,nModuleID,cAccess) andtblmodule(nModuleID,cModuleName) and i have a datagrid which is bounded to tblaccess. the problem is, I want to display cModuleName from tblmodule instead of nModuleID. please tell me how.

View 1 Replies

Store Data From A Table That Has Multi-dimensional Keys To Get A Single Value Result?

Feb 2, 2012

What type of collection should I use to store data from a table (in a book) that has multi-dimensional Keys to get a single value result?

What is the best way to store and retrieve these values in a program?

View 9 Replies

Update Using Table Adapter - "Update Statement Conflicted With Foreign Key Constaint....."

Apr 5, 2010

I have a form with comboboxes, if the user doesnt select a value in the box I want a null to be sent to the database. The insert works fine, i look at the record in the db and see the null. The update doesnt work. gives me a n "Update statement conflicted with foreign key constaint....."

Public Sub Update()
TableAdapterCreate().Update(ID1, ID2, CType(cbox1.SelectedValue, Integer), CType(cbox2.SelectedValue, Integer), CType(cbox3.SelectedValue, Integer), CType(cbox4.SelectedValue, Integer),

[CODE]...

As I step through the selected value of the cbox 5 is shown as nothing (which it should be because the user didnt select a value for this combobox). I assume this needs to be a null instead of nothing to get written to the database. the combox boxes need to match a primary key in another table so sending a 0 would also result in the forieign key error since the related table does not have a 0 ID field.

View 4 Replies

For Each Field In Mysql Table?

May 8, 2010

still having problems with mysql... im trying to find out how i can you a for each statement when getting data from mysql table...i have a table with IP and Port fields in... i want to get each and every one of the ip's and use it...for each ip in heat table and then run my "query()" function how can i do this in vb.net?... i can get ALL ips and ports and put in a gridview but would like to use the ip and display the Game Server Data that my query() function gets...this is from a while back when i made my Game Server Browser and just tryiong to make a better way of keeping my ip and port data and mysql seams to be the best way as its online and it can be added to and edited as and when needed and no one has to "redownload" anything?

View 3 Replies

Insert One Field From A Table Into Another?

Mar 17, 2012

Is it possible that i would only want to insert one field from a table into another table?

View 6 Replies

Just Need One Field Per Table Using Databases

Apr 23, 2012

I have done plenty of VB programming using flat files etc... Now I am attempting to create a SQL database (I have my sql 2008 express box up and running) I can connect just fine, I made my database, made an account and made it data reader and data writer. I even gave dbowner to my user for testing. I created some tables, and I can access them through the dataset designer in VB 2010. Now what I have been trying to figure out all day.

[Code]...

View 39 Replies

Retrieve A Value From A Field Of A Table?

Feb 2, 2009

I want to retrieve a value from a field of a table. How can I assign this value in the text of a tooltip. The tooltip will appear when the user will move the mouse on a textbox.

View 1 Replies

Add A Field Data Onto An Email Table?

Oct 12, 2010

How can I call my TextBox ID = "txtFromAddress" data to a table that I am trying to send? Right now it is just showing the text '& txtFromAddress' on the table on the email that I send.

[Code]...

View 3 Replies

Asp.net - Displaying A Table's Field In A Textbox?

May 6, 2009

I have a table and I want to select a field in it and then display it in a text box

something like:

SELECT userName
FROM userTable
WHERE (userLogged = 'ON')

how can I display the selected username in a textbox?

BTW the userLogged indicates wether the user is logged in or not

if the user is logged in then the userLogged will be changed to "ON"

if the user is not logged in it will be "OFF"

I know it's not that practical but I'm still practicing.

I'm using Visual Web Developer 2008 Express I use table adapter procedures for querying

View 3 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved