Not In List - Adding New Data to a Combobox

Suzy

Registered User.
Local time
Today, 13:46
Joined
Jan 20, 2004
Messages
29
Forms not updating !!!!!!

As i don't know Access very well, I am having problems with the forms. I have updated the table with new values but those values are not reflected in the form ! Please help me. thanking you in advance.
 
You need to use code to Requery the form's RecordSource.

Are you updating the table from the form or are you doing the (more sinister) method of typing directly into the table?
 
Unfortunately, I am typing the new values directly into the table in design mode !!!!
 
Suzy said:
I am typing the new values directly into the table in design mode !!!!

[nitpick] That's impossible. You are typing the new values directly into the table in View mode. [/nitpick] ;)

Whyever on earth would you do this?
 
In the design mode, I am entering the values for the "lookup" column under field properties. I have a combo-box in the form, you see. I had to add more values for the look up but those are not showing on the drop-down list of my form. Can you help please?
 
Would you not rather know how to use the combobox's NotInList property to directly add new information into the table from the combobox should it not already be in the list?:)
 
don't quite foolow u there, sorry.:(
 
Rather than type into a table a new piece of information that is not in the table that your combobox looks up, you can make the combobox say "This information is not in the list. Would you like to add it?"

Would you not rather have that?
 
Are you using Access97, 2000, or 2002?
 
Set the combo's LimitToList property to Yes.

Copy the code below into the combo's NotInList event (Code Builder)...you don't need the first and last lines of what is written below.

Change the MyCombo in the line Me.MyCombo.Undo to the name of your combobox.

Code:
Private Sub MyCombo_NotInList(NewData As String, Response As Integer)
    
    On Error GoTo Err_ErrorHandler
    
    ' provide text constants to reduce text later and allow for faster execution
    ' due to added speed from the compilation of constants
    Const Message1 = "The data you have entered is not in the current selection."
    Const Message2 = "Would you like to add it?"
    Const Title = "Unknown entry..."
    Const NL = vbCrLf & vbCrLf
    
    ' conenction and recordset object variables
    Dim cn As Connection
    Dim rs As ADODB.Recordset
    
    ' show message box and evaluate if the user has selected Yes or No
    If MsgBox(Message1 & NL & Message2, vbQuestion + vbYesNo, Title) = vbYes Then
        ' open a connection to the connection object
        Set cn = CurrentProject.Connection
        ' initialise the recordset object
        Set rs = New ADODB.Recordset
        ' using the recordset object
        With rs
            .Open "MyTable", cn, adOpenStatic, adLockPessimistic ' open it
            .AddNew ' prepare to add a new record
            .Fields("MyField") = NewData ' add unfound data into field
            .Update ' update the table
            .Close ' close the recordset connection
        End With
        Response = acDataErrAdded ' confirm record added
    Else
        Me.MyCombo.Undo ' clear the entry in the combobox
        Response = acDataErrContinue ' confirm the record is not allowed
    End If
    
Exit_ErrorHandler:
    ' de-initialise our object variables
    Set rs = Nothing
    Set cn = Nothing
    Exit Sub
    
Err_ErrorHandler:
    ' display error message and error number
    MsgBox Err.Description, vbExclamation, "Error #" & Err.Number
    Resume Exit_ErrorHandler
    
End Sub
 
Last edited:
No probs! :cool:

Just posting the exact same code but written for users of Access97 (DAO)

Code:
Private Sub MyCombo_NotInList(NewData As String, Response As Integer)
    
    On Error GoTo Err_ErrorHandler
    
    ' provide text constants to reduce text later and allow for faster execution
    ' due to added speed from the compilation of constants
    Const Message1 = "The data you have entered is not in the current selection."
    Const Message2 = "Would you like to add it?"
    Const Title = "Unknown entry..."
    Const NL = vbCrLf & vbCrLf
    
    ' database and recordset object variables
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    
    ' show message box and evaluate if the user has selected Yes or No
    If MsgBox(Message1 & NL & Message2, vbQuestion + vbYesNo, Title) = vbYes Then
        ' open a connection to the current database
        Set db = CurrentDb
        Set rs = "MyTable"
        ' using the recordset object
        With rs
            .AddNew ' prepare to add a new record
            .Fields("MyField") = NewData ' add unfound data into field
            .Update ' update the table
            .Close ' close the recordset connection
        End With
        Response = acDataErrAdded ' confirm record added
    Else
        Me.MyCombo.Undo ' clear the entry in the combobox
        Response = acDataErrContinue ' confirm the record is not allowed
    End If
    
Exit_ErrorHandler:
    ' de-initialise our object variables
    Set rs = Nothing
    Set db = Nothing
    Exit Sub
    
Err_ErrorHandler:
    ' display error message and error number
    MsgBox Err.Description, vbExclamation, "Error #" & Err.Number
    Resume Exit_ErrorHandler
    
End Sub
 

Users who are viewing this thread

Back
Top Bottom