What's new

Visual Basic HEEEEEEEEEEEEEEEELP

Spaghetto

Eternal Poster
Joined
Feb 23, 2017
Posts
435
Reaction
680
Points
289
Pa help po wala naman po error sa error list pero bat ganun huhu


1665749436237.png

1665749456770.png

Code:
Imports System.Data.SqlClient
Imports System.Net
Imports System.Security.Authentication.ExtendedProtection

Public Class Form1
    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
        Me.Dispose()

    End Sub

    Private Sub txtQty_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtQty.KeyPress
        Select Case Asc(e.KeyChar)
            Case 48 To 57
            Case 46
            Case 8
            Case Else
                e.Handled = True
        End Select
    End Sub

    Private Sub txtUnitPrice_TextChanged(sender As Object, e As EventArgs) Handles txtUnitPrice.TextChanged
        CalculateTotal()
    End Sub

    Private Sub txtDiscount_TextChanged(sender As Object, e As EventArgs) Handles txtDiscount.TextChanged
        CalculateTotal()
    End Sub

    Private Sub txtDiscount_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtDiscount.KeyPress
        Select Case Asc(e.KeyChar)
            Case 48 To 57
            Case 46
            Case 8
            Case Else
                e.Handled = True
        End Select
    End Sub
    Function GetInvoice() As String
        Try
            Dim sdate As String = Now.ToString("MMddyy")
            cn.Open()
            cm = New SqlCommand("select top 1 invoiceno from bills where invoiceno like '" & sdate & "%' order by id desc", cn)
            dr = cm.ExecuteReader
            dr.Read()
            If dr.HasRows Then GetInvoice = CLng(dr.Item("invoiceno").ToString) + 1 Else GetInvoice = sdate & "1001"
            dr.Close()
            cn.Close()

        Catch ex As Exception
            cn.Close()
            MsgBox(ex.Message, vbCritical)
        End Try
    End Function

    Private Sub billing_Click(sender As Object, e As EventArgs) Handles billing.Click
        txtInvoiceNo.Text = GetInvoice()
    End Sub
    Sub UpdateBill()

        Dim found As Boolean = False
        cn.Open()
        cm = New SqlCommand("select * from bills where invoiceno Like '" & txtInvoiceNo.Text & "'", cn)
        dr = cm.ExecuteReader
        dr.Read()
        If dr.HasRows Then found = True Else found = False
        cn.Close()
        dr.Close()

        If found = True Then
            cn.Open()
            cm = New SqlCommand("Update bills Set invoicedate = @invoicedate, billto = @billto, address = @address, invoicefor = @invoicefor, subtotal = @subtotal, taxrate = @taxrate, other = @other, where invoiceno = invoiceno", cn)
            With cm.Parameters
                .AddWithValue("@invoicedate", dtInvoice.Value)
                .AddWithValue("@billto", txtBill.Text)
                .AddWithValue("@address", txtAddress.Text)
                .AddWithValue("@invoicefor", txtInvoiceFor.Text)
                .AddWithValue("@subtotal", CDbl(txtSubTotal.Text))
                .AddWithValue("@taxrate", CDbl(txtTax.Text))
                .AddWithValue("@other", CDbl(txtOthers.Text))
                .AddWithValue("@invoiceno", txtInvoiceNo.Text)

            End With
            cm.ExecuteNonQuery()
            cn.Close()
        Else
            cn.Open()
            cm = New SqlCommand("insert into bills set (@invoiceno, @invoicedate,@ billto,@ address, @invoicefor, @subtotal, @taxrate, @other)", cn)
            With cm.Parameters
                .AddWithValue("@invoiceno", txtInvoiceNo.Text)
                .AddWithValue("@invoicedate", dtInvoice.Value)
                .AddWithValue("@billto", txtBill.Text)
                .AddWithValue("@address", txtAddress.Text)
                .AddWithValue("@invoicefor", txtInvoiceFor.Text)
                .AddWithValue("@subtotal", CDbl(txtSubTotal.Text))
                .AddWithValue("@taxrate", CDbl(txtTax.Text))
                .AddWithValue("@other", CDbl(txtOthers.Text))
            End With
            cm.ExecuteNonQuery()
            cn.Close()
        End If
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Connection()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            If txtBill.Text = String.Empty Or txtAddress.Text = String.Empty Or txtInvoiceNo.Text = String.Empty Or txtInvoiceFor.Text = String.Empty Then
                MsgBox("Required Missing Field!", vbCritical)
                Return
            End If
            If txtDescription.Text = String.Empty Or txtQty.Text = String.Empty Or txtUnitPrice.Text = String.Empty Or txtDiscount.Text = String.Empty Then
                MsgBox("Required Missing Field!", vbCritical)
                Return
            End If
            cn.Open()
            cm = New SqlCommand("insert into bills_items(invoiceno, desccription, qty, unitprice,discount)values(@invoiceno, @description,@qty,@unitprice,@discount)", cn)
            With cm.Parameters
                .AddWithValue("@invoiceno", txtInvoiceNo.Text)
                .AddWithValue("@description", txtDescription.Text)
                .AddWithValue("@qty", CDbl(txtQty.Text))
                .AddWithValue("@unitprice", CDbl(txtUnitPrice.Text))
                .AddWithValue("@discount", CDbl(txtDescription.Text))
            End With
            cm.ExecuteNonQueryAsync()
            cn.Close()
            Loadrecords()

        Catch ex As Exception
            cn.Close()
            MsgBox(ex.Message, vbCritical)
        End Try
    End Sub


    Sub CalculateTotal()
        Try
            txtTotal.Text = Format((CDbl(txtUnitPrice.Text) * CDbl(txtQty.Text)) - CDbl(txtDiscount.Text), "#,##0.00")
        Catch ex As Exception
            txtTotal.Text = "0.00"
        End Try

    End Sub

    Private Sub txtQty_TextChanged(sender As Object, e As EventArgs) Handles txtQty.TextChanged
        CalculateTotal()
    End Sub
    Sub Loadrecords()
        Dim _subtotal As Double = 0
        DataGridView1.Rows.Clear()
        Dim i As Integer
        cn.Open()
        cm = New SqlCommand("select * from bills_items where invoiceno = '" & txtInvoiceNo.Text & "'", cn)
        dr = cm.ExecuteReader
        While dr.Read
            i += 1
            _subtotal += CDbl(dr.Item("total").ToString)
            DataGridView1.Rows.Add(dr.Item("id").ToString, i, dr.Item("description").ToString, Format(CDbl(dr.Item("qty").ToString), "#,##0.00"), Format(CDbl(dr.Item("unitprice").ToString), "#,##0.00"), Format(CDbl(dr.Item("discount").ToString), "#,##0.00"), Format(CDbl(dr.Item("total").ToString), "#,##0.00"), "EDIT", "DELETE")
        End While
        dr.Close()
        cn.Close()

        txtSubTotal.Text = Format(_subtotal, "#,##0.00")
        CalculateBill()

    End Sub
    Sub CalculateBill()
        Try
            txtSales.Text = Format(CDbl(txtSubTotal.Text) * CDbl(txtTax.Text), "#,##0.00")
            txtGrandTotal.Text = Format(CDbl(txtSubTotal.Text) + CDbl(txtTax.Text) + CDbl(txtOthers.Text), "#,##0.00")
        Catch ex As Exception
            txtGrandTotal.Text = "0.00"
        End Try
    End Sub



    Private Sub txtTax_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtTax.KeyPress
        If Asc(e.KeyChar) = 13 Then
            UpdateBill()
        End If
    End Sub

    Private Sub txtOthers_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtOthers.KeyPress
        If Asc(e.KeyChar) = 13 Then
            UpdateBill()
        End If
    End Sub
End Class
 

Attachments

Back
Top