Hi, all
I'm a relative newbie with Access, only about 4 months.
I'm having a problem with the Not_In_List event for a combo box. I need to be able to add new items here and it is not working.
The code I'm using is as follows:
The problem is that the new record is added, but after adding, the new field values displayed by the form are from the first record in the query. This is confusing to users.
The combo box refreshes properly, but I cannot display the new record without closing the form and reopening it. When I tried adding in code for this,
it puts in a loop, adding multiple records, and I get an error after the loop:
I'm a relative newbie with Access, only about 4 months.
I'm having a problem with the Not_In_List event for a combo box. I need to be able to add new items here and it is not working.
The code I'm using is as follows:
Code:
[SIZE=3][FONT=Calibri]Private Sub cboClause_NotInList(NewData As String, Response As Integer)
[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]Dim intAnswer As Integer[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]Dim strsql As String[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]Dim strP As String[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]Dim strQ As String[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]Dim strR As String[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]Dim strS As String[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]intAnswer = MsgBox("The Clause Code, " & Chr(34) & NewData & _[/FONT][/SIZE]
[FONT=Calibri][SIZE=3]Chr(34) & ", is not currently listed." & vbCrLf & _[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]"Would you like to add it to the list now?" _[/SIZE][/FONT]
[FONT=Calibri][SIZE=3], vbQuestion + vbYesNo, "Add Clause Code")[/SIZE][/FONT]
[SIZE=3][FONT=Calibri]If intAnswer = vbYes Then[/FONT][/SIZE]
[FONT=Calibri][SIZE=3]strP = NewData + " - P Not applicable to Planning"[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]strQ = NewData + " - Q Not applicable to Quality"[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]strR = NewData + " - R Not applicable to Contract Review"[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]strS = NewData + " - S Not applicable to Shipping + Receiving"[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]strsql = "INSERT INTO tblClause([ClauseCode],[ClauseP],[ClauseQ],[ClauseR],[ClauseS],[ClauseType]) " & _[/SIZE][/FONT]
[FONT=Calibri] "[/FONT][FONT=Calibri][SIZE=3]VALUES ('" & NewData & "','" & strP & "','" & strQ & "','" & strR & "','" & strS & "','" & strType & "');"[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]DoCmd.SetWarnings False[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]DoCmd.RunSQL strsql[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]DoCmd.SetWarnings True[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]Response = acDataErrAdded[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]MsgBox "The new Clause Code has been added to the list." _[/SIZE][/FONT]
[FONT=Calibri][SIZE=3], vbInformation, "Customer Added"[/SIZE][/FONT]
[SIZE=3][FONT=Calibri]Else[/FONT][/SIZE]
[FONT=Calibri] [SIZE=3]MsgBox "Please choose a Clause Code from the list." _[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] , vbInformation, "Continue?"[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]Response = acDataErrContinue[/SIZE][/FONT]
[SIZE=3][FONT=Calibri]End If[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]cboCustomer_NotInList_Exit:[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]Exit Sub[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]cboCustomer_NotInList_Err:[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]MsgBox Err.Description, vbCritical, "Error"[/FONT][/SIZE]
[FONT=Calibri][SIZE=3]Resume cboCustomer_NotInList_Exit[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]End Sub[/SIZE][/FONT]
The combo box refreshes properly, but I cannot display the new record without closing the form and reopening it. When I tried adding in code for this,
Code:
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm "frmClauseEntry", acNormal
Run-time error '2501':
The Close action was cancelled.
What am I doing wrong? The Close action was cancelled.
Using Access 2016
Form Record Source is a query
Recordset Type is Dynaset
Combo box is unbound
Row Source is a query
Row Source type is Table/Query
Thanks for any help you can provide.Form Record Source is a query
Recordset Type is Dynaset
Combo box is unbound
Row Source is a query
Row Source type is Table/Query