Access ASPUsing ASP with Microsoft Access databases. For Access questions not specific to ASP, please use the Access forum. For more ASP forums, please see the ASP forum category.

Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access ASP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .

I'm just wondering what techniques professional developers use when inserting a record?

The scenario is that i want users to insert a record but after filling in the webform for the asp.net page to check if a record exists (i.e cytcle through the primary key for an existing entry for obvious purpose), and then to say... insert if the record does not exist, if it DOES exist change this field in the exisiting record.

any techniques/ideas/articles/structured english code would help, thanks
deian

Are you only allowing a user to input info to the table one time or are you permitting a user to update data?

In either case you will need something like...I assume you already know how to create a recordset.

Dim priKey, objRS, Keytest

''''First, write a loop to test for the primary key match
Do while not objRS.EOF
If priKey=obrRS("priKey") then
Keytest="true"
'''Write update command here
End If
objRS.MoveNext
Loop
'''Then write command to insert a record (Assuming keytest is not set to true
If Keytest<>"true" then
....Command to insert record
End if
''''Be sure second command is out of the loop...the loop only checks to see if the primary key already exists...if so it will do an update. If not, the value of keytest will never be set to true and then the insert command will be called.

Let me know if this helps
John

If a primary key is matched you can use an Update command
else
If primary key is not
end if

Imar,
I apologise for being confusing. I am new to many of those concepts and hope you will forgive my ignorance.
I have made a web form with Dreamweaver. I would like to have only one button for update or insert. so I am trying to find a "trick" to tell the server to check whether the record is already in the database or whether it is a new record.
something like if the record exists then update (using mm_update) or if the record is not in the database then insert.

What information are you passing to your page to edit the record in the first place? A QueryString with the record's unique ID?

If so, you can use that again to determine whether you're editing a record or not:

If Request.QueryString("ID") <> "" Then
' Update
Else
'New
End If

Because you are resubmitting the page when you save the record, you probably loose the querystring. You can fix that by setting the action attribute to the page's name with the Query String (e.g. action="InsertOrUpdate.asp?<%=Request.QueryString% > ) or you save the ID of the record in a hidden form field and use that.

If this doesn't help, I suggest you explain a bit more what you want and the technology you are using and post some relevant code. I saw your other post and you are using the term DataSet. Are you working on an ASP or ASP.NET application?

Imar,
Thank you for your reply. I understand that I am not very clear in my post and will try to explain myself better.

I have an access database with a lot of tables and am using Dreamweaver to create a web interface.
The primary use of the web interface is for data entry at this stage.

To start, my default page is a search page. The user enters the 'ID' to query if the 'ID' exists in one table.
On submit to the search, it opens a new page where a summary of the record is shown (or if there's no record a line says "would you like to create a new record.."). There is a button on this summary page if the user wants to edit the record. This button redirects to a new page bound to one table in the database. The 'ID' all along is passed from page to page via a hidden field. .
On that new edit page, the user can update the record. There is also a ddl wich redirects to different pages depending on the choice made.
On submit, it works, the table bound to the database is updated. and the user is redirected to the right page (depending on that choice made on the ddl) and another page bound to another table in the database is open.

That's where i am having trouble. On this new page, there's a new dataset, the record may or may not exists. The user needs to fill the form and submit and again he will be redirect to a new page with another dataset bound.

Anyway, passing the 'ID' from one page to another works fine. My problem is how to tell the server to either update or insert.
So far I can only do one at a time:
If i choose mm_insert, the table get a new record but I am getting duplicates (and yes, there's no primary key...)
If i choose mm_update. it doesn't work 'cos sometimes there's no records in that table.
With Dreamweaver, i can use mm_update or mm_insert
What I am trying to do is to write some type of statement like what you've written.
If Request.QueryString("ID") <> "" Then
' Update
Else
'New
End If
but i do not know how to properly write it and where.

This page only has MM_insert. I can also create a MM_update but as I said, I do not know how to write the statement if... then if... then end if...

Thanks so much for taking the time to read this.

( I have another issue with the ddl... I don't seem to be able to capture the right input. So far, the database is updated with the link not with the value of the field... for example, if the user chooses DOS, the database is updated with /DosorOS2.aspx)

Right, I finally understand the situation. The confusion was caused by the fact that you posted in the wrong forum. This is a "classic" ASP forum, while your question pertains to ASP.NET. In the future, you're probably better off posting in one of the .NET forums.

Anyway, I think you're much better of separating the Insert from the Update page. The amount of code that Dreamweaver generates with its Insert and Update behaviors make your pages pretty difficult to understand and manage, especially when you're mixing these behaviors. When I use Dreamweaver and its Server Behaviors, I usually have three pages:

1. List.aspx / Default.aspx
This page displays a list of editable records. An Update button takes you to UpdateItem.aspx. As a QueryString, I pass the ID of the record. A "Create New" button takes you to CreateItem.aspx

2. CreateItem.aspx
This pages contains a single Insert Record behavior

3. UpdateItem.aspx
This pages contains an Update Record behavior and a DataSet.

With this setup, I find things really easy to manage. The only downside is that you have to design and layout your form twice.

An alternative is to create a complete custom InsertUpdate page. Don't use the MM behaviors, but code your own logic that determine when to insert and when to update the record. Much more work, but you have full control.

That said, it think it *may* be possible to have one single CreateUpdate page with DW behaviors. If you look at each MM:Insert and MM:Update, you'll see they expose an Expression property. If you look into th source of the Dreamweaver controls (located here: C:\Program Files\Macromedia\Dreamweaver MX 2004\Configuration\ServerBehaviors\Shared\ASP.Net\ Scripts\Source\DreamweaverCtrls.cs) you'll see that Expression determines whether the control executes its internal methods. The Expression looks like this:

<%# Request.Form("MM_update") = "form1" %>

which means the Update will only be fired when the hidden MM_update contains form1. You can use this expression to determine whether to fire the Insert or Update behavior. If you merge the two forms that DW has created, you'll end up with one hidden field for the form name and one hidden field called theID that holds the ID of your record. When the ID is "", you're Inserting a record, otherwise you're updating.

So, for the MM:Insert you probably need this expression:

Expression='<%# Request.Form("theID") = "" %>'

and for the MM:Update you need this one:

Expression='<%# Request.Form("theID") <> "" %>'

You should also extend the expressions with something like this:

And Request.Form("MM_insert") = "form1"

to make sure the code only runs when the page has been posted back.

As you can see, this solution requires quite some work. If I were you, I'd separate the insert and update pages. Is there any reason you want to combine these two??

The tables I have to bind to the forms are not very easy to work with. In some cases, there's data, in some there isn't.
So I still want to try to get the update or insert on the same button.

I don't quite understand the merging of the two insert and update.
I thought there was a way of writing some code for the server to decide if the recordset is empty, insert, if it has the record, update.

How do you arrive at your update page? How is it possible that you think you are going to update something, just to find out the record doesn't exist and you want to insert a new one? Somehow, that doesn't make sense. In most applications I have built, I know there is something to update, so I redirect to the Update page. If I know no record exists, I redirect to the Create page.

In what circumstances do you not know in advance wether the record exists?