Robin.Fepsy
New member
- Local time
- Today, 03:44
- Joined
- Jan 27, 2015
- Messages
- 7
Dear Community
I'm not really familiar with VBA or SQL codes. Thats why I need your help!
I'm trying to write some Code with the target to take a value from a textbox (out of a form) and a pregiven value (in this case = 2) and enter them in a new record in a table. But only if there is not already a record with the exact same combination of these to values. When done, the same button should proceed you to the next form (but this code is not already implemented in the fragment below) even if no values are added because they already exist in this combination. I have no idea why my code doesn't works:
As you can see, I tried to use a function to refer to the value out of the form, because I didn't find out how to refer to a form in an SQL Code.
Just to give you a better overview: The table where the information should be added is named "tbl_PatientStudiesGroupTZP". The form where you find the Textbox "ID_PatientStudiesGroup" (with the value I need to transport) is named "frm_PatientStudiesGroupTZP". I'm sorry for the long code, but I was not able to find one part out of it, which could be the problem. Every part (except the WHERE NOT EXISTS part) worked perfectly for itself, but not when thrown together. I would be very thankful for every kind of help!
Greetings, Robin
I'm not really familiar with VBA or SQL codes. Thats why I need your help!
I'm trying to write some Code with the target to take a value from a textbox (out of a form) and a pregiven value (in this case = 2) and enter them in a new record in a table. But only if there is not already a record with the exact same combination of these to values. When done, the same button should proceed you to the next form (but this code is not already implemented in the fragment below) even if no values are added because they already exist in this combination. I have no idea why my code doesn't works:
Code:
Public Function GetID_PatientStudiesGroup&()
GetID_PatientStudiesGroup = [Forms]![frm_PatientStudiesGroupTZP]![ID_PatientStudiesGroup]
End Function
Private Sub BPRS_T1_Button_Click()
Call GetID_PatientStudiesGroup&
Dim strSQL As String
strSQL = "INSERT INTO tbl_PatientStudiesGroupTZP (ID_PatientStudiesGroup,ID_TZP) SELECT [GetID_PatientStudiesGroup], 2 FROM tbl_PatientStudiesGroupTZP WHERE NOT EXISTS (SELECT ID_PatientStudiesGroup, ID_TZP FROM tbl_PatientStudiesGroupTZP WHERE ID_PatientStudiesGroup = [GetIDPatientStudiesGroup] AND ID_TZP = 2)"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
End Sub
As you can see, I tried to use a function to refer to the value out of the form, because I didn't find out how to refer to a form in an SQL Code.
Just to give you a better overview: The table where the information should be added is named "tbl_PatientStudiesGroupTZP". The form where you find the Textbox "ID_PatientStudiesGroup" (with the value I need to transport) is named "frm_PatientStudiesGroupTZP". I'm sorry for the long code, but I was not able to find one part out of it, which could be the problem. Every part (except the WHERE NOT EXISTS part) worked perfectly for itself, but not when thrown together. I would be very thankful for every kind of help!
Greetings, Robin