Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Inserting data

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 measures 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

  1. Installing the Personal Web Server (PWS)
  2. or Internet Information Services (IIS)
  3. Create the Example ASP Files
  4. Create a DSN Connection
  5. Connect to the Database
  6. Modify Access Data Online
  7. Inserting Data
  8. Modifying Data
  9. Deleting Data
  10. Data Insert Form


No comments:

Post a Comment

Comments subject to moderation before publishing.




Post Feed


Popular Posts

Blog Archive

Powered by Blogger.


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