17.07
0
Desain Form Aplikasi :
Source Code Aplikasi :
 
Imports System.Data.OleDb
Imports word = Microsoft.Office.Interop.Word
Public Class Form1
    Dim koneksi As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Administrator\Documents\Pertemuan13-14.accdb")
    Public reader As OleDbDataAdapter = Nothing
    Sub kosong()
        txtNoFaktur.Text = ""
        txtKodeBarang.Text = ""
        txtNamaBarang.Text = ""
        txtHarga.Text = ""
        txtQty.Text = ""
        txtTotal.Text = ""
    End Sub
    Sub CariData(ByVal table As System.String, ByVal filter As System.String, ByVal isi As System.String)
        koneksi.Close()
        koneksi.Open()
        Dim cmdcari As New OleDbCommand

        cmdcari.Connection = koneksi
        cmdcari.CommandType = CommandType.Text
        cmdcari.CommandText = "SELECT * FROM " & table & " where " & filter & "='" & Trim(isi) & "'"

        Dim dr As OleDbDataReader
        dr = cmdcari.ExecuteReader

        If dr.HasRows = True Then
            dr.Read()
            txtNamaBarang.Text = dr("nama_barang")
            txtHarga.Text = dr("harga")
        Else
            MsgBox("Data tidak di temukan", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Pencarian")
        End If
    End Sub
   
    Sub KodeOtomatis()
        koneksi.Close()
        koneksi.Open()
        Dim cmd As New OleDbCommand
        cmd.Connection = koneksi
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT * FROM penjualan ORDER BY no_faktur DESC"
        Dim rd As OleDbDataReader
        rd = cmd.ExecuteReader
        rd.Read()
        If Not rd.HasRows Then
            txtNoFaktur.Text = "FK001"
        Else
            txtNoFaktur.Text = Val(Microsoft.VisualBasic.Mid(rd.Item("no_faktur").ToString, 5, 3)) + 1
            If Len(txtNoFaktur.Text) = 1 Then
                txtNoFaktur.Text = "FK00" & txtNoFaktur.Text & ""
            ElseIf Len(txtNoFaktur.Text) = 2 Then
                txtNoFaktur.Text = "FK0" & txtNoFaktur.Text & ""
            ElseIf Len(txtNoFaktur.Text) = 3 Then
                txtNoFaktur.Text = "FK" & txtNoFaktur.Text & ""
            End If
        End If
    End Sub
    Sub TotalHarga()
        Dim totalnya As Integer
        totalnya = Convert.ToInt32(txtHarga.Text) * Convert.ToInt32(txtQty.Text)
        txtTotal.Text = totalnya

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If txtNoFaktur.Text <> "" And txtKodeBarang.Text <> "" And txtNamaBarang.Text <> "" And txtHarga.Text <> "" And txtQty.Text <> "" And txtTotal.Text <> "" Then
            koneksi.Close()
            koneksi.Open()

            Dim cmdsave As New OleDbCommand

            cmdsave.Connection = koneksi
            cmdsave.CommandType = CommandType.Text
            cmdsave.CommandText = "SELECT * FROM penjualan where no_faktur='" & Trim(txtNoFaktur.Text) & "'"

            Dim dr As OleDbDataReader
            dr = cmdsave.ExecuteReader

            If dr.HasRows = True Then
                dr.Read()
                MsgBox("Data Sudah Ada")
            Else


                Dim simpan As New OleDbCommand
                simpan.Connection = koneksi
                simpan.CommandType = CommandType.Text
                simpan.CommandText = "INSERT INTO penjualan (no_faktur,kode_barang,nama_barang,harga,qty,total) " &
                "VALUES ('" & txtNoFaktur.Text & "', '" & txtKodeBarang.Text & "', '" & txtNamaBarang.Text & "','" & txtHarga.Text & "','" & txtQty.Text & "','" & txtTotal.Text & "')"
                simpan.ExecuteNonQuery()
                MsgBox("Data Tersimpan")
                Call kosong()

            End If
        Else
            MsgBox("Data tidak boleh kosong !")
            txtNoFaktur.Focus()

        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtNamaBarang.Enabled = False
        txtHarga.Enabled = False
        Call KodeOtomatis()

    End Sub

    Private Sub txtKodeBarang_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtKodeBarang.KeyPress
        If Asc(e.KeyChar) = 13 Then
            CariData("barang", "kode_barang", txtKodeBarang.Text)
        End If
    End Sub
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub txtKodeBarang_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtKodeBarang.LostFocus
        If txtKodeBarang.Text = "" Then
            MsgBox("Kode Barang Masih Kosong", MsgBoxStyle.Critical)
        Else
            CariData("barang", "kode_barang", txtKodeBarang.Text)
        End If

    End Sub
    Private Sub txtKodeBarang_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtKodeBarang.TextChanged
        txtKodeBarang.Text.ToUpper()
    End Sub
    Private Sub txtQty_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtQty.KeyPress
        If Asc(e.KeyChar) = 13 Then
            Call TotalHarga()
        End If
    End Sub
    Private Sub txtQty_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQty.LostFocus
        If txtKodeBarang.Text = "" Then
            MsgBox("Kode Barang Masih Kosong", MsgBoxStyle.Critical)
        Else
            Call TotalHarga()
        End If

    End Sub


Code Ke Word:
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        koneksi.Close()
        koneksi.Open()
        Dim cmdcari As New OleDbCommand

        cmdcari.Connection = koneksi
        cmdcari.CommandType = CommandType.Text
        cmdcari.CommandText = "SELECT * FROM penjualan ORDER BY no_faktur"

        Dim dr As OleDbDataReader
        dr = cmdcari.ExecuteReader

        If dr.HasRows = True Then
            dr.Read()
            Dim MyWordApp As New word.Application
            Dim MyWordDoc As New word.Document

            MyWordDoc = MyWordApp.Documents.Open("C:\Users\Administrator\Documents\DocPenjualan.docx")
            MyWordDoc.Bookmarks("nofaktur").Select()
            MyWordApp.Selection.TypeText(dr("no_faktur"))

            MyWordDoc.Bookmarks("kodebarang").Select()
            MyWordApp.Selection.TypeText(dr("kode_barang"))

            MyWordDoc.Bookmarks("namabarang").Select()
            MyWordApp.Selection.TypeText(dr("nama_barang"))

            MyWordDoc.Bookmarks("harga").Select()
            MyWordApp.Selection.TypeText(dr("harga"))

            MyWordDoc.Bookmarks("qty").Select()
            MyWordApp.Selection.TypeText(dr("qty"))

            MyWordDoc.Bookmarks("total").Select()
            MyWordApp.Selection.TypeText(dr("total"))


            '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.SaveAs2("C:\Users\Administrator\Documents\DocPenjualan" & dr("no_faktur") & ".docx")
        Else
            MsgBox("Data tidak di temukan", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Pencarian")
        End If
        
    End Sub

Hasil Print Ke Word:

0 komentar:

Posting Komentar