New category Problem

Mr_Adams

Active Member
Joined
Oct 7, 2002
Messages
475
I have a combobox on a form. it gets it data from Table "Departments"
I want the user to be able to enter a new department in the ComboBox and have it open the Department Maintenance Form so that they can add a department to the list.
Right now the code I have wants to open the Department Maintenance Form even if they choose a pre-existing Department.

I would aslo like the portion of the code in RED to display what was actually entered in the ComboBox

Can someone gelp me please

Here is the code I have

Private Sub DEPARTMENT_BeforeUpdate(Response As Integer)
' Add a new category by typing a name in lstCompany combo box.

Dim intNewDepartment As Integer, strTitle As String, intMsgDialog As Integer
Dim strMsg As String

' Display message box asking if user wants to add a new category.
strTitle = "New Department"
strMsg = "Data Entered in ComboBox is not in the Department list. "
strMsg = strMsg & "Would you like to add it?"
intMsgDialog = vbYesNo + vbQuestion + vbDefaultButton1
intNewDepartment = MsgBox(strMsg, intMsgDialog, strTitle)
If intNewDepartment = vbNo Then
Response = acDataErrDisplay
Else
'Open the Companies form
DoCmd.OpenForm "Department Maintenance", acNormal, , , acFormAdd, acDialog
End If

End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
OK My bone head move
I should have use NotInList instead of BeforeUpdate
And in properties "Limit to List" change to YES
and inproperties "On Not in List" use this event procedure

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

        ' Add a new category by typing a name in DEPARTMENT combo box.
    Dim intNewDepartment As Integer, strTitle As String, intMsgDialog As Integer
    Dim strMsg As String, rst As Recordset, Db As Database, newRst As Recordset
    Dim DepartmentID As Long
        ' Display message box asking if user wants to add a new category.
    strTitle = "New Department"
    strMsg = "'" & NewData & "' is not in the Department list.  "
    strMsg = strMsg & "Would you like to add it?"
    intMsgDialog = vbYesNo + vbQuestion + vbDefaultButton1
    intNewDepartment = MsgBox(strMsg, intMsgDialog, strTitle)
        If intNewDepartment = vbNo Then
            Response = acDataErrDisplay
                Else
                    'Open the Department Maintenance form
                DoCmd.OpenForm "Department Maintenance", acNormal, , , acFormAdd, acDialog
                End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,692
Messages
6,161,351
Members
451,697
Latest member
pedroDH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top