Home > database >  NotInList Error after adding an item through a pop-up form
NotInList Error after adding an item through a pop-up form

Time:08-19

I have a subform (frmAdicionarPersonagens) which has, among other commands, a combobox named cboPersonagem.

I can add new items to the combo through the NotInList event in the usual way by using an action query (INSERT INTO tblListaPersonagens… etc.).

Now, I want to do the same, but using another form (frmAddPersonagem), linked to table tblListaPersonagens, which is fired in modal mode (so the user is forced to close that form with the added item before returning to the subform frmAdicionarPersonagens and the combo cboPersonagem.

The point is, I want the form fired to check for typos and also to add some data (which I supressed here to simplify the problem).

When there are no typos, that is, when the word entered in the combo is not changed in the frmAddPersonagem, everything runs smoothly. However, if I edit the word (for whatever reason) when I close frmAddPersonagem, I get the usual error “the item is not in the list”. However, the “new” word is in fact in the list and I don’t know what is happening, or how can I get out of this problem.

My code for not in list event:

Private Sub cboPersonagem_NotInList(NewChar As String, Response As Integer)
'
'   Show a Yes/No message and get the result.
    Dim ButtonClicked As Integer
    ButtonClicked = MsgBox(Prompt:="Do you want to add """ & NewChar & """ as a new Character?", _
                    Buttons:=vbYesNo   vbQuestion, Title:="Character not in the list")
'
    If ButtonClicked = vbNo Then
'
        cboPersonagem.Undo

        Response = acDataErrContinue

    ElseIf ButtonClicked = vbYes Then
    
        ' Add in table "tblListaPersonagens" a new record with the new Character written in the form
'        DoCmd.SetWarnings False
'        DoCmd.RunSQL "INSERT INTO tblListaPersonagens(NomePersonagem) VALUES('" & NewChar & "')"
'        DoCmd.SetWarnings True
    
        DoCmd.OpenForm FormName:="frmAddPersonagem", WindowMode:=acDialog, OpenArgs:=NewChar

        Response = acDataErrAdded
            
        Me.cboPersonagem.Undo
        
        Me.cboPersonagem.Requery
        
        Me.cboPersonagem = DMax("IDPersonagem", "tblListaPersonagens")
'
    End If
'
End Sub

CodePudding user response:

Don't need both acDataErrAdded and Requery.

If you want to be able to edit the NewData passed to popup form, set the combobox to Null then populate it after new record edit is saved. I would be hesitant to use DMax() in a database with multiple simultaneous users. An Alternative is code behind the popup to:

  1. directly set value of combobox (see example below) or
  2. set a global or TempVars variable then the calling form uses that to set combobox value
Private Sub Combo29_NotInList(NewData As String, Response As Integer)
Dim sMsg As String
    'initialize response to error
    Response = acDataErrContinue
   
    'Ask if user wants to add a new item
    sMsg = "'" & NewData & "' is not in the current list. " _
        & vbCrLf & vbCrLf _
        & "Do you want to add it? "
    Me.Combo29 = Null    
    If MsgBox(sMsg, vbYesNo, "Add New Data") = vbYes Then
        DoCmd.OpenForm "Holidays", , , , acFormAdd, acDialog, NewData
        Response = acDataErrAdded
    End If
    Me.Combo29.SetFocus
End Sub
Private Sub Form_Load()
If Not IsNull(Me.OpenArgs) Then Me.HolName = Me.OpenArgs
End Sub
_______________________________________________________
Private Sub Form_Close()
If Not IsNull(Me.OpenArgs) Then Forms!Misc.Combo29 = Me.HolID
End Sub
  • Related