NotInList playing up (1 Viewer)

nashaz

Member
Local time
Today, 10:09
Joined
Mar 24, 2023
Messages
111
Greetings,

I have a basic table, CourseNameT which looks like this:

1711040581426.png


In the corresponding form, I have txtCourseID, cboCourseName, cboType (can only be either External or Internal), and txtValidFor. I am trying to use NotInList event so that user can add new courses if it doesn't already exists (the check and balance comes from BeforeUpdate even where if combo of CourseID and Type exists, user won't be allowed to proceed with new entry). Following is the simple code I have for this purpose:




Code:
Private Sub cboCourseName_NotInList(NewData As String, Response As Integer)

    Dim strTmp As String

    ' get confirmation
    strTmp = "'" & NewData & "' does not exist in the database." & vbCrLf & "Would you like to add '" & NewData & "' as a new course?"

    If MsgBox(strTmp, vbYesNo + vbDefaultButton2 + vbQuestion, "Add new course?") = vbYes Then

        ' Append the NewData as a record in the CourseNameT
        strTmp = "INSERT INTO CourseNameT ( CourseName ) " & _
            "SELECT """ & NewData & """ AS CourseName;"
        
        CurrentDb.Execute strTmp
        Me.cboCourseName.Requery
        Response = acDataErrAdded

    Else
        Me.Undo

    End If

End Sub

The issue I am having can be best shown via images, as below:

1711041373545.png


Upon pressing Yes, I get following:

1711041406334.png


As you can see, there is now an additional record, CourseID 78, with the same name as the one I entered i.e., CourseID 77. Any idea why this might be happening?

Thanks to all in advance.
 

cheekybuddha

AWF VIP
Local time
Today, 10:09
Joined
Jul 21, 2014
Messages
2,280
Why are you using a combo for CourseName?

Surely you don't want duplicate course names in your CourseNameT table?

When adding a new record, just type in the new course name directly.

Use the form BeforeUpdate event to check that it doesn't already exist in the table, and cancel the event if it does.
 
Last edited:

nashaz

Member
Local time
Today, 10:09
Joined
Mar 24, 2023
Messages
111
Why are you using a combo for CourseName?

Surely you don't want duplicate course names in your CourseNameT table?

When adding a new record, just type in the new course name directly.

Use the form BeforeUpdate event to check that it doesn't already exist in the table, and cancel the event if it does.
1711099208105.png


True... However, just for learning's sake, any idea why this may be happening?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:09
Joined
May 7, 2009
Messages
19,243
just maybe...
there are 2 columns in your combobox (and your combo is Bound to the first column, or the CourseID).
 

cheekybuddha

AWF VIP
Local time
Today, 10:09
Joined
Jul 21, 2014
Messages
2,280
any idea why this may be happening?
When you add the new course name using the INSERT statement you are adding a whole new record with empty values for Type and ValidFor.

This is in addition to the new record that you were already working on.

Normally you would not run in to this situation, since you would add a new course directly as suggested in Post #2.

You would use the NotInList event for a combo that is looking up a value from a related table, not a field in the same table.

Otherwise, if you really want to use a combo here (???), you can just set the combo LimitToList property to False, and completely remove the INSERT query from the NotInList event.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:09
Joined
May 7, 2009
Messages
19,243
Otherwise, if you really want to use a combo here (???), you can just set the combo LimitToList property to False, and completely remove the INSERT query from the NotInList event.
maybe need confirmation? since if you enter Garbage data, it will immediately save the Garbage.
 

cheekybuddha

AWF VIP
Local time
Today, 10:09
Joined
Jul 21, 2014
Messages
2,280
Yes, true.

Untested, but perhaps something like:
Code:
Private Sub cboCourseName_NotInList(NewData As String, Response As Integer)

    Dim strTmp As String

    ' get confirmation
    strTmp = "'" & NewData & "' does not exist in the database." & vbCrLf & "Would you like to add '" & NewData & "' as a new course?"

    If MsgBox(strTmp, vbYesNo + vbDefaultButton2 + vbQuestion, "Add new course?") = vbYes Then
        Response = acDataErrAdded
    Else
        Me.Undo
    End If

End Sub
 

Users who are viewing this thread

Top Bottom