Membuat program sederhana CRUD data Barang menggunakan program visual studio dan Automation Word
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\Muhamad Anjar\Documents\dbiventori.mdb")
Sub tampilgrid()
Dim da As New OleDb.OleDbDataAdapter("select * from tb_barang", con)
Dim ds As New DataSet
da.Fill(ds, "tb_barang")
DataGridView1.DataSource = (ds.Tables("tb_barang"))
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
tampilgrid()
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_barang where kd_barang='" & 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_barang VALUES('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & cmbSatuan.Text & "')"
cmdsave.ExecuteNonQuery()
MsgBox("Data Berhasil di Input")
tampilgrid()
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_barang where kd_barang='" & Trim(cari) & "'"
Dim dr As OleDbDataReader
dr = cmdcari.ExecuteReader
If dr.HasRows = True Then
dr.Read()
TextBox1.Text = dr("kd_barang")
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()
tampilgrid()
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_barang").Value
con.Close()
con.Open()
Dim cmd As New OleDbCommand
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = "UPDATE tb_barang set nama_barang='" & TextBox2.Text & "'," &
"harga='" & TextBox3.Text & "'," &
"satuan='" & cmbSatuan.Text & "'" &
"where kd_barang='" & brgId & "'"
cmd.ExecuteNonQuery()
MsgBox("Data Berhasil di Edit", MsgBoxStyle.Information)
tampilgrid()
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_barang").Value
con.Close()
con.Open()
Dim cmdcari As New OleDbCommand
cmdcari.Connection = con
cmdcari.CommandType = CommandType.Text
cmdcari.CommandText = "SELECT * FROM tb_barang where kd_barang='" & Trim(brgId) & "'"
Dim dr As OleDbDataReader
dr = cmdcari.ExecuteReader
If dr.HasRows = True Then
dr.Read()
TextBox1.Text = dr("kd_barang")
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_barang").Value
con.Close()
con.Open()
Dim cmd As New OleDbCommand
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = "DELETE from tb_barang where kd_barang='" & brgId & "'"
cmd.ExecuteNonQuery()
MsgBox("Data Berhasil di Hapus", MsgBoxStyle.Information)
tampilgrid()
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\Muhamad Anjar\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 = "Arial"
MyWordApp.Selection.Font.Size = 20
MyWordApp.Selection.Font.Bold = 1
MyWordApp.Selection.Font.Italic = 1
MyWordDoc.Save()
End Sub
End Class
0 komentar:
Posting Komentar