Programming for Everybody: March 2025

Access database VBA programmer: Create insert update delete and search in another database with code



 Private Sub Command12_Click()

If (Not IsNull(Me.idtxt.Value)) Then

Dim dbs As DAO.Database

Set dbs = OpenDatabase("H:\rr.accdb")

Dim strsql As String

strsql = "Select firstname,lastname,marks,image1 From Table6 " _

& " Where id= " & Me.idtxt.Value & ""

Dim rst As DAO.Recordset

Set rst = dbs.OpenRecordset(strsql)

If rst.EOF Then

Me.Image1.Picture = ""

Me.ftxt.Value = ""

Me.ltxt.Value = ""

Me.mtxt.Value = ""

Else

Me.Image1.Picture = rst.Fields("image1")

Me.ftxt.Value = rst.Fields("firstname")

Me.ltxt.Value = rst.Fields("lastname")

Me.mtxt.Value = rst.Fields("marks")

End If

rst.Close

Else

MsgBox "Please Enter ID"

End If

End Sub


Private Sub Command13_Click()

Dim dbs As DAO.Database

Set dbs = OpenDatabase("H:\rr.accdb")

Dim strsql As String

strsql = "Update Table6 Set firstname='" & Me.ftxt.Value & "', " _

& "lastname = '" & Me.ltxt.Value & "',marks=" & Me.mtxt.Value & "," _

& "image1='" & Me.Image1.Picture & "' Where id= " & Me.idtxt.Value & ""

dbs.Execute (strsql)

MsgBox "Data Updated Successfully"

End Sub


Private Sub Command14_Click()

Dim dbs As DAO.Database

Set dbs = OpenDatabase("H:\rr.accdb")

Dim strsql As String

strsql = "Delete From Table6 Where id= " & Me.idtxt.Value & ""

dbs.Execute (strsql)

Me.Image1.Picture = ""

Me.ftxt.Value = ""

Me.ltxt.Value = ""

Me.mtxt.Value = ""

Me.idtxt.Value = ""

MsgBox "Data Deleted Successfully"

End Sub


Private Sub Command7_Click()

Dim img_of As Office.FileDialog

Dim img_var As Variant

Set img_of = Application.FileDialog(msoFileDialogFilePicker)

img_of.Title = "Please Select image"

img_of.Filters.Clear

img_of.Filters.Add "Images", "*.png; *.jpg"

If img_of.Show = True Then

For Each img_var In img_of.SelectedItems

Me.Image1.Picture = img_var

Next

Else

Me.Image1.Picture = ""

MsgBox "Please Select Image"

End If

End Sub


Private Sub Command8_Click()

Me.Image1.Picture = ""

Me.ftxt.Value = ""

Me.ltxt.Value = ""

Me.mtxt.Value = ""

Me.idtxt.Value = ""

End Sub


Private Sub Command9_Click()

Dim dbs As DAO.Database

Set dbs = OpenDatabase("H:\rr.accdb")

Dim strsql As String

strsql = "Insert Into Table6(firstname,lastname,marks,image1) " _

& " Values('" & Me.ftxt.Value & "','" & Me.ltxt.Value & "'," _

& "" & Me.mtxt.Value & ",'" & Me.Image1.Picture & "')"

dbs.Execute (strsql)

MsgBox "Data Inserted Successfully"

End Sub



ASP.NET C# insert update delete using SQL database and Load data in Gridview (WITH CODE)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Configuration;

public partial class _Default : System.Web.UI.Page

{

    public SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["webaspConnectionString"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)

    {

        GridView1.DataBind();

    }


    protected void Button1_Click(object sender, EventArgs e)

    {

        SqlCommand cmd = new SqlCommand("Insert Into Table1(firstname,lastname,marks)Values(@firstname,@lastname,@marks)", conn);

        cmd.Parameters.AddWithValue("firstname", ftxt.Text.Trim());

        cmd.Parameters.AddWithValue("lastname", ltxt.Text.Trim());

        cmd.Parameters.AddWithValue("marks", mtxt.Text.Trim());

        conn.Open();

        cmd.ExecuteNonQuery();

        conn.Close();

        result_lbl.Visible = true;

        result_lbl.ForeColor = System.Drawing.Color.Green;

        result_lbl.Text = "Data Inserted Successfully";

        GridView1.DataBind();

    }


    protected void Button2_Click(object sender, EventArgs e)

    {

        ftxt.Text = "";

        ltxt.Text = "";

        mtxt.Text = "";

        id_lbl.Text = "";

        result_lbl.Text = "";

    }


    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

    {

        id_lbl.Text = GridView1.SelectedRow.Cells[0].Text.ToString();

        ftxt.Text = GridView1.SelectedRow.Cells[1].Text.ToString();

        ltxt.Text = GridView1.SelectedRow.Cells[2].Text.ToString();

        mtxt.Text = GridView1.SelectedRow.Cells[3].Text.ToString();

    }


    protected void Button3_Click(object sender, EventArgs e)

    {

        SqlCommand cmd = new SqlCommand("Update Table1 Set firstname=@firstname,lastname=@lastname,marks=@marks Where id=@id", conn);

        cmd.Parameters.AddWithValue("firstname", ftxt.Text.Trim());

        cmd.Parameters.AddWithValue("lastname", ltxt.Text.Trim());

        cmd.Parameters.AddWithValue("marks", mtxt.Text.Trim());

        cmd.Parameters.AddWithValue("id", id_lbl.Text.Trim());

        conn.Open();

        cmd.ExecuteNonQuery();

        conn.Close();

        result_lbl.Visible = true;

        result_lbl.ForeColor = System.Drawing.Color.Green;

        result_lbl.Text = "Data Updated Successfully";

        GridView1.DataBind();

    }


    protected void Button4_Click(object sender, EventArgs e)

    {

        SqlCommand cmd = new SqlCommand("Delete From Table1 Where id=@id", conn);

                cmd.Parameters.AddWithValue("id", id_lbl.Text.Trim());

        conn.Open();

        cmd.ExecuteNonQuery();

        conn.Close();

        result_lbl.Visible = true;

        result_lbl.ForeColor = System.Drawing.Color.Red;

        result_lbl.Text = "Data Deleted Successfully";

        GridView1.DataBind();

        ftxt.Text = "";

        ltxt.Text = "";

        mtxt.Text = "";

        id_lbl.Text = "";

        

    }