Inserting data from a form to an MS access database

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Inserting data from a form to an MS access database

    I'm new to coldfusion, and have run into a problem when trying to insert data
    from a basic HTML form into an MS Access database.

    The problem is the primary key. When I remove the primary key from the
    database, it works fine, but I need it to have one.

    I'm not sure how to add the primary key as an autonumber.

    I'm using Dreamweaver MX 2004 with a test setup of CF MX7 and IIS 5.1 on
    Windows XP Pro

    Code is below. The first page is the Form, and the second is the page that
    processes the form and adds it to the databse






    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <form action="signup2.cfm" method="post" name="Signup" id="Signup">
    <p>
    <input name="username" type="text" id="username">
    Name</p>
    <p>
    <input name="Age" type="text" id="Age">
    Age
    </p>
    <p>
    <input name="email" type="text" id="email">
    E-mail</p>
    <p>
    <input name="Location" type="text" id="Location">Location</p>
    <p>
    <input name="ID" type="hidden" id="ID">
    </p>
    <p>
    <input name="submit" type="submit">
    </p>
    <p>&nbsp;</p>
    </form>
    </body>
    </html>

    <cfquery name="adduser" datasource="CFtest">
    INSERT INTO signup
    VALUES ('#Form.username#', '#Form.Age#',
    '#Form.Location#', '#Form.email#', #Form.ID#)
    </cfquery>
    <html>
    <head>
    <title>You have added <cfoutput>#Form.username# to the
    database!</cfoutput></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    You have successfully added <cfoutput>#Form.username#</cfoutput> to the
    database.
    </body>
    </html>

    ms526 Guest

  2. Similar Questions and Discussions

    1. Copying data from one Access database to another
      I am working on a project where I need to get data from an Access database uploaded daily by my client and copy fields from various tables into...
    2. Trouble inserting data in database
      I use DW 2004, XP with sp2, and Access 2003. Im planning to make a site that is possible to log in and update news from the web. So I have made a...
    3. Inserting form info into access problem
      I am developing three different surveys which are the same topic - different view points. I have 2 forms working and going into the tables...
    4. Inserting data from text file into database
      Is it possible to write a script in an ASP page that reads a comma delimited text file and inserts the data row by row into an Access db? Each comma...
    5. Inserting an Excel chart in an Access form.
      The process described here must be completely automated. I'm building an Access form to show pricing evolution (price variance based on quantity...
  3. #2

    Default Re: Inserting data from a form to an MS access database

    Do you need to know what the primary key value is before you do the insert? Do
    you need to know what it is after? You should let Access create the value as an
    AUTONUMBER field, which means that you would insert all of the fields
    <i>except</i> the primary key (ID) field. If you needed the ID value, you would
    have to immediately query to get it (select max(id)...), and probably use
    cftransaction to ensure that your insert and select were in the same database
    transaction, otherwise on a busy site, you might get the wrong ID value. (SQL
    Server and Oracle have better methods for obtaining the last ID values, so when
    using Access it is kind of a kludge.)

    Phil

    paross1 Guest

  4. #3

    Default Re: Inserting data from a form to an MS access database

    Following up on Phil's comment's lets assume you don't need to know the primary
    key value before you do your insert. Change this:
    INSERT INTO signup
    VALUES ('#Form.username#', '#Form.Age#',
    '#Form.Location#', '#Form.email#', #Form.ID#)

    to this, (but with your actual field names)
    INSERT INTO signup
    (username, age, location, email)
    VALUES ('#Form.username#', '#Form.Age#',
    '#Form.Location#', '#Form.email#')



    Dan Bracuk Guest

  5. #4

    Default Re: Inserting data from a form to an MS access database

    ok thanks for your help guys. Dan's suggestion worked perfectly.
    ms526 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139