Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Inserting data

This example inserts a new record, with CategoryName and Description values, in the categories table of the Northwind database. It requires the files insert_form.html and insert.asp. The first displays a simple HTML form with two text box controls, while the second is an ASP script that actually inserts the data.

Once you've installed the files, open your browser and enter the URL. http://localhost/sample/insert_form.html to view the HTML form. The form doesn't have an input for CategoryID because that is an AutoNumber field that Access fills in automatically. You'll want to avoid that common mistake don't include AutoNumber fields in your form unless you take some measure to prevent the user from trying to change that data. The form also omits the Picture field, as upload images and other large objects are subjects for another article.

In insert_form.html, the statement

<form name="frmInsert" action="insert.asp"></form> Indicates that the script insert.asp will process the data. The first input

<input type="text" name="txtCategory" /> Accepts a new category name to pass as the field txtCategory, while the code

<textarea name="txtDescription" rows="10" cols="40"></textarea>

Produces the multiple-line Description field, 10 rows high by 40 characters wide, for a new description to pass as the field txtDescription

Enter a new category name and description and click the Insert button to call the ASP script insert.asp. If the task is successful, the script will display the confirmation message

You have successfully inserted a new record.

Notice that the browser's location bar now displays the name of the ASP file. If you open the Northwind database in Access, you will find that the categories table now contains a new record with the name and description that you added.

The ASP script assigns the contents of the txtCategory and txtDescription controls in the HTML form to two variables named category and Description, respectively. Because all ASP variables are Variants, you don't need to specify a data type when declaring a variable. In fact, you don't have to declare the variables at all; however, doing so does make your script more readable.

It then inserts those variables into a SQL statement:

Sql = "INSERT INTO Categories (CategoryName, Description)"

Sql = sql & " Values(" & Category &" , " & description & " )"

These lines construct an INSERT INTO statement using the submitted form values. If either was left blank, the script returns a generic error.

The FormsCollection collection

When an HTML form submits data, ASP stores it in the Request.FormCollection collection, which you can manipulate with the syntax

Request.form(dataItem) Where dataItem is the numerical index or name of a data field, such as txtCategory. You can also count the number of fields with the request.form.Count property and iterate through them with a statement such as

For x = 1 to request.form.count

Click Next for Modifying-data


No comments:

Post a Comment

Comments are subject to moderation, before publishing.


Subscribe in a reader
Your email address:

Delivered by FeedBurner


Popular Posts

Search This Blog

Blog Archive

Powered by Blogger.

Follow by Email


Forms How Tos Functions MS-Access Security Reports msaccess forms Animations msaccess animation Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Queries Array External Links msaccess reports msaccess tips Accesstips Class Module Menus and Toolbars MsaccessLinks Process Controls Art Work Downloads Objects Property msaccess How Tos Graph Charts msaccessQuery Collection Object Combo Boxes List Boxes VBA Command Buttons Data Emails and Alerts Event Query Calculation Controls Custom Wizards DOS Commands Data Type Dictionary Object Form WithEvents ms-access functions msaccess functions msaccess graphs msaccess reporttricks Command Button Object Reference msaccessprocess security advanced Access Security Add Custom Functions Field Type Form Instances Item Key Macros Menus Report Top Values Variables msaccess email msaccess menus progressmeter Access2007 Auto-Number Copy Expression Fields Join Methods Microsoft Numbering System RaiseEvent Records Recordset Security Split SubForm Table Time Difference Utility Workgroup Wrapper Classes database msaccess wizards tutorial Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Conditional Formatting Data Filtering Defining Pages Diagram Disk Dynamic Lookup Error Handler Excel Export External Filter Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Monitoring Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page Tables Union Query User Users Water-Mark Word automatically commands function hyperlinks iSeries Date iif ms-access msaccess msaccess alerts pdf files reference restore switch text toolbar updating upload vba code

Featured Post

Access Form Control Arrays and Event-3

This is the continuation of last Week’s Topic on Text Box AfterUpdate and LostFocus Event capturing and performing Validation Checks on val...


Blog Archive

Recent Posts