Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
63 views

Insert, Update, and Delete Records From A Table Using Access SQL

Uploaded by

bibliohashoka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Insert, Update, and Delete Records From A Table Using Access SQL

Uploaded by

bibliohashoka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

01/01/2024 17:08 VBA-content/VBA/Access-VBA/articles/insert-update-and-delete-records-from-a-table-using-access-sql.

md at master · OfficeDev/VBA-content · GitHub

This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

OfficeDev / VBA-content Public archive

Code Issues Pull requests Actions Projects Wiki Security Insights

VBA-content / VBA / Access-VBA / articles / insert-update-and-delete-records-from-a-table-using-access-sql.md

John Austin Added friendly name markdown files to replace guid named files 3738bb9 · 7 years ago History

Preview Code Blame 266 lines (127 loc) · 8.21 KB Raw

title ms.prod ms.assetid ms.date

Insert, Update, and Delete Records From a Table Using 0d71f4f1-efc1-127e-5edc-


access 06/08/2017
Access SQL 263a3a2a30fb

Insert, Update, and Delete Records From a Table Using Access


SQL

Inserting Records into a Table


There are essentially two methods for adding records to a table. The first is to add one record at a time; the second is to add many
records at a time. In both cases, you use the SQL statement INSERT INTO to accomplish the task. INSERT INTO statements are
commonly referred to as append queries.
https://github.com/OfficeDev/VBA-content/blob/master/VBA/Access-VBA/articles/insert-update-and-delete-records-from-a-table-using-access-sql.md 1/7
01/01/2024 17:08 VBA-content/VBA/Access-VBA/articles/insert-update-and-delete-records-from-a-table-using-access-sql.md at master · OfficeDev/VBA-content · GitHub

To add one record to a table, you must use the field list to define which fields to put the data in, and then you must supply the data
itself in a value list. To define the value list, use the VALUES clause. For example, the following statement will insert the values "1",
"Kelly", and "Jill" into the CustomerID, Last Name, and First Name fields, respectively.

INSERT INTO tblCustomers (CustomerID, [Last Name], [First Name])


VALUES (1, 'Kelly', 'Jill')

You can omit the field list, but only if you supply all the values that record can contain.

INSERT INTO tblCustomers


VALUES (1, Kelly, 'Jill', '555-1040', 'someone@microsoft.com')

To add many records to a table at one time, use the INSERT INTO statement along with a SELECT statement. When you are inserting
records from another table, each value being inserted must be compatible with the type of field that will be receiving the data.

The following INSERT INTO statement inserts all the values in the CustomerID, Last Name, and First Name fields from the
tblOldCustomers table into the corresponding fields in the tblCustomers table.

INSERT INTO tblCustomers (CustomerID, [Last Name], [First Name])


SELECT CustomerID, [Last Name], [First Name]
FROM tblOldCustomers

If the tables are defined exactly alike, you can leave out the field lists.

INSERT INTO tblCustomers


SELECT * FROM tblOldCustomers

https://github.com/OfficeDev/VBA-content/blob/master/VBA/Access-VBA/articles/insert-update-and-delete-records-from-a-table-using-access-sql.md 2/7
01/01/2024 17:08 VBA-content/VBA/Access-VBA/articles/insert-update-and-delete-records-from-a-table-using-access-sql.md at master · OfficeDev/VBA-content · GitHub

Updating Records in a Table


To modify the data that is currently in a table, you use the UPDATE statement, which is commonly referred to as an update query. The
UPDATE statement can modify one or more records and generally takes this form.

UPDATE table name


SET field name = some value

To update all the records in a table, specify the table name, and then use the SET clause to specify the field or fields to be changed.

UPDATE tblCustomers
SET Phone = 'None'

In most cases, you will want to qualify the UPDATE statement with a WHERE clause to limit the number of records changed.

UPDATE tblCustomers
SET Email = 'None'
WHERE [Last Name] = 'Smith'

Deleting Records from a Table


To delete the data that is currently in a table, you use the DELETE statement, which is commonly referred to as a delete query. This is
also known as truncating a table. The DELETE statement can remove one or more records from a table and generally takes this form:

DELETE FROM table list

https://github.com/OfficeDev/VBA-content/blob/master/VBA/Access-VBA/articles/insert-update-and-delete-records-from-a-table-using-access-sql.md 3/7
01/01/2024 17:08 VBA-content/VBA/Access-VBA/articles/insert-update-and-delete-records-from-a-table-using-access-sql.md at master · OfficeDev/VBA-content · GitHub

The DELETE statement does not remove the table structure—only the data that is currently being held by the table structure. To
remove all the records from a table, use the DELETE statement and specify which table or tables from which you want to delete all the
records.

DELETE FROM tblInvoices

In most cases, you will want to qualify the DELETE statement with a WHERE clause to limit the number of records to be removed.

DELETE FROM tblInvoices


WHERE InvoiceID = 3

If you want to remove data only from certain fields in a table, use the UPDATE statement and set those fields equal to NULL, but only
if they are nullable fields.

UPDATE tblCustomers
SET Email = Null

Inserting Records into a Table


There are essentially two methods for adding records to a table. The first is to add one record at a time; the second is to add many
records at a time. In both cases, you use the SQL statement INSERT INTO to accomplish the task. INSERT INTO statements are
commonly referred to as append queries.

To add one record to a table, you must use the field list to define which fields to put the data in, and then you must supply the data
itself in a value list. To define the value list, use the VALUES clause. For example, the following statement will insert the values "1",
"Kelly", and "Jill" into the CustomerID, Last Name, and First Name fields, respectively.

https://github.com/OfficeDev/VBA-content/blob/master/VBA/Access-VBA/articles/insert-update-and-delete-records-from-a-table-using-access-sql.md 4/7
01/01/2024 17:08 VBA-content/VBA/Access-VBA/articles/insert-update-and-delete-records-from-a-table-using-access-sql.md at master · OfficeDev/VBA-content · GitHub

INSERT INTO tblCustomers (CustomerID, [Last Name], [First Name])


VALUES (1, 'Kelly', 'Jill')

You can omit the field list, but only if you supply all the values that record can contain.

INSERT INTO tblCustomers


VALUES (1, Kelly, 'Jill', '555-1040', 'someone@microsoft.com')

To add many records to a table at one time, use the INSERT INTO statement along with a SELECT statement. When you are inserting
records from another table, each value being inserted must be compatible with the type of field that will be receiving the data.

The following INSERT INTO statement inserts all the values in the CustomerID, Last Name, and First Name fields from the
tblOldCustomers table into the corresponding fields in the tblCustomers table.

INSERT INTO tblCustomers (CustomerID, [Last Name], [First Name])


SELECT CustomerID, [Last Name], [First Name]
FROM tblOldCustomers

If the tables are defined exactly alike, you can leave out the field lists.

INSERT INTO tblCustomers


SELECT * FROM tblOldCustomers

Updating Records in a Table


To modify the data that is currently in a table, you use the UPDATE statement, which is commonly referred to as an update query. The
UPDATE statement can modify one or more records and generally takes this form:

https://github.com/OfficeDev/VBA-content/blob/master/VBA/Access-VBA/articles/insert-update-and-delete-records-from-a-table-using-access-sql.md 5/7
01/01/2024 17:08 VBA-content/VBA/Access-VBA/articles/insert-update-and-delete-records-from-a-table-using-access-sql.md at master · OfficeDev/VBA-content · GitHub

UPDATE table name


SET field name = some value

To update all the records in a table, specify the table name, and then use the SET clause to specify the field or fields to be changed.

UPDATE tblCustomers
SET Phone = 'None'

In most cases, you will want to qualify the UPDATE statement with a WHERE clause to limit the number of records changed.

UPDATE tblCustomers
SET Email = 'None'
WHERE [Last Name] = 'Smith'

Deleting Records from a Table


To delete the data that is currently in a table, you use the DELETE statement, which is commonly referred to as a delete query. This is
also known as truncating a table. The DELETE statement can remove one or more records from a table and generally takes this form:

DELETE FROM table list

The DELETE statement does not remove the table structure—only the data that is currently being held by the table structure. To
remove all the records from a table, use the DELETE statement and specify which table or tables from which you want to delete all the
records.

DELETE FROM tblInvoices

https://github.com/OfficeDev/VBA-content/blob/master/VBA/Access-VBA/articles/insert-update-and-delete-records-from-a-table-using-access-sql.md 6/7
01/01/2024 17:08 VBA-content/VBA/Access-VBA/articles/insert-update-and-delete-records-from-a-table-using-access-sql.md at master · OfficeDev/VBA-content · GitHub

In most cases, you will want to qualify the DELETE statement with a WHERE clause to limit the number of records to be removed.

DELETE FROM tblInvoices


WHERE InvoiceID = 3

If you want to remove data only from certain fields in a table, use the UPDATE statement and set those fields equal to NULL, but only
if they are nullable fields.

UPDATE tblCustomers
SET Email = Null

https://github.com/OfficeDev/VBA-content/blob/master/VBA/Access-VBA/articles/insert-update-and-delete-records-from-a-table-using-access-sql.md 7/7

You might also like