Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to subtract in VB with Access DB

How to subtract in VB with Access DB

Scheduled Pinned Locked Moved Visual Basic
csharpdatabasevisual-studiosecuritydebugging
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Landon Asis
    wrote on last edited by
    #1

    So i have a accdb name itemdb with table name Products and i want to subtract the value input in the txtbox8 to the field name RegBal so far i have this code

    Imports System.Data.OleDb
    Public Class Order

    Public conString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Landon-PC\\Documents\\Visual Studio 2010\\Projects\\Final\\Final\\bin\\Debug\\itemdb.accdb;Persist Security Info=False"
    Public con As New OleDbConnection
    
    
    Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con.ConnectionString = conString
        If con.State = ConnectionState.Closed Then
            con.Open()
            MsgBox("Connected")
        End If
    End Sub
    Private Sub Button4\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    
    End Sub
    Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim sqlQuery As String = "SELECT itemdb.Products, Products.RegBal, Products.RegBal-  " & TextBox8.Text & "
    
            Dim sqlCommand As New OleDbCommand
    
            With sqlCommand
                .CommandText = sqlQuery
                .Connection = con
                .ExecuteNonQuery()
            End With
            MsgBox("SAVED")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    
    End Sub
    
    
    Private Sub Button3\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()
        Main.Show()
    
    End Sub
    

    End Class

    Richard DeemingR M 2 Replies Last reply
    0
    • L Landon Asis

      So i have a accdb name itemdb with table name Products and i want to subtract the value input in the txtbox8 to the field name RegBal so far i have this code

      Imports System.Data.OleDb
      Public Class Order

      Public conString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Landon-PC\\Documents\\Visual Studio 2010\\Projects\\Final\\Final\\bin\\Debug\\itemdb.accdb;Persist Security Info=False"
      Public con As New OleDbConnection
      
      
      Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          con.ConnectionString = conString
          If con.State = ConnectionState.Closed Then
              con.Open()
              MsgBox("Connected")
          End If
      End Sub
      Private Sub Button4\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
      
      End Sub
      Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          Try
              Dim sqlQuery As String = "SELECT itemdb.Products, Products.RegBal, Products.RegBal-  " & TextBox8.Text & "
      
              Dim sqlCommand As New OleDbCommand
      
              With sqlCommand
                  .CommandText = sqlQuery
                  .Connection = con
                  .ExecuteNonQuery()
              End With
              MsgBox("SAVED")
          Catch ex As Exception
              MsgBox(ex.ToString)
          End Try
      
      End Sub
      
      
      Private Sub Button3\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
          Me.Close()
          Main.Show()
      
      End Sub
      

      End Class

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query. Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^] How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^] Query Parameterization Cheat Sheet | OWASP[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • L Landon Asis

        So i have a accdb name itemdb with table name Products and i want to subtract the value input in the txtbox8 to the field name RegBal so far i have this code

        Imports System.Data.OleDb
        Public Class Order

        Public conString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Landon-PC\\Documents\\Visual Studio 2010\\Projects\\Final\\Final\\bin\\Debug\\itemdb.accdb;Persist Security Info=False"
        Public con As New OleDbConnection
        
        
        Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            con.ConnectionString = conString
            If con.State = ConnectionState.Closed Then
                con.Open()
                MsgBox("Connected")
            End If
        End Sub
        Private Sub Button4\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        
        End Sub
        Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                Dim sqlQuery As String = "SELECT itemdb.Products, Products.RegBal, Products.RegBal-  " & TextBox8.Text & "
        
                Dim sqlCommand As New OleDbCommand
        
                With sqlCommand
                    .CommandText = sqlQuery
                    .Connection = con
                    .ExecuteNonQuery()
                End With
                MsgBox("SAVED")
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        
        End Sub
        
        
        Private Sub Button3\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Me.Close()
            Main.Show()
        
        End Sub
        

        End Class

        M Offline
        M Offline
        Michael_Davies
        wrote on last edited by
        #3

        First Richard Deeming is right, do not concatenate SQL strings form user input, wide open to injection attack, use parameterised queries. That said your SQL will do what you ask, however it is a SELECT statement which will return rows of data to your program, take it that part of your sqlQuery string is missing as you also need a FROM and table name. You execute it as NonQuery which will return nothing from a SELECT anyway, you need to look at UPDATE if you want to alter the values in table(s).

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups