Hi - I am a novice to Access and VBa - so please bear with me...
I have a combo box that lists ID numbers. The user types in an ID - and if it exists - the form reveals the relevent information associated with that ID.
However, if the ID does not exist, then an error message is generated (Not in List) and one cannot proceed until a valid ID is selected.
Unfortunately this means that one cannot then create a new record (or add the new ID to the list).
Searching around I thought I had found a general way around it - as follows:
Private Sub Research_Id_NotInList(NewData As String, Response As Integer)
Dim Db As DAO.Database
Dim Rs As DAO.Recordset
Dim Msg As String
Msg = "'" & NewData & "' is not in the list." & vbCr & vbCr
Msg = Msg & "Do you want to add it?"
If MsgBox(Msg, vbQuestion + vbYesNo) = vbNo Then
Response = acDataErrContinue
MsgBox "Try again."
Else
Set Db = CurrentDb
Set Rs = Db.OpenRecordset("TABLE_1", dbOpenDynaset)
Rs.AddNew
Rs![ID] = NewData
Rs.Update
Response = acDataErrAdded
End If
End Sub
Unfortunately, although it works (sort of...), it creates anomalies in the database. This is because the ID combo box is in a form header while I have a subform in the body (listing the instances and dates of instances for that particular client). ..so when I click OK, it creates a new record (fine and dandy) ... but ALSO changes the ID of the client whose information was just being displayed to the ID I just typed... (oooops)
So, to prevent that, I have tried to write into the code
DoCmd.GoToRecord , , acLast
DoCmd.GoToRecord , , acNext
..to get to that "(new)" record and to make sure all the information fields are blank before it starts, but no matter where i put that - all that does is break it...
BUT, IF I can get to that (last) blank record - the one that Access labels as "(new)" - then I can make the NotInLIst code work (...almost...) - I say almost because if I manually go to that last "(new)" record and then type in an ID that is not in the list (and the code initiates and I click OK) - then I find it has created TWO new records, one as I typed the ID in - and one according to the VB code!
...If I could then delete one of those records (from within the code - ignoring the error messages so it just deletes), then all would be fine (well, except that in the original code I would still have to find a way to GET to that last "(new)" record...
...and find a way to prevent users from clicking YES originally...
..and ...oh my ...So I have been running incircles all day...
so there must be a better way - (I hope)... and therefore with hat in hand I come to the forum... any help please ?