Loading

updating & viewing records on ms access using vb.net



hello!

i'm new to vb.net and have very little knowledge of it especially when it comes to databases so i'm not really sure i can give a sample code right now. maybe if it's vb6 then that might be different but i'm supposed to use this :D
maybe screenshots on what i'm doing would work for now.

right now i'm working on a coffee shop inventory system but my concern for now is this...

here's my problem

i need help on how to update records on an existing access database using vb.net with just a textbox and a command button and how a specific record of a field can show up in a label or a textbox

i connected vb.net to my access database by just clicking on "add new data source">>"new connection">>"microsoft access database file" and just continue..tested connection and done.

as you can see in the screenshot of my form, by default the left side textboxes are disabled but they're enabled if the user checks the checkboxes and disabled again if unchecked.

assuming that the textbox is already enabled and i want to enter a value in the Brewed Coffee area of the form...for example 5. when i click on my Submit button the value of that particular record in the database will be updated, so in my table the value of the quantity field in Brewed Coffee will be 5.

another thing is that how can i see a particular value on a label or textbox from my database like on the Prices area. in my database if a product's price is 80 then it should show a 80 in the label. the values of the prices from the screesnshot of the form are not the ones on the database xD i just typed them in...
just guide me to the right path po!

thanks!!

helpful hints, tips, and suggestions will be greatly appreciated

i'll try to add more info if needed

AttachmentSize
form.jpg44.24 KB
msaccessdb.jpg32.34 KB


admin's picture

Your question is pretty simple if you will just follow my tutorial on Database Programming Made Easy.

thanks i somehow understand now

i managed to display a datagrid from an Access database but now i need to find the total sum of a column from it

it can either be displayed in a textbox or a label

please help i really don't know how

thank you very much!

admin's picture

I have already included this on my Hotel Reservation System (VB.NET).

Let me know if you can't find it there so I can separate this on other page.

Plz help me out sir,

I am new 2 vb.net
I have prepared forms n database bt dont knw how to connect them.

plz

hi admin

thanks for hotel management software. it is very high level for me now to understand. i write a simple access database manipulating program. this program read data from database columns and display in the list view and SUMS in the labels. but i was unable to display them in the labels.

so, can u please help. here is my code below.

  1. Imports System.Data.OleDb
  2.  
  3. Public Class Form1
  4. Dim Cmd As New OleDbCommand
  5. Dim db As New OleDbCommand
  6. Dim cd As New OleDbCommand
  7. Dim Reader As OleDbDataReader
  8. Dim Cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data " & _
  9. "Source=|DataDirectory|\tv.mdb; Persist Security Info=True; " & _
  10. "Jet OLEDB:Database Password=12345")
  11.  
  12. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  13. Cmd.Connection = Cn
  14. Cmd.CommandText = "SELECT Dbt AS Debit, Cdt AS Credit FROM AccTransaction"
  15. Cn.Open()
  16. Reader = Cmd.ExecuteReader
  17. lv_Trans.Clear()
  18.  
  19. For i As Integer = 0 To Reader.FieldCount - 1
  20. lv_Trans.Columns.Add(Reader.GetName(i), 130, HorizontalAlignment.Center)
  21. Next
  22. While Reader.Read
  23. Dim LI As New ListViewItem
  24. LI.Text = Convert.ToString(Reader.Item("Debit"))
  25. LI.SubItems.Add(Convert.ToString(Reader.Item("Credit")))
  26. lv_Trans.Items.Add(LI)
  27. End While
  28. Reader.Close()
  29. Cn.Close()
  30.  
  31. 'here problem begins with lbl_Dbt.Text. it gives an exception error
  32. Cmd.CommandText = "SELECT SUM(Dbt), SUM(Cdt) FROM AccTransaction"
  33. Cn.Open()
  34. Reader = Cmd.ExecuteScalar
  35. While Reader.Read
  36. lbl_Dbt.Text = ??????
  37. lbl_Cdt.Text = ??????
  38. End While
  39. Cn.Close()
  40. End Sub
  41. End Class
admin's picture

You can actually do that within this line:

  1. For i As Integer = 0 To Reader.FieldCount - 1
  2. lv_Trans.Columns.Add(Reader.GetName(i), 130, HorizontalAlignment.Center)
  3. Next
  4. While Reader.Read
  5. Dim LI As New ListViewItem
  6. LI.Text = Convert.ToString(Reader.Item("Debit"))
  7. LI.SubItems.Add(Convert.ToString(Reader.Item("Credit")))
  8. lv_Trans.Items.Add(LI)
  9.  
  10. lbl_Dbt.Text = lbl_Dbt.Text + Reader.Item("Debit")
  11. lbl_Cdt.Text = lbl_Cdt.Text + Reader.Item("Credit")
  12. End While

Kindly check for data type conversion to avoid error.

twilight's picture

hi admin

thx for ur reply. as i mentioned earlier, i have two labels to display two different sql SUM operation in them. how can i perform two different sql operation in one while statement. for ur better understanding i upload the project. please check it.

waiting for ur reply

admin's picture

See attached file

twilight's picture

hi admin

thanks for ur reply. i found another example in the net and tried it. but still no luck and get this conversion error "Conversion from string "Dbt" to type 'Integer' is not valid." and i don't know how to convert or deal with this specific problem.

so can u help me please. below i add my code and the whole project for ur better understanding.

Imports System.Data.OleDb

Public Class Form1
Dim Cmd As New OleDbCommand
Dim db As New OleDbCommand
Dim cd As New OleDbCommand
Dim Reader As OleDbDataReader
Dim Cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data " & _
"Source=|DataDirectory|\tv.mdb; Persist Security Info=True; " & _
"Jet OLEDB:Database Password=12345")

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Cmd.Connection = Cn
Cmd.CommandText = "SELECT Dbt AS Debit, Cdt AS Credit FROM AccTransaction"
Cn.Open()
Reader = Cmd.ExecuteReader
lv_Trans.Clear()

For i As Integer = 0 To Reader.FieldCount - 1
lv_Trans.Columns.Add(Reader.GetName(i), 130, HorizontalAlignment.Center)
Next
While Reader.Read
Dim LI As New ListViewItem
LI.Text = Convert.ToString(Reader.Item("Debit"))
LI.SubItems.Add(Convert.ToString(Reader.Item("Credit")))
lv_Trans.Items.Add(LI)
End While
Reader.Close()
Cn.Close()

'here problem begins with lbl_Dbt.Text. it gives an exception error
Cmd.CommandText = "SELECT SUM(Dbt), SUM(Cdt) FROM AccTransaction"
Cn.Open()
Reader = Cmd.ExecuteReader
While Reader.Read
lbl_Dbt.Text = Reader.GetString("Dbt")
lbl_Cdt.Text = Reader.GetString("Cdt")
End While
Cn.Close()
End Sub
End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
Dim frm As Form
frm = New info

If dtable.Rows.Count <> 0 Then

dtable.Rows(rowspost)("StudYr") = txtyear.Text
dtable.Rows(rowspost)("StudName") = txtname.Text
dtable.Rows(rowspost)("StudCon") = txtcontact.Text
dtable.Rows(rowspost)("StudAdd") = txtadd.Text
dtable.Rows(rowspost)("StudCors") = txtcourse.Text
dtable.Rows(rowspost)("StudDate") = dtp.Text
dtable.Rows(rowspost)("StudBal") = lblbill.Text
adapter.Update(dtable)
MessageBox.Show("Update successful")
End If

End Sub

what is wrong here? only the first row is updating, there's error when updating the rest rows.

my complete codes:

Imports System.Data.OleDb
Imports System.IO
Imports System.Data
Public Class info
Private con As New OleDb.OleDbConnection
Private adapter As New OleDb.OleDbDataAdapter
Private dtable As New DataTable
Private cbuilder As New OleDb.OleDbCommandBuilder
Private rowspost As Integer = 0
Private Sub info_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call dbconnect()

End Sub
Private Sub lblmain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblmain.Click
main.Show()
Me.Hide()
End Sub

Private Sub btnpay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpay.Click
Dim b As Double
b = Val(lblbalance.Text) - Val(txtmbill.Text)
lblbill.Text = b
End Sub
Private Sub dbconnect()
con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
System.Environment.CurrentDirectory.ToString() & "\db2.mdb")
con.Open()
adapter = New OleDb.OleDbDataAdapter("Select * from Studentinfo", con)
cbuilder = New OleDb.OleDbCommandBuilder(adapter)
adapter.Fill(dtable)
End Sub
Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click
Dim com As OleDbCommand
Dim srch As OleDbDataReader
com = New OleDbCommand
com.Connection = con
com.CommandType = CommandType.Text
com.CommandText = String.Format("select * from Studentinfo where StudId ='{0}'", txtstudid.Text)
srch = com.ExecuteReader
If srch.Read() Then
lblid.Text = srch("StudId")
txtcourse.Text = srch("StudCors")
txtyear.Text = srch("StudYr")
txtname.Text = srch("StudName")
txtadd.Text = srch("StudAdd")
txtcontact.Text = srch("StudCon")
txtplan.Text = srch("StudPlan")
txtbfull.Text = srch("StudTuition")
lblbalance.Text = srch("StudBal")
lblmbill.Text = srch("StudMonthbill")
lblbill.Text = "0"
txtmbill.Text = "0"
Else
MessageBox.Show("MisMatch")
End If
End Sub

Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim frm As Form
frm = New info

If dtable.Rows.Count <> 0 Then
dtable.Rows(rowspost)("StudId") = lblid.Text
dtable.Rows(rowspost)("StudYr") = txtyear.Text
dtable.Rows(rowspost)("StudName") = txtname.Text
dtable.Rows(rowspost)("StudCon") = txtcontact.Text
dtable.Rows(rowspost)("StudAdd") = txtadd.Text
dtable.Rows(rowspost)("StudCors") = txtcourse.Text
dtable.Rows(rowspost)("StudDate") = dtp.Text
dtable.Rows(rowspost)("StudBal") = lblbill.Text
adapter.Update(dtable)
MessageBox.Show("Update successful")
frm.Show()
Me.Hide()
End If

End Sub

Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
Dim strSQL As String
Dim frm As New Form
If MessageBox.Show("Are you sure you want to delete the current curriculum from the database?", "Curriculum Deletion Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
strSQL = "DELETE FROM Studentinfo WHERE StudId='" & txtstudid.Text & "'"
Dim dCmdx As OleDbCommand = New OleDbCommand(strSQL, con)
dCmdx.ExecuteNonQuery()
dCmdx.Dispose()
MessageBox.Show("A curriculum is deleted from the database.", "Curriculum Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information)
frm = New info
frm.Show()
Me.Hide()
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
Dim frm As Form
frm = New info

If dtable.Rows.Count <> 0 Then

dtable.Rows(rowspost)("StudYr") = txtyear.Text
dtable.Rows(rowspost)("StudName") = txtname.Text
dtable.Rows(rowspost)("StudCon") = txtcontact.Text
dtable.Rows(rowspost)("StudAdd") = txtadd.Text
dtable.Rows(rowspost)("StudCors") = txtcourse.Text
dtable.Rows(rowspost)("StudDate") = dtp.Text
dtable.Rows(rowspost)("StudBal") = lblbill.Text
adapter.Update(dtable)
MessageBox.Show("Update successful")
End If

End Sub

Private Sub txtplan_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtplan.TextChanged
If txtplan.Text = "Full" Then
btnpay.Enabled = False
btnupdate.Enabled = False

End If
End Sub
End Class

admin's picture

If possible attached your project file here so I can see your database schema also.

hi i need your help
help me with update code
am online till 5pm
thank you

Add new comment