Kamis, 24 Juli 2014

Aplikasi Sederhana Contact Apps dengan Android SQLLITE

Kali ini saya akan membuat aplikasi sederhana berbasis android dengan menggunakan sqllite. Aplikasi ini digunakan untuk menyimpan nama, no telepon dan email di kontak android Anda.

Pertama kita akan membuka aplikasi dengan mengklik ContactApps


Tampilan pertama aplikasi
 
 
Tampilan Inputan Data

Setelah diinput

Hapus kontak

Edit Kontak

View Kontak :v
 
 
 
MainActivity.java
package id.pratama.contactapps;

import id.pratama.contactapps.adapter.ContactAdapter;
import id.pratama.contactapps.database.ContactDB;
import id.pratama.contactapps.entity.Contact;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements
        OnItemClickListener
{
    private ListView        lv;
    private ContactDB        db;
    private ContactAdapter    adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db = ContactDB.getInstance(this);

        lv = (ListView) findViewById(R.id.listview_contact);

        lv.setEmptyView(findViewById(R.id.empty));

        if (db.isContactHasData())
        {
            adapter = new ContactAdapter(this, db.getAllContact());
            lv.setAdapter(adapter);
        }
        lv.setOnItemClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.action_add:
                Toast.makeText(this, "add", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, AddContactActivity.class));
                finish();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3)
    {
        Bundle b = new Bundle();
        b.putInt("id", ((Contact) adapter.getItem(position)).getId());
        b.putString("nama", ((Contact) adapter.getItem(position)).getNama());
        b.putString("phone", ((Contact) adapter.getItem(position)).getNumber());
        b.putString("email", ((Contact) adapter.getItem(position)).getEmail());

        Intent i = new Intent(this, DetailContactActivity.class);
        i.putExtras(b);
        startActivity(i);
        finish();

    }
}




AddContactActivity.java
package id.pratama.contactapps;

import id.pratama.contactapps.database.ContactDB;
import id.pratama.contactapps.entity.Contact;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddContactActivity extends ActionBarActivity implements
        OnClickListener
{
    private EditText    nama, email, phone;
    private Button        cancel, done;
    private ContactDB    db;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_contact);

        db = ContactDB.getInstance(this);

        nama = (EditText) findViewById(R.id.input_name);
        email = (EditText) findViewById(R.id.input_email);
        phone = (EditText) findViewById(R.id.input_phone);
        cancel = (Button) findViewById(R.id.btn_cancel);
        done = (Button) findViewById(R.id.btn_done);

        cancel.setOnClickListener(this);
        done.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        if (v == done)
        {
            String txtemail = "null";
            if (!TextUtils.isEmpty(nama.getText().toString())
                    && !TextUtils.isEmpty(phone.getText().toString()))
            {
                if (!TextUtils.isEmpty(email.getText().toString()))
                    txtemail = email.getText().toString();

                db.addContact(new Contact(nama.getText().toString(), phone
                        .getText().toString(), txtemail));

                startActivity(new Intent(this, MainActivity.class));
                finish();
            } else
            {
                Toast.makeText(this, "Lengkapi Data", Toast.LENGTH_SHORT)
                        .show();
            }
        } else if (v == cancel)
        {

            startActivity(new Intent(this, MainActivity.class));
            finish();
        }
    }

    @Override
    public void onBackPressed()
    {
        // TODO Auto-generated method stub
        super.onBackPressed();
        finish();
        startActivity(new Intent(this, MainActivity.class));
    }
}




EditActivity.java
package id.pratama.contactapps;

import id.pratama.contactapps.database.ContactDB;
import id.pratama.contactapps.entity.Contact;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class EditActivity extends ActionBarActivity implements OnClickListener
{
    private EditText    nama, email, phone;
    private Button        cancel, done;
    private ContactDB    db;
    private int            id;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        getSupportActionBar().setTitle("Edit Contact");

        db = ContactDB.getInstance(this);

        nama = (EditText) findViewById(R.id.edit_name);
        email = (EditText) findViewById(R.id.edit_email);
        phone = (EditText) findViewById(R.id.edit_phone);
        cancel = (Button) findViewById(R.id.btn_cancel);
        done = (Button) findViewById(R.id.btn_done);

        Bundle b = getIntent().getExtras();
        if (b != null)
        {
            id = b.getInt("id");
            nama.setText(b.getString("name"));
            phone.setText(b.getString("phone"));
            email.setText(b.getString("email"));
        }

        cancel.setOnClickListener(this);
        done.setOnClickListener(this);

    }

    @Override
    public void onClick(View v)
    {
        if (v == done)
        {
            db.editContact(new Contact(id, nama.getText().toString(), phone
                    .getText().toString(), email.getText().toString()));
            startActivity(new Intent(this, MainActivity.class));
            finish();

        } else if (v == cancel)
        {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }
    }

    @Override
    public void onBackPressed()
    {
        // TODO Auto-generated method stub
        super.onBackPressed();
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }
}



 EditContactActivity.java
package id.pratama.contactapps;

import id.pratama.contactapps.database.ContactDB;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class DetailContactActivity extends ActionBarActivity implements
        OnClickListener
{

    private TextView    phone, email;
    private int            id;
    private ImageView    call, btnEmail;
    private ContactDB    db;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_contact);

        db = ContactDB.getInstance(this);

        phone = (TextView) findViewById(R.id.txt_phone_number);
        email = (TextView) findViewById(R.id.txt_email);
        call = (ImageView) findViewById(R.id.btn_call);
        btnEmail = (ImageView) findViewById(R.id.btn_email);

        Bundle b = getIntent().getExtras();
        if (b != null)
        {
            id = b.getInt("id");
            phone.setText(b.getString("phone"));
            getSupportActionBar().setTitle(b.getString("nama"));
            if (!b.getString("email").equals("null"))
            {
                email.setText(b.getString("email"));
            } else
            {
                email.setText("");
            }

        }

        call.setOnClickListener(this);
        btnEmail.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.detail_contact, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.action_edit:
                Bundle b = new Bundle();
                b.putInt("id", id);
                b.putString("name", getSupportActionBar().getTitle().toString());
                b.putString("email", email.getText().toString());
                b.putString("phone", phone.getText().toString());

                Intent i = new Intent(this, EditActivity.class);
                i.putExtras(b);
                startActivity(i);
                finish();
                return true;
            case R.id.action_delete:
                showSettingsAlert();
                return true;
            default:
                return super.onOptionsItemSelected(item);

        }

    }

    @Override
    public void onClick(View v)
    {
        if (v == btnEmail)
        {
            if (!email.getText().toString().equals(""))
            {
                Intent emailIntent = new Intent(Intent.ACTION_SENDTO,
                        Uri.fromParts("mailto", email.getText().toString(),
                                null));

                startActivity(Intent
                        .createChooser(emailIntent, "Send email..."));

            }

        } else if (v == call)
        {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:" + phone.getText().toString()));
            startActivity(callIntent);

        }

    }

    private void showSettingsAlert()
    {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Delete");
        alert.setMessage("Contact akan dihapus");

        alert.setPositiveButton("Ya", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                db.deleteContact(id);
                startActivity(new Intent(DetailContactActivity.this,
                        MainActivity.class));
                finish();
            }
        });
        alert.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.cancel();
            }
        });

        alert.show();

    }

    @Override
    public void onBackPressed()
    {
        super.onBackPressed();
        startActivity(new Intent(DetailContactActivity.this, MainActivity.class));
        finish();
    }

}




ContactDB.java
package id.pratama.contactapps.database;

import java.util.ArrayList;
import java.util.List;

import id.pratama.contactapps.entity.Contact;
import id.pratama.contactapps.util.Utils;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class ContactDB extends SQLiteOpenHelper
{
    private static final String        DB_NAME        = "db_contact";
    private static final int        DB_VERSION    = 1;

    private static ContactDB        dbInstance;
    private static SQLiteDatabase    db;

    private static final String        TB_CONTACT    = "tb_contact";

    interface COLUMN
    {
        String    id        = "_id";
        String    nama    = "nama";
        String    nomor    = "nomor";
        String    email    = "email";
    }

    private static final String    SQL_CREATE_CONTACT    = "CREATE TABLE "
                                                            + TB_CONTACT
                                                            + "( "
                                                            + COLUMN.id
                                                            + " integer primary key autoincrement not null, "
                                                            + COLUMN.nama
                                                            + " text, "
                                                            + COLUMN.email
                                                            + " text, "
                                                            + COLUMN.nomor
                                                            + " text)";

    private ContactDB(Context context)
    {
        super(context, DB_NAME, null, DB_VERSION);
    }

    public static ContactDB getInstance(Context context)
    {
        if (dbInstance == null)
        {
            dbInstance = new ContactDB(context);
            db = dbInstance.getWritableDatabase();
        }
        return dbInstance;
    }

    @Override
    public synchronized void close()
    {
        super.close();
        if (dbInstance != null)
            dbInstance.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        // TODO Auto-generated method stub
        Utils.TRACE("ContactDB", "" + SQL_CREATE_CONTACT);
        db.execSQL(SQL_CREATE_CONTACT);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL("DROP TABLE IF EXIST " + TB_CONTACT);
        onCreate(db);
    }

    public List<Contact> getAllContact()
    {
        List<Contact> listContacts = new ArrayList<Contact>();

        Cursor cursor = db.query(TB_CONTACT, new String[] { COLUMN.id,
                COLUMN.nama, COLUMN.email, COLUMN.nomor }, null, null, null,
                null, COLUMN.nama);
        cursor.moveToFirst();
        do
        {
            listContacts
                    .add(new Contact(cursor.getInt(cursor
                            .getColumnIndexOrThrow(COLUMN.id)), cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(COLUMN.nama)),
                            cursor.getString(cursor
                                    .getColumnIndexOrThrow(COLUMN.nomor)),
                            cursor.getString(cursor
                                    .getColumnIndexOrThrow(COLUMN.email))));
        } while (cursor.moveToNext());
        return listContacts;
    }

    public boolean isContactHasData()
    {
        Cursor cursor = db.query(TB_CONTACT, new String[] { COLUMN.id }, null,
                null, null, null, null);
        return (cursor.getCount() > 0) ? true : false;
    }

    public boolean addContact(Contact contact)
    {
        ContentValues values = new ContentValues();

        values.put(COLUMN.nama, contact.getNama());
        values.put(COLUMN.email, contact.getEmail());
        values.put(COLUMN.nomor, contact.getNumber());

        return ((db.insert(TB_CONTACT, null, values)) != -1) ? true : false;
    }

    public boolean editContact(Contact contact)
    {
        ContentValues values = new ContentValues();

        values.put(COLUMN.nama, contact.getNama());
        values.put(COLUMN.email, contact.getEmail());
        values.put(COLUMN.nomor, contact.getNumber());

        return ((db.update(TB_CONTACT, values, COLUMN.id + "=?",
                new String[] { String.valueOf(contact.getId()) })) == 1) ? true
                : false;
    }

    public void deleteContact(int id)
    {
        db.delete(TB_CONTACT, COLUMN.id + "=?",
                new String[] { String.valueOf(id) });
    }
}

Jumat, 23 Mei 2014

APLIKASI SEDERHANA ANDROD HITUNG LUAS LINGKARAN

aplikasi ini untuk menghitung luas lingkaran















#SKRIP DI MAIN CLASS
package com.example.hitunglingkaran;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener
{
 private EditText etJari, etHasil;
 private Button  btnHitung;
 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  etJari = (EditText) findViewById(R.id.editTextJari);
  etHasil = (EditText) findViewById(R.id.editTextHasil);
  btnHitung = (Button) findViewById(R.id.btnHitung);
  btnHitung.setOnClickListener(this);
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu)
 {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }
 @Override
 public void onClick(View v)
 {
  switch (v.getId())
  {
   case R.id.btnHitung:
    etHasil.setText(String.valueOf(hitLuas(Float.parseFloat(etJari.getText().toString()))));
    break;
  }
 }
 private float hitLuas(float jari)
 {
  float luas;
  luas = (float) (3.14 * jari * jari);
  return luas;
 }
}
#SKRIP DI LAYOUT
<relativelayout android:layout_height="match_parent" android:layout_width="match_parent" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
    <textview android:id="@+id/textView1" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_height="wrap_content" android:layout_margintop="65dp" android:layout_width="wrap_content" android:text="Jari">
    <edittext android:ems="10" android:id="@+id/editTextJari" android:layout_below="@+id/textView1" android:layout_centerhorizontal="true" android:layout_height="wrap_content" android:layout_width="wrap_content">
     <requestfocus>
    </requestfocus></edittext>
    <edittext android:ems="10" android:id="@+id/editTextHasil" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:layout_height="wrap_content" android:layout_width="wrap_content">
    <button android:id="@+id/btnHitung" android:layout_below="@+id/editTextHasil" android:layout_centerhorizontal="true" android:layout_height="wrap_content" android:layout_margintop="34dp" android:layout_width="wrap_content" android:text="Hitung">
    <textview android:id="@+id/textView2" android:layout_above="@+id/editTextHasil" android:layout_alignleft="@+id/textView1" android:layout_height="wrap_content" android:layout_marginbottom="16dp" android:layout_width="wrap_content" android:text="Hasil">
</textview></button></edittext></textview></relativelayout>

APLIKASI KALKULATOR SEDERHANA ANDROID

Assalamualaikum, kali ini saya akan mencoba berbagi kepada teman-teman mengenai aplikasi sederhana "Kalkulator".
Aplikasi sederhana ini berfungsi untuk mengetahui nilai yang sudah di tambah, kurang, kali, dan bagi yang sudah ditentukan oleh pengguna. Pengguna tinggal memasukan angka nilai.
Berikut adalah penjelasan mengenai cara membuat aplikasi.
Buat tampilan seperti gambar dibawah ini:


















Dalam pembuatan aplikasi sederhana ini digunakan beberapa komponen yang terdiri dari:
  • Text View
  • Edit Text
  • Button
  • List View
Berikut adalah source code nya.
package com.example.uts_003111121011;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity implements OnClickListener {
    protected float angka1, angka2, angkahasil;
    EditText tek_1, tek_2, tek_hasil;
    private ArrayList<String> isi_histori = new ArrayList<String>();
    ListView histori;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tek_1 = (EditText) findViewById(R.id.angka_1);
        tek_2 = (EditText) findViewById(R.id.angka_2);
        tek_hasil = (EditText) findViewById(R.id.text_hasil);
        histori = (ListView) findViewById(R.id.list_histori);
        Button tambah = (Button) findViewById(R.id.tombol_plus);
        Button kurang = (Button) findViewById(R.id.tombol_min);
        Button kali = (Button) findViewById(R.id.tombol_x);
        Button bagi = (Button) findViewById(R.id.tombol_bagi);
        tambah.setOnClickListener(this);
        kurang.setOnClickListener(this);
        kali.setOnClickListener(this);
        bagi.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        angka1 = Float.parseFloat( tek_1.getText().toString());
        angka2 = Float.parseFloat( tek_2.getText().toString());
        switch (v.getId()) {
        case R.id.tombol_plus:
            angkahasil = angka1 + angka2;
            tek_hasil.setText(Float.toString(angkahasil));
            this.tampilkan_histori(angka1 + " + " + angka2 + " = " + angkahasil);
            break;
        case R.id.tombol_min:
            angkahasil = angka1 - angka2;
            tek_hasil.setText(Float.toString(angkahasil));
            this.tampilkan_histori(angka1 + " - " + angka2 + " = " + angkahasil);
            break;
        case R.id.tombol_x:
            angkahasil = angka1 * angka2;
            tek_hasil.setText(Float.toString(angkahasil));
            this.tampilkan_histori(angka1 + " x + " + angka2 + " = " + angkahasil);
            break;
        case R.id.tombol_bagi:
            angkahasil = angka1 / angka2;
            tek_hasil.setText(Float.toString(angkahasil));
            this.tampilkan_histori(angka1 + " : " + angka2 + " = " + angkahasil);
            break;
        default:
            break;
        }
    }
    private void tampilkan_histori(String tambahan){
        isi_histori.add(tambahan);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

android.R.layout.simple_list_item_1, isi_histori);
        histori.setAdapter(adapter);

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Senin, 30 Desember 2013

TUGAS APLIKASI CRUD DENGAN VB.NET AUTOMATION WORD






Membuat program sederhana CRUD data Barang VB.NET








Imports System.Data.OleDb
Imports word = Microsoft.Office.Interop.Word
Public Class Form3
    Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\KatonFB\Documents\dbiventory.mdb")
    Sub showgrid()
        Dim da As New OleDb.OleDbDataAdapter("select * from tb_brng", con)

        Dim ds As New DataSet
        da.Fill(ds, "tb_brng")
        DataGridView1.DataSource = (ds.Tables("tb_brng"))

    End Sub
    Sub clear()
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        cmbSatuan.Text = ""
        TextBox1.Focus()

    End Sub
    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        showgrid()
    End Sub

 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        con.Close()
        con.Open()
        Dim cmdsave As New OleDbCommand

        cmdsave.Connection = con
        cmdsave.CommandType = CommandType.Text
        cmdsave.CommandText = "SELECT * FROM tb_brng where kd_brng='" & Trim(TextBox1.Text) & "'"


        Dim dr As OleDbDataReader
        dr = cmdsave.ExecuteReader

        If dr.HasRows = True Then
            dr.Read()
            MsgBox("Data Sudah Ada")
          
        Else
            con.Close()
            con.Open()
            cmdsave.Connection = con
            cmdsave.CommandType = CommandType.Text
            cmdsave.CommandText = "INSERT INTO tb_brng VALUES('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & cmbSatuan.Text & "')"
            cmdsave.ExecuteNonQuery()
            MsgBox("Data Berhasil di Input")
            showgrid()
            clear()
        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim cari = InputBox("Masukan Kode Barang yg di Cari")

        con.Close()
        con.Open()
        Dim cmdcari As New OleDbCommand

        cmdcari.Connection = con
        cmdcari.CommandType = CommandType.Text
        cmdcari.CommandText = "SELECT * FROM tb_brng where kd_brng='" & Trim(cari) & "'"

        Dim dr As OleDbDataReader
        dr = cmdcari.ExecuteReader

        If dr.HasRows = True Then
            dr.Read()
            TextBox1.Text = dr("kd_brng")
            TextBox2.Text = dr("nama_barang")
            TextBox3.Text = dr("harga")
            cmbSatuan.Text = dr("satuan")
        Else
            MsgBox("Data tidak di temukan", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Pencarian")
        End If

    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click

        If Me.btnEdit.Text = "Update" Then
            UpdateData()
            showgrid()
        Else
            Selectdata()
            Me.btnEdit.Text = "Update"
        End If
    End Sub
    Sub UpdateData()
        If Me.DataGridView1.RowCount > 0 Then
            If Me.DataGridView1.SelectedRows.Count > 0 Then
                Dim brgId As String = Me.DataGridView1.SelectedRows(0).Cells("kd_brng").Value

                con.Close()
                con.Open()

                Dim cmd As New OleDbCommand
                cmd.Connection = con
                cmd.CommandType = CommandType.Text
                cmd.CommandText = "UPDATE tb_brng set nama_barang='" & TextBox2.Text & "'," &
                                   "harga='" & TextBox3.Text & "'," &
                                   "satuan='" & cmbSatuan.Text & "'" &
                                   "where kd_brng='" & brgId & "'"
                cmd.ExecuteNonQuery()

                MsgBox("Data Sukses di Edit", MsgBoxStyle.Information)
                showgrid()
                clear()
                Me.btnEdit.Text = "Edit"
            End If
        End If
    End Sub
    Sub Selectdata()
        If Me.DataGridView1.RowCount > 0 Then
            If Me.DataGridView1.SelectedRows.Count > 0 Then
                Dim brgId As String = Me.DataGridView1.SelectedRows(0).Cells("kd_brng").Value

                con.Close()
                con.Open()



                Dim cmdcari As New OleDbCommand
                cmdcari.Connection = con
                cmdcari.CommandType = CommandType.Text
                cmdcari.CommandText = "SELECT * FROM tb_brng where kd_brng='" & Trim(brgId) & "'"

                Dim dr As OleDbDataReader
                dr = cmdcari.ExecuteReader

                If dr.HasRows = True Then
                    dr.Read()
                    TextBox1.Text = dr("kd_brng")
                    TextBox2.Text = dr("nama_barang")
                    TextBox3.Text = dr("harga")
                    cmbSatuan.Text = dr("satuan")
                End If
                con.Close()

            End If
        End If
    End Sub
    Private Sub btnHapus_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHapus.Click
        If Me.DataGridView1.RowCount > 0 Then
            If Me.DataGridView1.SelectedRows.Count > 0 Then
                Dim brgId As String = Me.DataGridView1.SelectedRows(0).Cells("kd_brng").Value

                con.Close()
                con.Open()

                Dim cmd As New OleDbCommand
                cmd.Connection = con
                cmd.CommandType = CommandType.Text
                cmd.CommandText = "DELETE from tb_brng where kd_brng='" & brgId & "'"
                cmd.ExecuteNonQuery()

                MsgBox("Data Berhasil di Hapus", MsgBoxStyle.Information)
                showgrid()
                clear()



            End If
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        clear()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim MyWordApp As New word.Application
        Dim MyWordDoc As New word.Document

        MyWordDoc = MyWordApp.Documents.Open("C:\Users\KatonFB\Documents\AutoSolution.docx")
        MyWordDoc.Bookmarks("nama").Select()
        MyWordApp.Selection.TypeText(TextBox1.Text)

        MyWordDoc.Bookmarks("materi").Select()
        MyWordApp.Selection.TypeText(TextBox2.Text)

        MyWordDoc.Bookmarks("sesi").Select()
        MyWordApp.Selection.TypeText(TextBox3.Text)


        'MyWordApp.Selection.Paragraphs.Alignment = word.WdParagraphAlignment.wdAlignParagraphCenter

        MyWordApp.Selection.Font.Name = "Calibri"
        MyWordApp.Selection.Font.Size = 21
        MyWordApp.Selection.Font.Bold = 1
        MyWordApp.Selection.Font.Italic = 1

        MyWordDoc.Save()

    End Sub
End Class