Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site
   WARNING!
What you learn from these Totally FREE
Einstein Newsletters could cause your friends to mistake you for someone else!
  [] automobiles
  [] business
  [] parenting
  [] computers
  [] contests
  [] education
  [] entertainment
  [] food/wine
  [] free stuff
  [] genealogy
  [] health/fitness
  [] home/garden
  [] humor
  [] marketing
  [] investing
  [] pets
  [] inspiration
  [] self-improve
  [] recreation
  [] travel
  [] womens stuff
  [] writing/reading
Click here and choose as many as you like!
Visit bucarotechelp.com Bucaro TecHelp Newsletter
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web.
~ ~ ~ March 28, 2006 Volume 6 Number 3 ~ ~ ~

Create an Access Database Using Only Notepad
Part 8 Code to Add Records to a Database

In this series of articles, you'll learn how to create a Microsoft Access database using only a basic ASCII text editor, like Windows Notepad. That's right, you don't need Microsoft's Office suite, or Access database application, or Visual Basic programming environment in order to create an Access database. All you need is your handy text editor.

In this example we will be designing an Order Entry System database. In previous parts of this series you learned how to: create an HTML Application (HTA); determine if the required Microsoft Data Access Components (MDAC) were installed on your computer; design database tables following rules of normalization; and about ActiveX Data Objects Extended (ADOX) Field Properties.

- If you missed previous parts of this series of articles, you can still collect the entire series. Past issues of the newsletter are available in the Archive section of bucarotechelp.com

In Part 7 of this series you used Notepad to enter the code to create a database with only one table, the Customers table. In this article you'll use Notepad to enter the code to add records to the Customers table. Navigate to the folder where you saved the database from the last article and create a text file there. In that text file, type in the tags that create an HTML Application, as shown below.

<html>
<body>
<hta:application>
<script language="VBScript">

</script>
</body>
</html>

We'll enter our code between the <script language="VBScript"> and </script> tags, beginning with the declaration of three variables as shown below.

Dim objConn, strConnection, objRS

Below the variable declarations, enter the three lines shown below, which open a connection to database.

Set objConn = CreateObject("ADODB.Connection")
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Order Entry\orders.mdb'"
objConn.Open strConnection

In the second line shown above, replace the path with the path to the folder where you saved your database. Just below the lines shown above, enter the line shown below, which creates an ADO RecordSet object.

Set objRS = CreateObject("ADODB.RecordSet")

As its name implies, the RecordSet object holds a set of records from the table. Just below the lines shown above, enter the line shown below, which fills the RecordSet with all the records from the Customers table (of course their aren’t any records in the table yet).

objRS.Open "SELECT * FROM tblCustomers", objConn, 3, 3 ' adOpenStatic, adLockOptimistic

We'll be using the RecordSet object's Open method quite a bit in this series of examples, so lets examine it a bit right now. You may recognize that the first parameter in quotes is actually an SQL (Structured Query Language) statement. It basically says "select all records in the Customers table. The second parameter is the connection object.

The third parameter is a numeration for the "cursor" type. In the comment I note that the number 3 defines adOpenStatic. This creates a "static" cursor. In other words if someone else modifies the records while you're looking at them, you won't see the change (until you repeat the request for the recordset).

The fourth parameter is a numeration for the record "locking" type. The number 3 defines optimistic locking which locks the records only during the call to the Update method. Another type of record locking, Pessimistic locking (adLockPessimistic) would keep the records locked until we released them, preventing anyone else from modifying the records while you're looking at them (we don't need that).

Just below the objRS.Open line, enter the line shown below, which sets RecordSet object to add a new record to the table.

objRS.AddNew

Just below the objRS.AddNew line, enter the lines shown below, which defines a record for a customer named "Jimmy Abalone".

objRS("FirstName") = "Jimmy"
objRS("LastName") = "Abalone"
objRS("Street") = "Main"
objRS("City") = "Phonix"
objRS("State") = "AZ"
objRS("ZipCode") = "85268"
objRS("Phone") = "1234561234"
objRS("Email") = "me@mydomain.com"
objRS("Notes") = "Hello There!"

Just below those line, enter the line shown below, which actually adds the new record to the Customers table.

objRS.Update

Finally, enter the lines shown below, which close the recordset and the database connection and clean up the memory objects.

objRS.Close
objConn.Close
Set objRS = Nothing
Set objConn = Nothing

Now save the file with a .hta extension, for example addCustomer.hta. Then double click on the file name to execute the script. A blank window will open (obviously we're not getting fancy here) and customer Jimmy Abalone's record will be added to the Customers table.

If you received an error message when you executed the file, then (assuming that you where previously able to successfully create the database) check your code for typos).

If your script ran without error, I know it's a bit of a let down to have successfully created an Access database and added a record to it without being able to read the record back. If you actually have Microsoft Access installed on your computer, you can open the database in Design View, double-click on the Customers table and view Jimmy Abalone's record. Otherwise, in the next part of this series, we'll create a script to display the records in the database.



Newsletter Back Issues
2006
2005

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269