Hello,
I am using a form in VB to access a db to view, add, edit, etc.
The addnew button adds a record to the bottom of the table but it also over writes the record the datacontrol happens to be on at the time the addnew is used. Below is some of the code, I hope someone can help me with this.
Option Explicit
Public db As Database
Public rs As Recordset
Public ws As Workspace
Public x As Integer
Public intMsg As Integer
Public Function initdb()
Set ws = DBEngine.Workspaces(0)
Set db = ws.OpenDatabase("A:\inventory.mdb"
Set rs = db.OpenRecordset("table", dbOpenDynaset)
End Function
Private Sub cmdAddNew_Click()
x = 1
Set rs = db.OpenRecordset("table", dbOpenDynaset)
rs.MoveLast
txtPartNo.Text = ""
txtQuantity.Text = ""
txtPrice.Text = ""
txtDesc.Text = ""
lblID.Caption = rs.RecordCount + 1
txtPartNo.SetFocus
End Sub
Private Sub cmdDone_Click()
If x = "1" Then
rs.AddNew
rs("ID" = rs.RecordCount + 1
rs("PartNo" = txtPartNo.Text
rs("Quantity" = txtQuantity.Text
rs("Price" = Format(txtPrice.Text, "$##.##"
rs("Desc" = txtDesc.Text
rs.Update
intMsg = MsgBox("Record has been updated sucessfully!", vbOKOnly)
End If
If x = "2" Then
' Code here for edit function which seems to work ok
End If
Set rs = db.OpenRecordset("table", dbOpenDynaset)
rs.MoveFirst
End Sub
Private Sub Form_Load()
initdb
If rs.RecordCount > 0 Then
rs.MoveFirst
txtPartNo.Text = rs("PartNo"
txtQuantity.Text = rs("Quantity"
txtPrice.Text = Format(rs("Price", "$##.##"
txtDesc.Text = rs("Desc"
lblID.Caption = rs("ID"
End If
End Sub
Another problem is when the form shows the price it isn't in the correct format?!
Any help would be appreciated, Thanks.
I am using a form in VB to access a db to view, add, edit, etc.
The addnew button adds a record to the bottom of the table but it also over writes the record the datacontrol happens to be on at the time the addnew is used. Below is some of the code, I hope someone can help me with this.
Option Explicit
Public db As Database
Public rs As Recordset
Public ws As Workspace
Public x As Integer
Public intMsg As Integer
Public Function initdb()
Set ws = DBEngine.Workspaces(0)
Set db = ws.OpenDatabase("A:\inventory.mdb"
Set rs = db.OpenRecordset("table", dbOpenDynaset)
End Function
Private Sub cmdAddNew_Click()
x = 1
Set rs = db.OpenRecordset("table", dbOpenDynaset)
rs.MoveLast
txtPartNo.Text = ""
txtQuantity.Text = ""
txtPrice.Text = ""
txtDesc.Text = ""
lblID.Caption = rs.RecordCount + 1
txtPartNo.SetFocus
End Sub
Private Sub cmdDone_Click()
If x = "1" Then
rs.AddNew
rs("ID" = rs.RecordCount + 1
rs("PartNo" = txtPartNo.Text
rs("Quantity" = txtQuantity.Text
rs("Price" = Format(txtPrice.Text, "$##.##"
rs("Desc" = txtDesc.Text
rs.Update
intMsg = MsgBox("Record has been updated sucessfully!", vbOKOnly)
End If
If x = "2" Then
' Code here for edit function which seems to work ok
End If
Set rs = db.OpenRecordset("table", dbOpenDynaset)
rs.MoveFirst
End Sub
Private Sub Form_Load()
initdb
If rs.RecordCount > 0 Then
rs.MoveFirst
txtPartNo.Text = rs("PartNo"
txtQuantity.Text = rs("Quantity"
txtPrice.Text = Format(rs("Price", "$##.##"
txtDesc.Text = rs("Desc"
lblID.Caption = rs("ID"
End If
End Sub
Another problem is when the form shows the price it isn't in the correct format?!
Any help would be appreciated, Thanks.