Family Budget System in VB with SQL Integration

The objective of this project is to develop a family budget system using Visual Basic (VB) that integrates with a SQL database. This system will allow users to input and track their expenses and savings related to typical household items such as bills, food, and other necessities. The application will provide functionalities to calculate total spendings and savings, offering a clear overview of the family’s financial health.

Imports System.Data.SqlClient

Public Class BudgetSystem
    Private connectionString As String = "Data Source=your_server;Initial Catalog=your_database;Integrated Security=True"
    
    ' Method to add an expense
    Public Sub AddExpense(itemName As String, amount As Decimal, category As String)
        Using connection As New SqlConnection(connectionString)
            Dim command As New SqlCommand("INSERT INTO Expenses (ItemName, Amount, Category) VALUES (@ItemName, @Amount, @Category)", connection)
            command.Parameters.AddWithValue("@ItemName", itemName)
            command.Parameters.AddWithValue("@Amount", amount)
            command.Parameters.AddWithValue("@Category", category)
            connection.Open()
            command.ExecuteNonQuery()
        End Using
    End Sub

    ' Method to calculate total expenses
    Public Function CalculateTotalExpenses() As Decimal
        Dim total As Decimal = 0
        Using connection As New SqlConnection(connectionString)
            Dim command As New SqlCommand("SELECT SUM(Amount) FROM Expenses", connection)
            connection.Open()
            Dim result = command.ExecuteScalar()
            If result IsNot DBNull.Value Then
                total = Convert.ToDecimal(result)
            End If
        End Using
        Return total
    End Function

    ' Method to calculate savings
    Public Function CalculateSavings(income As Decimal) As Decimal
        Dim totalExpenses As Decimal = CalculateTotalExpenses()
        Return income - totalExpenses
    End Function
End Class

Code Explanation

The provided code defines a simple family budget system in Visual Basic that interacts with a SQL database to manage household expenses. Here’s a breakdown of the key components:

  1. Imports and Connection String:
    • The System.Data.SqlClient namespace is imported to facilitate SQL database operations.
    • A connection string is defined to connect to the SQL database. Ensure to replace your_server and your_database with actual values.
  2. AddExpense Method:
    • This method allows users to add an expense to the database. It takes three parameters: itemNameamount, and category.
    • A SQL command is prepared to insert the expense into the Expenses table. Parameters are used to prevent SQL injection attacks.
    • The connection is opened, and the command is executed to insert the data.
  3. CalculateTotalExpenses Method:
    • This function calculates the total expenses by summing the Amount column in the Expenses table.
    • It returns the total as a decimal value. If there are no expenses recorded, it returns zero.
  4. CalculateSavings Method:
    • This method calculates the savings by subtracting the total expenses from the provided income.
    • It calls the CalculateTotalExpenses method to get the current total expenses.

This budget system provides a foundational structure for managing household finances, allowing users to track their spending and savings effectively. By integrating SQL, it ensures that data is stored persistently and can be accessed easily for future calculations.

Happy coding… 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *