Click here to Skip to main content
16,007,472 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
VB
Imports System.Data.OleDb
Imports System.Data
Public Class WithdrawandDeposit2
    Dim adapt As New OleDbDataAdapter
    Dim dset As New DataSet
    Dim bal, num As String
    Dim total As Integer
    Function draw()
        cn = New OleDb.OleDbConnection
        With cn
            .ConnectionString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Maccount.accdb"
            .Open()
        End With
        Dim dt As New DataSet("NewAccount")
        Dim rs As New OleDb.OleDbDataAdapter(" Select * from NewAccount where Saccountno = '" + TextBox1.Text + "'", cn)
        rs.Fill(dt)
        If dt.Tables(0).Rows.Count > 0 Then
            bal = dt.Tables(0).Rows(0)(6).ToString()
            num = dt.Tables(0).Rows(0)(0).ToString()
            TextBox2.Text = dt.Tables(0).Rows(0)(2).ToString()
        End If
        Return 0
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Val(WithBox.Text) > bal Then
            LowerBalance.Show()
        ElseIf Me.WithBox.Text = "" Then
            Errorform1.Show()
        Else
            total = bal - Val(WithBox.Text)
            Dim dbcommand As String = "update NewAccount set Samount = '" & total & "'where AccountName='" & TextBox1.Text & "'"
            adapt = New OleDbDataAdapter(dbcommand, cn)
            dset = New DataSet
            adapt.Fill(dset)
            'rs.Dispose()
            'cn.Close()
            Call draw()

        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
        Transactions.Show()
    End Sub

    Private Sub WithdrawandDeposit2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call draw()
        TextBox2.Enabled = False
    End Sub
End Class


What I have tried:

I am trying to reduce my current balance in access database, the problem is when i click button1 the result is always the if val(withBox.text) Statement
Posted
Updated 8-Mar-16 15:02pm
v3
Comments
Ben J. Boyle 8-Mar-16 10:10am    
Both the value for "bal" and the value from the textbox are strings, so you may be falling fouls of the string comparison, where for example "10" is less than "2"
ZurdoDev 8-Mar-16 10:33am    
Just debug the code and watch what is happening. Simple.
Sergey Alexandrovich Kryukov 8-Mar-16 10:48am    
With Access? You cannot be serious. It would need as a minimum, at least the client-server architecture...
—SA
Maciej Los 8-Mar-16 11:00am    
Well said!

1 solution

For starters, never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Once you have fixed that, use the debugger.
Make a trivial change to make debugging easier:
Change
VB
If Val(WithBox.Text) > bal Then
    LowerBalance.Show()
To
VB
Dim wb = Val(WithBox.Text)
If wb > bal Then
    LowerBalance.Show()

Put a breakpoint on the first line of the Button1_Click handler, and run your program.
Now step over the code and watch what happens to your variables. Look ate exactly what they contain, and work out what the code should do as a result, before you step each line. Did it do what you expected? If so, move on. If not...why not? It should be reasonably obvious what the problem is if you look carefully at what is happening.
 
Share this answer
 
Comments
CaptainChizni 8-Mar-16 22:28pm    
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Val(WithBox.Text) > bal Then
LowerBalance.Show()
ElseIf Me.WithBox.Text = "" Then
Errorform1.Show()
Else
total = bal - Val(WithBox.Text)
Dim dbcommand As String = "update NewAccount set Samount = '" & total & "'where AccountName='" & TextBox1.Text & "'"
adapt = New OleDbDataAdapter(dbcommand, cn)
dset = New DataSet
adapt.Fill(dset)
'rs.Dispose()
'cn.Close()
Call draw()

End If
End Sub
im getting a problem in else statement. i don't know if it's the data types or my access database. I've try the Option Strict but nothing's happening. Please help me I'm new to vb.net and Ms access if only i can post my database file here

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900