Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 11th, 2005, 02:44 PM
Authorized User
 
Join Date: Jun 2004
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default

actually have you tried to requery right at the end of your code under the NotInList event?

Julio Cesar

-"Bueller.......... Bueller.......... Bueller.......... Bueller.........."
 
Old August 11th, 2005, 02:54 PM
Authorized User
 
Join Date: Aug 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just tried it, and it actually shows the default warning message AND my custom message too.

I dont think requery is required at all because in a NotInList event is supposed to do the requery automatically:

Quote:
quote:Setting Response = acDataErrAdded suppresses the default Access message, and allows you to add the new value to the combo's list in the event procedure. After you add the value to the list by one of the methods shown below, Access re-queries the combo and re-checks the value against the existing list contents. If the value still isn't in the list, Access displays the default message.
as mentioned in here: http://www.pacificdb.com.au/MVP/Code/NIL.htm


My code does everything that site instructs me to do (at least as far as I can see) and it still doesnt work!!!


Thanks

ÐaÐeVi£
 
Old August 15th, 2005, 10:37 AM
Authorized User
 
Join Date: Aug 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, I figured it out I think!!
I am sure this would be useful to others in the future.

I used the code below:


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
Code from: http://www.access-programmers.co.uk/...ad.php?t=61414

What this code does different from the code I wrote is that it declares the ADODB connection that I need (cs) INSIDE the NotInList event along with the recordset I will use (rs). Then what does the trick is this:
Code:
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


What this does is open the actual table (MyTable) in the database, adds the NewData to the field MyField, Updates the table and closes the recordset connection.
In my code, what I did was use an INSERT query to input data in. This I assume does not work because vba cannot tell if the data went in or not since I am using the GetRecords function...which also cannot tell VBA that the data went in, and so Access assumes the worst and asks to insert new data repeatedly.

Someone please correct me if I am wrong, but I believe that was the problem in my code. Hope this helps someone.

Thank You

ÐaÐeVi£





Similar Threads
Thread Thread Starter Forum Replies Last Post
NotInList Dungunny BOOK: Access 2003 VBA Programmer's Reference 0 March 29th, 2007 01:58 PM
Database troubles notrosh ASP.NET 2.0 Professional 0 June 13th, 2006 01:20 PM
CurrentRegion troubles Glen Frank Excel VBA 2 July 20th, 2005 07:04 AM
NotInList for DAPs JpJoe Access 0 July 5th, 2005 06:41 AM
PHP Troubles[:D] peter_budo BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 September 3rd, 2004 04:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.