MS Access 2007 - Semi-Autonumber - ms-access-2007

I am building my own CRM access database that will contain a list of contacts. I would like to have an opportunity reference one contact as an employee, and another as the customer. Is there a way that I can add employees using their internal user ID (always 4 digits) and autonumber customer contacts?
I don't plan on building forms until I am comfortable that everything is working properly in table view.

You cannot assign an ID to an autonumber field. I might suggest adding an employeeID field to your contacts table that would have the employee ID number. The contact ID would still be an autonumber, so in essence they would have two IDs, but their employeeID would need to be stored in a different field.
You can try something different, but you will likely run into a duplicate ID at some point. Use a seperate table for autonumber Customer IDs. When adding a customer into your contact table, you "insert" a new record into the separate customer ID table to get the autonumber ID. If you're inserting an employee, just use the EmployeeID as the contact ID. So you need to be very careful when mixing two ID systems. It's best to use 1 ID and then use the other ID in some other field.

Consider using two tables, one for contact fields and another for employee-specific fields. Put your AutoNumber field in the contact table, and put the employee id in the employee table. In the employee table, use a foreign key that refers to the AutoNumber field in the contact table.

Related

MS Access duplication

I got my query in MS Access duplicating its errors. The data is being entered from a Windows Forms app, and the data is in two separate tables. I'm using a basic query to show each row in the table. Name table and score table.
Now the issue is that the data is duplicating itself.
Image of the access table
This is the SQL query:
SELECT DISTINCT score.score, name.Name
FROM name, score;
Table Schema
What SQL code could I use to stop the duplication error?
Thanks in advance
Need to add a long integer number field StudID in Scores table then do data entry to add the appropriate student identifier value into each Scores record. This will establish a primary/foreign key relationship between tables that can be used in query with a LEFT or RIGHT JOIN on key fields.
This assumes a student can have multiple scores (for what?). If there can be multiple scores, then probably also need a date field ScoreDate and then possibly should be a Subject field.
If student can have only 1 score, then don't need Scores table - have a field Score in Students table.
Strongly advise not to use reserved words as names for anything. Name is a reserved word.

SQL Referencing a particular table chosen from a row value in another table

I've got a database called SimpleCredentials in which there is a table called dbo.Properties which has (PK) UserID and then some other attributes like Name, Date of Birth etc. There is another Primary Key attribute called ExtendedCredentials which is a string dbo.UserID. So, for example the user with UserID = S-1-5-21-2177 will have the string dbo.S15212177 in their ExtendedCredentials column.
I've got another database called ExtendedCredentials. For every User there is a unique table in that database. Using the previous example, there will be a table called dbo.S15212177.
So, if I have 100 users there will be 100 rows in the dbo.Properties table in the SimpleCredentials database, and there will be 100 unique tables incorporating their UserID in the ExtendedCredentials database.
I want to create an entity relationship diagram, and eventually a MS SQL schema, but how do I represent the multiplicity of dbo.UserIDs and their relationship to their dbo.UserID string attribute in dbo.Properties?
Am I getting something fundamentally wrong here?
You may ask why I don't have a single database called ExtendedProperties with a single table in which each row is the UserID PK and the various extended properties are contained in columns. The simple answer is that some properties are themselves tables. Not every user has the same attributes in those tables. And I can't know ahead of time (a priori) what the full set of user extended property attributes is. So each user gets a table of their own.
Is there a better way to do this?

Sql server, insert data into two tables, populate one before the other

My first post here as I am starting in a new job where my old sql skills already are on the test - I am not advanced user from before either.
I believe there might be some answers here already that might be answering my question, but I am still a bit unfamiliar - both with the forum and more advanced sql syntaxes. Some answers are pretty old as well.
And - please excuse me for any grammatical errors.
Anyways, if anyone might be able to help.
I will be receive huge lists (.csv or similar) with data input.
The datalists will contain fields for type customerdata (name, address etc) and fields for type real estate/property data (street address, buildingIDs etc).
The customerdata and propertydata needs to be put into two separate tables.
My problem is like this:
- The two tables are depended on eachother as in the propertydata table needs to be populated with data first, which will generate a unique GUID - which again will be used when populating the customerdata table - connecting the customer to the correct property/real estate.
The first thing that came to mind is to populate all data into a temporary table.
But I am not quite sure how to loop through each row so I ensure that propertytable is populated first, then the customertable using the GUID.
Get data that involves propertydata and populate the property table
Get the unique GUID generated in property table
Get data that involves customerdata and populate the customer table - with correct GUID
Loop through rest of the set until there are no more rows with data.
I have seen some things like transactions, cursors, output etc that seems to be within my scope, but not sure which would be best way to solve my challenge? Am I near something when thinking like that?
EDIT:
These are example fields that will come as one row in .xlsx/.csv format.
Number of rows in such list will vary from time to time.
Property ID
Property address
Property building ID (only one)
Property established date
...Misc other property related fields
Customer ID
Customer Name
Customer Address
Postal code
...Misc other customer related fields
Fields 1 through 5 will need to populate a property table first. When each row in property table is populated it will generate unique GUID.
Then fields 6 through 10 will be used to populate a customer table, but also need to be populated with the corresponding unique GUID created above in the property table.
Property table:
Property ID
Property address
Property building ID (only one)
Property established date
...Misc other property related fields
UNIQUE PROPERTY GUID (created when populating each new row in table)
Customer table:
UNIQUE PROPERTY GUID
Customer ID
Customer Name
Customer Address
Postal code
...Misc other customer related fields
I suggest you use a staging table.
Load all data into a staging / temporary table and assign GUID numbers to each row.
Copy property details from staging table to property table taking GUID from the staging table
Copy customer details from staging table to customer table taking GUID from the staging table.
Delete data in staging table.
A quick example:
INSERT INTO PropertyDetails( GUID, PropertyID, PropertyAddress, ... )
SELECT GUID, PropertyID, PropertyAddress, ...
FROM StagingTable
INSERT INTO CustomertDetails( GUID, CustomerID, CustomerName, ... )
SELECT GUID, PropertyID, PropertyAddress, ...
FROM StagingTable

Inserting data into two different tables with SQL Server 2008?

I have three tables in my database: Contacts (master table), Users and Staff. The Contacts table has a primary key that is referenced as a foreign key from the Users and Staff tables.
Contacts
ContactID (Primary Key)
First Name
Last Name
Email
Users:
ContactID (Foreign Key)
Username
Password
Staff:
ContactID (Foreign Key)
Position
Comments
Pulling data from these tables is fairly simple with INNER JOIN and depends what data you need from the Users or Staff table. Updating and inserting new records is something that I'm not sure which way to approach.
Since the Contacts table is my master table and holds unique ID's but at the same time is shared between Users and Staff, I'm not sure about this situation.
Let's say I want to enter new user record. I need to enter First, Last name and email into the Contacts table, and User Name and Password into the Users table. At the same time I have to check if First, Last name and email already exist in the Contacts table, since maybe this Contact record has been previously entered for a Staff record.
I'm not sure how to prevent duplicates but at the same time there might be user or staff with the same name. Should I limit/filter by email and not let them enter the records if the email already exist in contacts table? Or is there a better way to handle this?
The whole point of having three tables is to prevent redundant data since User might be a Staff, but Staff might not be a User if that make sense. If anyone can help me pick the best approach please let me know. Thanks
You have to start by using a SELECT query and whatever business rules you want to test to decide if you will perform the INSERT at all, or UPDATE an existing record instead.
Then if you are going to INSERT, you have to INSERT into Contacts first, get the newly inserted ID, and then INSERT into the other two tables.

Assign unique ID to duplicates in Access

I had a very big excel spreadsheet that I moved into Access to try to deal with it easier. I'm very much a novice. I'm trying to use SQL via Access.
I need to assign a unique identifier to duplicates. I've seen people use DENSE_RANK in SQL but I can't get it to work in Access.
Here's what I'm trying to do: I have a large amount of patient and sample data (20k rows). My columns are called FULL_NAME, SAMPLE_NUM, and DATE_REC. Some patients have come in more than once and have multiple samples. I want to give each patient a unique ID that I want to call PATIENT_ID.
I can't figure out how to do this, aside from typing it out on each row. I would greatly appreciate help as I really don't know what I'm doing and there is no one at my work who can help.
To illustrate the previous answers' textual explanation, consider the following SQL action queries which can be run in an Access query window one by one or as VBA string queries with DAO's CurrentDb.Execute or DoCmd.RunSQL. The ALTER statements can be done in MSAcecss.exe.
Create a Patients table (make-table query)
SELECT DISTINCT s.FULL_NAME INTO myPatientsTable
FROM mySamplesTable s
WHERE s.FULL_NAME IS NOT NULL;
Add an autonumber field to new Patients table as a Primary Key
ALTER TABLE myPatientsTable ADD COLUMN PATIENT_ID AUTOINCREMENT NOT NULL PRIMARY KEY;
Add a blank Patient_ID column to Samples table
ALTER TABLE mySamplesTable ADD COLUMN PATIENT_ID INTEGER;
Update Patient_ID Column in Samples table using FULL_NAME field
UPDATE mySamplesTable s
INNER JOIN myPatientsTable p
ON s.[FULL_NAME] = p.[FULL_NAME]
SET s.PATIENT_ID = p.PATIENT_ID;
Maintain third-norm principles of relational databases and remove FULL_NAME field from Samples table
ALTER TABLE mySamplesTable DROP COLUMN FULL_NAME;
Then in a separate query, add a foreign key constraint on PATIENT_ID
ALTER TABLE mySamplesTable
ADD CONSTRAINT PatientRelationship
FOREIGN KEY (PATIENT_ID)
REFERENCES myPatientsTable (PATIENT_ID);
Sounds like FULL_NAME is currently the unique identifier. However, names make very poor unique identifiers and name parts should be in separate fields. Are you sure you don't have multiple patients with same name, e.g. John Smith?
You need a PatientInfo table and then the SampleData table. Do a query that pulls DISTINCT patient info (apparently this is only one field - FULL_NAME) and create a table that generates unique ID with autonumber field. Then build a query that joins tables on the two FULL_Name fields and updates a new field in SampleData called PatientID. Delete the FULL_Name field from SampleData.
The command to number rows in your table is [1]
ALTER TABLE MyTable ADD COLUMN ID AUTOINCREMENT;
Anyway as June7 pointed out it might not be a good idea to combine records just based on patient name as there might be duplicates. Better way will be treat each record as unique patient for now and have a way to fix patient ID when patient comes back. I would suggest to go this way:
create two new columns in your samples table
ID with autoincrement as per query above
patientID where you will copy values from ID column - for now they will be same. But in future they will diverge
copy columns patientID and patientName into separate table patients
now you can delete patientName column from samples table
add column imported to patients table to indicate, that there might be some other records that belong to this patient.
when patients come back you open his record, update all other info like address, phone, ... and look for all possible samples record that belong to him. If so, then fix patient id in those records.
Now you can switch imported indicator because this patient data are up to date.
After fixing patientID for samples records. You will end up with patients with no record in samples table. So you can go and delete them.
Unless you already have a natural key you will be corrupting this data when you run the distinct query and build a key from it. From your posting I would guess a natural key would be SAMPLE_NUM. Another problem is that if you roll up by last name you will almost certainly be combining different patients into one.