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


Subscribe in a reader
Your email address:

Delivered by FeedBurner


Popular Posts

Search This Blog

Blog Archive

Powered by Blogger.


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 External Links msaccess reports msaccess tips Accesstips Array Menus and Toolbars MsaccessLinks Process Controls Art Work Downloads msaccess How Tos Graph Charts msaccessQuery Class Module List Boxes Property VBA Combo Boxes Command Buttons Data Emails and Alerts Objects Query Calculation Custom Wizards DOS Commands Data Type ms-access functions msaccess functions msaccess graphs msaccess reporttricks Collection Object msaccessprocess security advanced Access Security Custom Functions Field Type Form Macros Menus Object Reference Report Top Values Variables msaccess email msaccess menus progressmeter Access2007 Auto-Number Command Button Controls Copy Dictionary Object Expression Fields Form Instances Item Join Key Methods Microsoft Numbering System Records Recordset Security Split SubForm Table Time Difference Utility Workgroup Wrapper Classes database msaccess wizards tutorial Access2003 Accounting Year Action Add 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

Sorting Dictionary Object Keys and Items

Sorting/Indexing of Table Records is a frequently used important exercise to organize the data in proper order, for faster retrieval of inf...


Blog Archive

Recent Posts