Hi,
so i have this database that has unbound forms and unbound subforms (not my choice but, have to go with it). So i have a subform that has a listbox that pulls out a list of vendors from table1 which is store in subform1. Now this subform: subform 2 records the vendor's license info etc on it which is unbound as well. the information on subform2 is stored in table 2. The main form for both subforms are not the same as they are stored in a different section of the database.
I am having issues with the SAVE button. When a user clicks on the listbox, the subform loads with the corresponding contact names to that vendor list. But after filling out one of the fields on the subform and hitting SAVE button, the values are saved. but, say i change a value and click save, a new record is created on the table. i dont know how to set it right. can you guys help me. Please find code below and the image of the subform attached.
Please help:banghead:
so i have this database that has unbound forms and unbound subforms (not my choice but, have to go with it). So i have a subform that has a listbox that pulls out a list of vendors from table1 which is store in subform1. Now this subform: subform 2 records the vendor's license info etc on it which is unbound as well. the information on subform2 is stored in table 2. The main form for both subforms are not the same as they are stored in a different section of the database.
I am having issues with the SAVE button. When a user clicks on the listbox, the subform loads with the corresponding contact names to that vendor list. But after filling out one of the fields on the subform and hitting SAVE button, the values are saved. but, say i change a value and click save, a new record is created on the table. i dont know how to set it right. can you guys help me. Please find code below and the image of the subform attached.
Code:
Private Sub Save_Click()
On Error GoTo cmd_Save_Click_Err
On Error Resume Next
Dim db As Database
Set db = CurrentDb
Dim rs As DAO.Recordset
Dim rsC As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim SQLStr As String
Dim SQLStrC As String
Dim SQLStrU As String
Dim rcount As Integer
Dim log As Integer
Dim LCode As Integer
SQLStrC = "SELECT Count(*) FROM TMFStaffQual LEFT JOIN Contacts ON TMFStaffQual.ContactsId = Contacts.ContactId" & _
"WHERE TMFStaffQual.TMFStaffQualID = " & Me.lstContacts.Value
Set rsC = db.OpenRecordset(SQLStrC, dbOpenDyanset)
rsC.MoveFirst
rcount = rsC.Fields(0).Value
rsC.Close
Set rsC = Nothing
SQLStr = "SELECT TMFStaffQual.TMFStaffQualID, TMFStaffQual.SiteId, TMFStaffQual.FDstat" & _
"FROM TMFStaffQual LEFT JOIN Contacts ON TMFStaffQual.ContactsId = Contacts.ContactID" & _
", TMFStaffQual.FDdt, TMFStaffQual.CVstat, TMFStaffQual.CVdt, TMFStaffQual.CVexpdt" & _
"WHERE TMFStaffQual.SiteId = " & Me.Parent!txtNewSiteId.Value & " AND TMFStaffQual.TMFStaffQualID = " & Me.lstContacts.Value
Set rs = db.OpenRecordset(SQLStr, dbOpenDynaset)
rs.Close
If rcount > 1 Then
ElseIf rcount = 0 Then
Set rs2 = db.OpenRecordset("TMFStaffQual")
With rs2
.AddNew
![siteid] = Me.Parent!txtNewSiteId.Value
![ContactsId] = Me.lstContacts.Value
![FDstat] = Me.txtFD.Value
![FDdt] = Me.txtFDdt.Value
![CVstat] = Me.txtCV.Value
![CVdt] = Me.txtCVdt.Value
![CVexpdt] = Me.txtCVexp.Value
![Lstat] = Me.txtLic.Value
![State] = Me.txtst.Value
![LNum] = Me.txtLnum.Value
![Lexpdt] = Me.txtLexp.Value
![Sign] = Me.txtSign.Value
![Signdt] = Me.txtSigndt.Value
![Train] = Me.txtTrain.Value
![Traindt] = Me.txtTrdt.Value
![EDC] = Me.txtEDC.Value
![EDCdt] = Me.txtEDCdt.Value
![PFT] = Me.txtPFT.Value
![PFTdt] = Me.txtPFTdt.Value
.Update
End With
rs2.Close
Set rs2 = Nothing
MsgBox ("Record is saved")
SQLStrO = "UPDATE Contacts Set [Corder] = " & "'" & txtorder.Value & "'" & " WHERE Contacts.ContactID = " & Me.lstContacts.Value
db.Execute SQLStrO
rs2.Close
Set rs2 = Nothing
Else
SQLStrU = "UPDATE TMFStaffQual SET [Fdstat] = " & "'" & Me.txtFD.Value & "'" & ", [FDdt] = " & "'" & Me.txtFDdt.Value & "'" & _
", [CVstat] = " & "'" & Me.txtCV.Value & "'" & ", [CVdt] = " & "'" & Me.txtCVdt.Value & "'" & ", [CVexpdt] = " & "'" & Me.txtCVexp.Value & "'" & _
", [Lstat] = " & "'" & Me.txtLic.Value & "'" & ", [State] = " & "'" & Me.txtst.Value & "'" & ",[LNum] = " & "'" & Me.txtLnum.Value & "'" & _
", [Lexpdt] = " & "'" & Me.txtLexp.Value & "'" & ", [Sign] = " & "'" & Me.txtSign.Value & "'" & _
", [Signdt] = " & "'" & Me.txtSigndt.Value & "'" & ", [Train] = " & "'" & Me.txtTrain.Value & "'" & ", [Traindt] = " & "'" & Me.txtTrdt.Value & "'" & _
", [EDC] = " & "'" & Me.txtEDC.Value & "'" & ", [EDCdt] = " & "'" & Me.txtEDCdt.Value & "'" & ", [PFT] = " & "'" & Me.txtPFT.Value & "'" & _
", [PFTdt] = " & "'" & Me.txtPFTdt.Value & "'" & " WHERE TMFStaffQual.TMFStaffQualID = " & Me.lstContacts.Value
db.Execute SQLStrU
'TMFStaffQual.TMFStaffQualID = " & Me.lstContacts.Value & " AND TMFStaffQual.SiteId = " & Me.Parent!txtNewSiteId.Value
MsgBox ("Record is updated")
End If
db.Close
cmd_Save_Click_Exit:
Exit Sub
cmd_Save_Click_Err:
MsgBox Error$
Resume cmd_Save_Click_Exit
End Sub
Please help:banghead: