473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

delete record from ms access database

Phi
Hi,

I hope somebody could help me with this problem.

I would like to make a form to add and delete records from my ms access
database. I've found most of the codes from the internet and adjusted
some part of it. Anyhow, inserting records to the database seems to work
fine, but deleting the records is not working as I want to and I just
can't see what's causing the problem.

Please, feel free to respond. Any comments/suggestion is welcome.

Thanks in advance for you help.

Kind regards,

Phi

code
========

<%@ Import Namespace="System.Data" %>
<%@ Page Language = "vb" debug="true"%>
<%@ Import Namespace = "System.Data.OleDb" %>

<script language="vb" runat="server">
Private Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
viewstate("sortField") = "test_id"
viewstate("sortDirection") = "DESC"
testBindGrid()
End If
End Sub
sub onupdate(obj as object, e as eventargs)
dim StrSQl as string = "Insert into tbltest (test, test2 ) values
('" & tbx_test.text & "', '" & tbx_test2.text & "')"
ExecuteStatement(StrSQL)

testBindGrid()
End Sub

sub Delete_Record_tblTest(obj as object, e as DataGridCommandEventargs)
dim strSQL as string = "delete from tbltest where test_id = " &
e.Item.ItemIndex
lbl_delete_strSQL.text = strsql
end sub

function ExecuteStatement(StrSQL)
Dim strConn As String = "Provider=Microsoft.Jet.Oledb.4.0;" & "Data
Source=" & Server.MapPath("/data/test.mdb") & ";"
Dim MyConn As New OleDBConnection(strConn)
dim objCmd as new OleDbCommand(StrSQL, MyConn)
objCmd.Connection.Open()
objCmd.ExeCuteNonQuery()

''close the connection to get the affected record in the table
objCmd.Connection.Close()
end function

Private Sub testBindGrid()
Dim strConn As string = "Provider=Microsoft.Jet.Oledb.4.0;" & "Data
Source=" & Server.MapPath("/data/test.mdb") & ";"
Dim MySQL As string = "SELECT tbltest.test_id, tbltest.test,
tbltest.test2 FROM tbltest ORDER BY " _
& viewstate("sortField").ToString() & " " & viewstate
("sortDirection").ToString()
Dim MyConn As New OleDBConnection(strConn)
Dim ds As DataSet = New DataSet()
Dim Cmd As New OleDBDataAdapter(MySQL,MyConn)
Cmd.Fill(ds,"test")

testDataGrid.DataSource = ds
testDataGrid.Databind()

End Sub
Private Sub testPageGrid(ByVal source As Object, ByVal e As
DataGridPageChangedEventArgs)
testDataGrid.CurrentPageIndex = e.NewPageIndex
testBindGrid()
End Sub

Private Sub testSortGrid(ByVal source As Object, ByVal e As
DataGridSortCommandEventArgs)
If e.SortExpression.ToString() = viewstate("sortField").ToString() Then
Select Case viewstate("sortDirection").ToString()
Case "ASC"
viewstate("sortDirection") = "DESC"
Case "DESC"
viewstate("sortDirection") = "ASC"
End Select
Else
viewstate("sortField") = e.SortExpression
viewstate("sortDirection") = "DESC"
End If
testBindGrid()
End Sub

</script>
<html>

<body>

<form runat="server" method="post">
Insert Records in to the table Test<br><br>

<table>
<tr>
<td>test:</td>

<td><asp:textbox id="tbx_test"
runat="server"/></td>
</tr>

<tr>
<td>test2:</td>

<td><asp:textbox id="tbx_test2"
runat="server"/><br></td>
</tr>

<tr>
<td>

</td>

<td>

<asp:button id="button1" OnClick=
"onupdate" text="Add record" runat="server"/><br>
<asp:label id="lbl_delete_strSQL"
runat="server"/>

</td>
</tr>
</table>
<br><br><br>

<asp:Datagrid runat="server"
Id="testDataGrid"
Width="130"
GridLines="Both"
AutoGenerateColumns="false"
BorderColor="silver"
CellPadding="4"
CellSpacing="0"
AllowPaging="true"
OnPageIndexChanged="testPageGrid"
AllowSorting="true"
OnSortCommand="testSortGrid"
BorderWidth="1"
PageSize="20"
Primarykey="test_id"
OnDeleteCommand="Delete_Record_tblTest">

<headerstyle
Forecolor="#FFFFFF"
Font-Name="Tahoma"
Font-Size="100%"
Font-Bold="true"
BackColor="#FF0000">
</headerstyle>

<itemstyle
Font-Name="Verdana"
Font-Size="70%"
VerticalAlign="top"
BackColor="#FFFFFF">
</itemstyle>

<alternatingitemstyle
BackColor="#F4F4F4">
</alternatingitemstyle>

<pagerstyle
Position="bottom"
Mode="NumericPages"
HorizontalAlign="Center"
ForeColor="#FFFFFF"
BackColor="#FF0000">
</pagerstyle>

<columns>

<asp:boundColumn
DataField="test_id"
SortExpression="test_id"
HeaderText="ID"
ItemStyle-Wrap="false">
</asp:boundColumn>

<asp:boundColumn
DataField="test"
SortExpression="test"
HeaderText="Test"
ItemStyle-Wrap="false">
</asp:boundColumn>

<asp:boundColumn
DataField="test2"
SortExpression="test2"
HeaderText="Test2"
ItemStyle-Wrap="false">
</asp:boundColumn>

<asp:ButtonColumn
HeaderText=""
Text="Delete"
CommandName="delete"/>
</columns>
</asp:DataGrid>
</form>

</body>
</html>
Nov 20 '05 #1
3 11788
Wrong newsgroup buddy, try adonet or asp

OHM
"Phi" <ph*******@hotmail.com(remove)> wrote in message
news:Xn**********************************@194.109. 133.20...
Hi,

I hope somebody could help me with this problem.

I would like to make a form to add and delete records from my ms access
database. I've found most of the codes from the internet and adjusted
some part of it. Anyhow, inserting records to the database seems to work
fine, but deleting the records is not working as I want to and I just
can't see what's causing the problem.

Please, feel free to respond. Any comments/suggestion is welcome.

Thanks in advance for you help.

Kind regards,

Phi

code
========

<%@ Import Namespace="System.Data" %>
<%@ Page Language = "vb" debug="true"%>
<%@ Import Namespace = "System.Data.OleDb" %>

<script language="vb" runat="server">
Private Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
viewstate("sortField") = "test_id"
viewstate("sortDirection") = "DESC"
testBindGrid()
End If
End Sub
sub onupdate(obj as object, e as eventargs)
dim StrSQl as string = "Insert into tbltest (test, test2 ) values
('" & tbx_test.text & "', '" & tbx_test2.text & "')"
ExecuteStatement(StrSQL)

testBindGrid()
End Sub

sub Delete_Record_tblTest(obj as object, e as DataGridCommandEventargs)
dim strSQL as string = "delete from tbltest where test_id = " &
e.Item.ItemIndex
lbl_delete_strSQL.text = strsql
end sub

function ExecuteStatement(StrSQL)
Dim strConn As String = "Provider=Microsoft.Jet.Oledb.4.0;" & "Data
Source=" & Server.MapPath("/data/test.mdb") & ";"
Dim MyConn As New OleDBConnection(strConn)
dim objCmd as new OleDbCommand(StrSQL, MyConn)
objCmd.Connection.Open()
objCmd.ExeCuteNonQuery()

''close the connection to get the affected record in the table
objCmd.Connection.Close()
end function

Private Sub testBindGrid()
Dim strConn As string = "Provider=Microsoft.Jet.Oledb.4.0;" & "Data
Source=" & Server.MapPath("/data/test.mdb") & ";"
Dim MySQL As string = "SELECT tbltest.test_id, tbltest.test,
tbltest.test2 FROM tbltest ORDER BY " _
& viewstate("sortField").ToString() & " " & viewstate
("sortDirection").ToString()
Dim MyConn As New OleDBConnection(strConn)
Dim ds As DataSet = New DataSet()
Dim Cmd As New OleDBDataAdapter(MySQL,MyConn)
Cmd.Fill(ds,"test")

testDataGrid.DataSource = ds
testDataGrid.Databind()

End Sub
Private Sub testPageGrid(ByVal source As Object, ByVal e As
DataGridPageChangedEventArgs)
testDataGrid.CurrentPageIndex = e.NewPageIndex
testBindGrid()
End Sub

Private Sub testSortGrid(ByVal source As Object, ByVal e As
DataGridSortCommandEventArgs)
If e.SortExpression.ToString() = viewstate("sortField").ToString() Then
Select Case viewstate("sortDirection").ToString()
Case "ASC"
viewstate("sortDirection") = "DESC"
Case "DESC"
viewstate("sortDirection") = "ASC"
End Select
Else
viewstate("sortField") = e.SortExpression
viewstate("sortDirection") = "DESC"
End If
testBindGrid()
End Sub

</script>
<html>

<body>

<form runat="server" method="post">
Insert Records in to the table Test<br><br>

<table>
<tr>
<td>test:</td>

<td><asp:textbox id="tbx_test"
runat="server"/></td>
</tr>

<tr>
<td>test2:</td>

<td><asp:textbox id="tbx_test2"
runat="server"/><br></td>
</tr>

<tr>
<td>

</td>

<td>

<asp:button id="button1" OnClick=
"onupdate" text="Add record" runat="server"/><br>
<asp:label id="lbl_delete_strSQL"
runat="server"/>

</td>
</tr>
</table>
<br><br><br>

<asp:Datagrid runat="server"
Id="testDataGrid"
Width="130"
GridLines="Both"
AutoGenerateColumns="false"
BorderColor="silver"
CellPadding="4"
CellSpacing="0"
AllowPaging="true"
OnPageIndexChanged="testPageGrid"
AllowSorting="true"
OnSortCommand="testSortGrid"
BorderWidth="1"
PageSize="20"
Primarykey="test_id"
OnDeleteCommand="Delete_Record_tblTest">

<headerstyle
Forecolor="#FFFFFF"
Font-Name="Tahoma"
Font-Size="100%"
Font-Bold="true"
BackColor="#FF0000">
</headerstyle>

<itemstyle
Font-Name="Verdana"
Font-Size="70%"
VerticalAlign="top"
BackColor="#FFFFFF">
</itemstyle>

<alternatingitemstyle
BackColor="#F4F4F4">
</alternatingitemstyle>

<pagerstyle
Position="bottom"
Mode="NumericPages"
HorizontalAlign="Center"
ForeColor="#FFFFFF"
BackColor="#FF0000">
</pagerstyle>

<columns>

<asp:boundColumn
DataField="test_id"
SortExpression="test_id"
HeaderText="ID"
ItemStyle-Wrap="false">
</asp:boundColumn>

<asp:boundColumn
DataField="test"
SortExpression="test"
HeaderText="Test"
ItemStyle-Wrap="false">
</asp:boundColumn>

<asp:boundColumn
DataField="test2"
SortExpression="test2"
HeaderText="Test2"
ItemStyle-Wrap="false">
</asp:boundColumn>

<asp:ButtonColumn
HeaderText=""
Text="Delete"
CommandName="delete"/>
</columns>
</asp:DataGrid>
</form>

</body>
</html>

Nov 20 '05 #2
Hello,

"Phi" <ph*******@hotmail.com(remove)> schrieb:
I would like to make a form to add and delete records from my
ms access database.


This is a VB.NET language group. Notice that you will have a better
chance to get an answer if you post to the ADO.NET newsgroup in future:

news://msnews.microsoft.com/microsof...amework.adonet

Web interface:

http://msdn.microsoft.com/newsgroups...amework.adonet

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #3
phy
"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> wrote in
news:ei**************@TK2MSFTNGP12.phx.gbl:
msnews.microsoft.com/microsoft.public.dotnet.framework.adonet


Sorry, I'm trying a new mailreader...
I pushed the button too soon..

Phi
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Steve | last post by:
I have several pairs of synchronized subforms in an application. I have a Delete button for each pair that uses the following code or similar to delete a record in the second subform: ...
4
by: Stephen | last post by:
Hello People, Using MS Access 2003 VBA I get the error 3020 Update or CancelUpdate without AddNew or Edit when I run through the following code. Can anyone help suggest anything to try? Thanks....
4
by: Susan Bricker | last post by:
I have a command button on a form that is supposed to Delete the record being displayed. The record is displayed one to a form. The form is not a Pop-Up nor is it Modal. Tracing the btnDelete...
3
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all...
3
by: bluez | last post by:
I want to design a webpage where user can search the data from the database and list out the related records. Each of the record got a delete button which allow user to delete the record. ...
10
beacon
by: beacon | last post by:
Hi everybody, This is probably going to sound unorthodox, but I have to log records that are deleted...I know, go figure. Anyway, I have a form with a (continuous) subform, and on the subform I...
6
BeemerBiker
by: BeemerBiker | last post by:
I can't get rid of a "deleted" record in a database. Even if I delete it the record shows back up the next time I do an update. The following code generates an error message when using...
31
by: matthewslyman | last post by:
I have an unusual design and some very unusual issues with my code... I have forced Access to cooperate on everything except one issue - record deletion. My form design involves a recursively...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.