Hello colleagues,
I'm struggling with Combo Box and Not In List event. I want to be able to "add new record of First Name and Last Name if it is not in the list.
Here is the picture of how my form looks:
I have two tables: "Patients" and "Visit" - these are related as one to many(one patient can have many visit dates)
I also have Two forms - "Main" and "Patient_ex" .
So far my code for the Not In List event is:
As you can see in the picture, the code half works.
It doesn't save the new record with values Last Name and First Name.
Moreover I do not want to open a new form in order to add the new record details, but instead I would like to be able to add the new record details in the same form as the combo box(as you see in the picture).
my goal is:
1. When I enter new Name, Access detects that it is not in the list (Quite working)
2. If the name is NOT in the list, then the Values I entered (Last and first name) goes to the Text Field in the Patient_ex form so that I would no need to enter them again. Then I could update the new entered record and save it.(saving buttons works).
Guys, please, help me! I really struggling with VB...
Also, can I achieve same thing using only macros? (Access 2010)
Thank you!
I'm struggling with Combo Box and Not In List event. I want to be able to "add new record of First Name and Last Name if it is not in the list.
Here is the picture of how my form looks:
I have two tables: "Patients" and "Visit" - these are related as one to many(one patient can have many visit dates)
I also have Two forms - "Main" and "Patient_ex" .
So far my code for the Not In List event is:
Code:
Private Sub Combo19_NotInList(NewData As String, Response As Integer)
Dim Db As DAO.Database
Dim Rs As DAO.Recordset
Dim Msg As String
Dim NewID As String
On Error GoTo Err_combo19_NotInList
' Exit this subroutine if the combo box was cleared.
If NewData = "" Then Exit Sub
' Confirm that the user wants to add the new customer.
Msg = "'" & NewData & "' is not in the list." & vbCr & vbCr
Msg = Msg & "Do you want to add it?"
If MsgBox(Msg, vbQuestion + vbYesNo) = vbNo Then
' If the user chose not to add a customer, set the Response
' argument to suppress an error message and undo changes.
Response = acDataErrContinue
' Display a customized message.
MsgBox "Please try again."
Else
' If the user chose to add a new customer, open a recordset
' using the table.
Set Db = CurrentDb
Set Rs = Db.OpenRecordset("Patients", dbOpenDynaset)
' Create a new record.
Rs.AddNew
' Save the record.
Rs.Update
' Set Response argument to indicate that new data is being added.
Response = acDataErrAdded
End If
Exit_combo19_NotInList:
Exit Sub
Err_combo19_NotInList:
' An unexpected error occurred, display the normal error message.
MsgBox Err.Description
' Set the Response argument to suppress an error message and undo
' changes.
Response = acDataErrContinue
End Sub
As you can see in the picture, the code half works.
It doesn't save the new record with values Last Name and First Name.
Moreover I do not want to open a new form in order to add the new record details, but instead I would like to be able to add the new record details in the same form as the combo box(as you see in the picture).
my goal is:
1. When I enter new Name, Access detects that it is not in the list (Quite working)
2. If the name is NOT in the list, then the Values I entered (Last and first name) goes to the Text Field in the Patient_ex form so that I would no need to enter them again. Then I could update the new entered record and save it.(saving buttons works).
Guys, please, help me! I really struggling with VB...
Also, can I achieve same thing using only macros? (Access 2010)
Thank you!