Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AddNew not adding record to end of recordset?

Status
Not open for further replies.

dmkolb

Technical User
Feb 17, 2003
15
0
0
US
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. dmkolb@comcast.net
 
Double post, please respond in thread709-477925. Thanks and Good Luck!

zemp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top