I have been running into some issues trying to find specific records and then saving changes. Each time I try to save the edited record the code creates a new record. Here is the coding that I am using for the form. Any suggestions on what I am doing wrong? Each field in the recordset is bound to a field in the form.
Option Compare Database
Private Sub form_load()
Dim db As DAO.Database
Dim rsViewUpdate As DAO.Recordset
Set db = CurrentDb
Set rsViewUpdate = db.OpenRecordset("select PINVSTN.* from PINVSTN", dbOpenDynaset)
Set Me.Form.Recordset = rsViewUpdate
Set rsViewUpdate = Nothing
Set db = Nothing
End Sub
Private Sub View_Update_Part_Click()
Dim db As DAO.Database
Dim rsViewUpdate As DAO.Recordset
Dim varNSN As Variant
Dim varPN As Variant
Dim strNSN As String
Dim strPN As String
Dim responce As String
Dim responce1 As String
Dim responce2 As String
Dim responce3 As String
Dim responce4 As String
Set db = CurrentDb
varNSN = Me.StockNumb1
varPN = Me.Part_num1
If IsNull(varNSN) Then
If IsNull(varPN) Then
responce = MsgBox("Please enter either the Stock Number or Part Number.", vbOKOnly)
If responce = vbOK Then
Exit Sub
End If
End If
End If
Set rsViewUpdate = db.OpenRecordset("select PINVSTN.* from PINVSTN", dbOpenDynaset)
If Not IsNull(varNSN) Then
With rsViewUpdate
.MoveFirst
Do Until .EOF
strNSN = !STOCK_NUM1
If Not varNSN = strNSN Then
If .EOF Then
responce1 = MsgBox("" & varNSN & " not found. Please re-enter the Stock Number.", vbOKOnly)
If responce1 = vbOK Then
Exit Do
End If
End If
.MoveNext
Else
Me.StockNumb1 = !STOCK_NUMB1
Me.Part_num1 = !Part_num1
Me.Part_Nomen = !Nomen
Me.Location1 = !Location
Me.QtyUP1 = !QUAN_UPACK
Me.Base_SupLoc = !BASE_SULOC
Me.BaseSupQty = !QUAN_BSULO
Me.qty1 = !QUAN_STOCK
Me.Issue_Unit1 = !ISSUE_UNIT
Me.Qty_Reord1 = !QUAN_REORD
Me.Cost1 = !COST
Me.DateLastIss1 = !LAST_IS_DT
Me.DateReord = !REORDERDATE
Me.REORDERFLAG = !REORDERFLAG
responce4 = MsgBox("Is this the Stock Number you require?", vbYesNo)
If responce4 = vbNo Then
.MoveNext
Else
Exit Do
End If
If .EOF Then
responce1 = MsgBox("" & varNSN & " not found. Please re-enter the Stock Number.", vbOKOnly)
If responce1 = vbOK Then
Me.StockNumb1 = Null
Me.Part_num1 = Null
Me.Part_Nomen = Null
Me.Location1 = Null
Me.QtyUP1 = Null
Me.Base_SupLoc = Null
Me.BaseSupQty = Null
Me.qty1 = Null
Me.Issue_Unit1 = Null
Me.Qty_Reord1 = Null
Me.Cost1 = Null
Me.DateLastIss1 = Null
Me.DateReord = Null
Me.REORDERFLAG = Null
Exit Do
End If
End If
Loop
End With
If Not IsNull(varPN) Then
With rsViewUpdate
.MoveFirst
Do Until .EOF
Do While IsNull(rsViewUpdate!Part_num1)
rsViewUpdate.MoveNext
If Not IsNull(rsViewUpdate!Partnum1) Then
Exit Do
End If
Loop
strPN = !Part_num1
If Not varPN = strPN Then
.MoveNext
If .EOF Then
responce3 = MsgBox("" & varPN & " not found. Please re-enter Part Number.", vbOKOnly)
If responce3 = vbOK Then
Me.StockNumb1 = Null
Me.Part_num1 = Null
Me.Part_Nomen = Null
Me.Location1 = Null
Me.QtyUP1 = Null
Me.Base_SupLoc = Null
Me.BaseSupQty = Null
Me.qty1 = Null
Me.Issue_Unit1 = Null
Me.Qty_Reord1 = Null
Me.Cost1 = Null
Me.DateLastIss1 = Null
Me.DateReord = Null
Me.REORDERFLAG = Null
Exit Do
End If
End If
Else
Me.StockNumb1 = !STOCK_NUMB1
Me.Part_num1 = !Part_num1
Me.Part_Nomen = !Nomen
Me.Location1 = !Location
Me.QtyUP1 = !QUAN_UPACK
Me.Base_SupLoc = !BASE_SULOC
Me.BaseSupQty = !QUAN_BSULO
Me.qty1 = !QUAN_STOCK
Me.Issue_Unit1 = !ISSUE_UNIT
Me.Qty_Reord1 = !QUAN_REORD
Me.Cost1 = !COST
Me.DateLastIss1 = !LAST_IS_DT
Me.DateReord = !REORDERDATE
Me.REORDERFLAG = !REORDERFLAG
responce2 = MsgBox("Is this the Part Number you require?", vbYesNo)
If responce2 = vbNo Then
.MoveNext
Else
Exit Do
End If
Loop
End With
End If
End If
End Sub
Private Sub Save_Edit()
Dim db As DAO.Database
Dim rsViewUpdate As DAO.Recordset
Dim responce4 As String
Dim responce5 As String
Dim varNSN1 As Variant
Dim varPN1 As Variant
Set db = CurrentDb
Set rsViewUpdate = db.OpenRecordset("select PINVSTN.* from PINVSTN", dbOpenDynaset)
If IsNull(varNSN) Then
If IsNull(varPN) Then
responce4 = MsgBox("Please enter either the Stock Number or Part Number.", vbOKOnly)
If responce4 = vbOK Then
Exit Sub
End If
End If
End If
With rsViewUpdate
.Edit
!STOCK_NUMB1 = Me.StockNumb1
!Part_num1 = Me.Part_num1
!Nomen = Me.Part_Nomen
!Location = Me.Location1
!QUAN_UPACK = Me.QtyUP1
!BASE_SULOC = Me.Base_SupLoc
!QUAN_BSULO = Me.BaseSupQty
!QUAN_STOCK = Me.qty1
!ISSUE_UNIT = Me.Issue_Unit1
!QUAN_REORD = Me.Qty_Reord1
!COST = Me.Cost1
!LAST_IS_DT = Me.DateLastIss1
!REORDERDATE = Me.DateReord
!REORDERFLAG = Me.REORDERFLAG
.Update
End With
varNSN1 = Me.StockNumb1
varPN1 = Me.Part_num1
responce5 = MsgBox("" & varNSN1 & "" & varPN1 & " successfully changed.", vbOKOnly)
If responce5 = vbOK Then
Exit Sub
End If
End Sub
Option Compare Database
Private Sub form_load()
Dim db As DAO.Database
Dim rsViewUpdate As DAO.Recordset
Set db = CurrentDb
Set rsViewUpdate = db.OpenRecordset("select PINVSTN.* from PINVSTN", dbOpenDynaset)
Set Me.Form.Recordset = rsViewUpdate
Set rsViewUpdate = Nothing
Set db = Nothing
End Sub
Private Sub View_Update_Part_Click()
Dim db As DAO.Database
Dim rsViewUpdate As DAO.Recordset
Dim varNSN As Variant
Dim varPN As Variant
Dim strNSN As String
Dim strPN As String
Dim responce As String
Dim responce1 As String
Dim responce2 As String
Dim responce3 As String
Dim responce4 As String
Set db = CurrentDb
varNSN = Me.StockNumb1
varPN = Me.Part_num1
If IsNull(varNSN) Then
If IsNull(varPN) Then
responce = MsgBox("Please enter either the Stock Number or Part Number.", vbOKOnly)
If responce = vbOK Then
Exit Sub
End If
End If
End If
Set rsViewUpdate = db.OpenRecordset("select PINVSTN.* from PINVSTN", dbOpenDynaset)
If Not IsNull(varNSN) Then
With rsViewUpdate
.MoveFirst
Do Until .EOF
strNSN = !STOCK_NUM1
If Not varNSN = strNSN Then
If .EOF Then
responce1 = MsgBox("" & varNSN & " not found. Please re-enter the Stock Number.", vbOKOnly)
If responce1 = vbOK Then
Exit Do
End If
End If
.MoveNext
Else
Me.StockNumb1 = !STOCK_NUMB1
Me.Part_num1 = !Part_num1
Me.Part_Nomen = !Nomen
Me.Location1 = !Location
Me.QtyUP1 = !QUAN_UPACK
Me.Base_SupLoc = !BASE_SULOC
Me.BaseSupQty = !QUAN_BSULO
Me.qty1 = !QUAN_STOCK
Me.Issue_Unit1 = !ISSUE_UNIT
Me.Qty_Reord1 = !QUAN_REORD
Me.Cost1 = !COST
Me.DateLastIss1 = !LAST_IS_DT
Me.DateReord = !REORDERDATE
Me.REORDERFLAG = !REORDERFLAG
responce4 = MsgBox("Is this the Stock Number you require?", vbYesNo)
If responce4 = vbNo Then
.MoveNext
Else
Exit Do
End If
If .EOF Then
responce1 = MsgBox("" & varNSN & " not found. Please re-enter the Stock Number.", vbOKOnly)
If responce1 = vbOK Then
Me.StockNumb1 = Null
Me.Part_num1 = Null
Me.Part_Nomen = Null
Me.Location1 = Null
Me.QtyUP1 = Null
Me.Base_SupLoc = Null
Me.BaseSupQty = Null
Me.qty1 = Null
Me.Issue_Unit1 = Null
Me.Qty_Reord1 = Null
Me.Cost1 = Null
Me.DateLastIss1 = Null
Me.DateReord = Null
Me.REORDERFLAG = Null
Exit Do
End If
End If
Loop
End With
If Not IsNull(varPN) Then
With rsViewUpdate
.MoveFirst
Do Until .EOF
Do While IsNull(rsViewUpdate!Part_num1)
rsViewUpdate.MoveNext
If Not IsNull(rsViewUpdate!Partnum1) Then
Exit Do
End If
Loop
strPN = !Part_num1
If Not varPN = strPN Then
.MoveNext
If .EOF Then
responce3 = MsgBox("" & varPN & " not found. Please re-enter Part Number.", vbOKOnly)
If responce3 = vbOK Then
Me.StockNumb1 = Null
Me.Part_num1 = Null
Me.Part_Nomen = Null
Me.Location1 = Null
Me.QtyUP1 = Null
Me.Base_SupLoc = Null
Me.BaseSupQty = Null
Me.qty1 = Null
Me.Issue_Unit1 = Null
Me.Qty_Reord1 = Null
Me.Cost1 = Null
Me.DateLastIss1 = Null
Me.DateReord = Null
Me.REORDERFLAG = Null
Exit Do
End If
End If
Else
Me.StockNumb1 = !STOCK_NUMB1
Me.Part_num1 = !Part_num1
Me.Part_Nomen = !Nomen
Me.Location1 = !Location
Me.QtyUP1 = !QUAN_UPACK
Me.Base_SupLoc = !BASE_SULOC
Me.BaseSupQty = !QUAN_BSULO
Me.qty1 = !QUAN_STOCK
Me.Issue_Unit1 = !ISSUE_UNIT
Me.Qty_Reord1 = !QUAN_REORD
Me.Cost1 = !COST
Me.DateLastIss1 = !LAST_IS_DT
Me.DateReord = !REORDERDATE
Me.REORDERFLAG = !REORDERFLAG
responce2 = MsgBox("Is this the Part Number you require?", vbYesNo)
If responce2 = vbNo Then
.MoveNext
Else
Exit Do
End If
Loop
End With
End If
End If
End Sub
Private Sub Save_Edit()
Dim db As DAO.Database
Dim rsViewUpdate As DAO.Recordset
Dim responce4 As String
Dim responce5 As String
Dim varNSN1 As Variant
Dim varPN1 As Variant
Set db = CurrentDb
Set rsViewUpdate = db.OpenRecordset("select PINVSTN.* from PINVSTN", dbOpenDynaset)
If IsNull(varNSN) Then
If IsNull(varPN) Then
responce4 = MsgBox("Please enter either the Stock Number or Part Number.", vbOKOnly)
If responce4 = vbOK Then
Exit Sub
End If
End If
End If
With rsViewUpdate
.Edit
!STOCK_NUMB1 = Me.StockNumb1
!Part_num1 = Me.Part_num1
!Nomen = Me.Part_Nomen
!Location = Me.Location1
!QUAN_UPACK = Me.QtyUP1
!BASE_SULOC = Me.Base_SupLoc
!QUAN_BSULO = Me.BaseSupQty
!QUAN_STOCK = Me.qty1
!ISSUE_UNIT = Me.Issue_Unit1
!QUAN_REORD = Me.Qty_Reord1
!COST = Me.Cost1
!LAST_IS_DT = Me.DateLastIss1
!REORDERDATE = Me.DateReord
!REORDERFLAG = Me.REORDERFLAG
.Update
End With
varNSN1 = Me.StockNumb1
varPN1 = Me.Part_num1
responce5 = MsgBox("" & varNSN1 & "" & varPN1 & " successfully changed.", vbOKOnly)
If responce5 = vbOK Then
Exit Sub
End If
End Sub