0

I am developing a point of sale system using vb.net and SQL Server Compact 4.0. I have a new transaction button that generates a new invoice number using id once clicked. But the problem is its generates a new invoice even when an existing transaction isn't completed. I want to disable this until a transaction is completed, that is once payment is saved where its proceeds to the next invoice number automatically. Kindly help.

    Private Sub btnNewTrns_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewTrns.Click
    If cmbCustomer.SelectedValue Is Nothing Or cmbCustomer.SelectedIndex = -1 Then
        MsgBox("Required: Customer name", MsgBoxStyle.Information, "Information")
    Else
        ExecuteSQLQuery(" INSERT INTO tbl_sale (Sales_Date, Sales_Time, User_ID, customer_id, grand_disc, collection_full, POS, term_of_payment, consin1, consin2, narration, Payment, Change) VALUES  " &
                         " ('" & Format(Now, "MM/dd/yyyy") & "', '" + TimeOfDay + "', '" & xUser_ID & "', " & cmbCustomer.SelectedValue & ", 0, 'N', 'Y', 'DUE', '-', '-', '-',0 , 0) ")
        ExecuteSQLQuery("SELECT        SALES_ID  FROM            tbl_sale  ORDER BY SALES_ID DESC")
        txtInvoiceID.Text = sqlDT.Rows(0)("SALES_ID")
        loadSalesItem()
        txtBarcode.Select()
    End If
End Sub

Until this happens

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    If cmbType.Text = "" Then
        MsgBox("Required: TERM OF PAYMENT", MsgBoxStyle.Information, "Information")
    ElseIf Not IsNumeric(txtPayment.Text) Then
        MsgBox("Required: Payment", MsgBoxStyle.Information, "Information")
    ElseIf Val(txtPayment.Text) <= 0 Then
        MsgBox("Required: Payment", MsgBoxStyle.Information, "Information")
    Else
        If Val(txtGtotal.Text) <= Val(txtPayment.Text) Then
            ExecuteSQLQuery(" UPDATE  tbl_sale SET  grand_disc=" & str_repl(txtDiscount.Text) & ", collection_full='Y', term_of_payment='" + cmbType.Text + "', Change='" & txtChange.Text & "', Payment='" & txtPayment.Text & "'  WHERE        (SALES_ID = " & txtInvoiceID.Text & ")")
            MsgBox("Payment Completed.", MsgBoxStyle.Information, "Information")
            If MsgBox("Are you sure to print preview INV. NO#  " & txtInvoiceID.Text, MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, "Confirm..") = MsgBoxResult.Yes Then
                LoadCompanyDetails()
                LoadPOSRecipt(txtInvoiceID.Text)
            End If
            Me.Close()
            frmPOS.btnNewTrns.PerformClick()
            frmPOS.LoadItem()
            frmPOS.txtBarcode.Select()
        Else
            MsgBox("Sorry! Payment not acceptable.", MsgBoxStyle.Critical, "Sorry!")
        End If
    End If
End Sub
LarsTech
  • 80,625
  • 14
  • 153
  • 225
kelvin
  • 1
  • 1
    `btnNewTrns.Enabled = False` or `btnNewTrns.Enabled = True` will disable/enable the buttons. You should place them at the correct point in your code, bearing in mind Exceptions and other expected failures – JayV Dec 15 '20 at 11:22
  • 1
    Welcome, take the [tour]. You could consider rewriting to avoid sql injection issues. Also be worth splitting your methods into logical methods: 'validation', 'saleadd','saleupdate','refreshsalelist' etc. – Andrew Mortimer Dec 15 '20 at 11:23
  • Note that if you're using WPF, instead of setting `Enabled` on the button directly, it may work better to set a boolean variable to which the enabled state of the button is bound (and remember to notify for changes so the UI will update). The same considerations as in JayV's comment apply. – Craig Dec 15 '20 at 13:44
  • 1
    @Craig From the signature of the button events, it looks like WinForms. – Mary Dec 16 '20 at 00:58
  • Doesn't Compact have a DateTime datatype? If possible don't store dates and times as strings. Suppose you want to retrieve the sales from date1 to date2. If you have stored as string - Good Luck! – Mary Dec 16 '20 at 01:05
  • Doesn't Compact have Scope_Identity()? Retrieving all SalesID values is a slow way to do this. – Mary Dec 16 '20 at 01:08
  • Please turn on Option Strict. This is a 2 part process. First for the current project - In Solution Explorer double click My Project. Choose Compile on the left. In the Option Strict drop-down select ON. Second for future projects - Go to the Tools Menu -> Options -> Projects and Solutions -> VB Defaults. In the Option Strict drop-down select ON. This will save you from bugs at runtime. – Mary Dec 16 '20 at 01:08
  • No IsNumeric! Use the .TryParse methods. – Mary Dec 16 '20 at 01:10
  • Don't concatenate strings to build sql statements. Always use parameters. – Mary Dec 16 '20 at 01:11
  • 1
    Get rid of Val() from vb6 too. – Mary Dec 16 '20 at 01:12

0 Answers0