|
Subject:
|
NotInList event troubles
|
|
Posted By:
|
DaDeViL
|
Post Date:
|
8/11/2005 10:41:59 AM
|
Hello, I can't seem to get my NotInList event working. If I enter a value that doesnt exist in a table:
- it asks me if I would like to add the new value,
- I click YES,
- and then get the message that the new data has been added
- I click OK
- then I see the combo box is expanded ( the values in the combo box still doesnt have the new value)
- I try clicking on another control
- prompts me again asking if I want to enter the new value(starting the loop all over again)
To get out of the loop I am forced to select a value that is on the list and move on.
When I check my table to see if it has been updated, the new value exists, the combo box is just not requeried or updated like it is supposed to be.
Here is the code:
Private Sub Route_NotInList(NewData As String, Response As Integer)
Response = Access.acDataErrContinue
If MsgBox("The route " & Chr(34) & NewData & Chr(34) & " is not in the list of available routes. " & Chr(10) & _
"Would you like to enter a new route?", vbYesNo + vbQuestion, "Insert Route?") = vbYes Then
'The user clicked yes, so add the route
Dim strInsertRoute, strRouteNumberText As String
Dim ResultQuery As Variant
Dim intRouteNumberID As Integer
strInsertRoute = "INSERT INTO ROUTE_NUMBER(ROUTE_NUMBER) VALUES('" & NewData & "');"
ResultQuery = "SELECT @@Identity FROM ROUTE_NUMBER"
' call sub to run the queries
' Create Recordset --------------------------------
Dim rlRecordSet As New ADODB.Recordset '|
' -------------------------------------------------
Call GetRecords(ResultQuery, rlRecordSet, strInsertRoute)
' Close Recordset --------------------------------
rlRecordSet.Close '|
Set rlRecordSet = Nothing '|
' ------------------------------------------------
MsgBox "Route " & Chr(34) & NewData & Chr(34) & " has been added to the list.", vbOKOnly + vbExclamation _
, "Route Added!"
Response = Access.acDataErrAdded
Else
'The user clicked no, so undo what they entered
Response = Access.acDataErrContinue
End If
If Err Then
MsgBox "An error occurred. Please try again."
Response = Access.acDataErrContinue
Else
Response = Access.acDataErrAdded
Response = Access.acDataErrContinue
End If
End Sub
Thanks!
ŠaŠeVi£
|
|
Reply By:
|
sdilucca
|
Reply Date:
|
8/11/2005 10:45:38 AM
|
you should requery your controls if you change the data behind them. are you doing that?
Julio Cesar
-"Bueller.......... Bueller.......... Bueller.......... Bueller.........."
|
|
Reply By:
|
DaDeViL
|
Reply Date:
|
8/11/2005 10:54:13 AM
|
Yes, i tried a requery in the AfterUpdate, but it did not work..any idea what event I should put a requery in?
Thanks
ŠaŠeVi£
|
|
Reply By:
|
sdilucca
|
Reply Date:
|
8/11/2005 12:32:17 PM
|
show me your GetRecords code
Julio Cesar
-"Bueller.......... Bueller.......... Bueller.......... Bueller.........."
|
|
Reply By:
|
DaDeViL
|
Reply Date:
|
8/11/2005 1:18:21 PM
|
Here is GetRecords:
Public Function GetRecords(ByVal strSQL As String, ByRef rstRecordSet As ADODB.Recordset, Optional ByRef actionSQL As Variant = "") As Boolean
If (actionSQL <> "") Then
Dim cmdCommand As New ADODB.Command
End If
Dim rstCommand As New ADODB.Command
If (actionSQL <> "") Then
MainConnection.Execute actionSQL, dbFailOnError
With cmdCommand
.ActiveConnection = MainConnection
.CommandText = actionSQL
.CommandType = adCmdText
End With
End If
'Defines our command object
With rstCommand
.ActiveConnection = MainConnection
.CommandText = strSQL
.CommandType = adCmdText
End With
With rstRecordSet
.CursorType = adOpenStatic
.CursorLocation = adUseClient
.LockType = adLockOptimistic
.Open rstCommand
End With
If (actionSQL <> "") Then
Set cmdCommand = Nothing
End If
Set rstCommand = Nothing
End Function
The ADODB connection: MainConnection is declared as a global variable, and a separate function is used to open a new connection when the form is opened.
Thanks!!
ŠaŠeVi£
|
|
Reply By:
|
sdilucca
|
Reply Date:
|
8/11/2005 1:42:13 PM
|
i know this is kind of getting of the subject, but i guess i have the right to learn too :-) .... what is the purposed of cmdCommand? I never actually see you use it? still working on ur stuff
Julio Cesar
-"Bueller.......... Bueller.......... Bueller.......... Bueller.........."
|
|
Reply By:
|
DaDeViL
|
Reply Date:
|
8/11/2005 1:54:39 PM
|
ADO is really really confusing to me for some reason lol, I dont really know why cmdCommand is in there because the Execute command only uses actionSQL. GetRecords is actually a combination of two different functions i put together, so when i wrote it i assued cmdCommand was needed, but by the looks of it, seems its just a hood ornament for the function.
I was so frustrated with ADO that after I got the function finally working I was happy with NEVER looking at it again! lol
ŠaŠeVi£
|
|
Reply By:
|
sdilucca
|
Reply Date:
|
8/11/2005 2:03:11 PM
|
i don't see what the purpose of rlRecordSet is. I figured you were going to make it the RowSource for your combobox. But I don't see you doing that. Does anything actually change when you run this code?
Julio Cesar
-"Bueller.......... Bueller.......... Bueller.......... Bueller.........."
|
|
Reply By:
|
DaDeViL
|
Reply Date:
|
8/11/2005 2:12:14 PM
|
Well, I use GetRecords to insert data into the database, but the RecordSet parameter to the function is not optional, so I HAVE to create a new RecordSet(rlRecordSet) to throw to the function. But this is a convenient way of doing it. Suppose I need the ID of the record I just entered into the database using strInsertRoute (and for a NotInList event, I assumed I needed it), then all i have to do is set
ResultQuery = "SELECT @@Identity FROM ROUTE_NUMBER" (as the function already does)
So now if I type
MsgBox rlRecordSet.Fields(0)
right after I call GetRecords function, it will prompt the ID of the record I just inserted, and this code actually works...I used it to test and make sure that data is being entered into the database table when I enter new data into the combo box.
This way, if I cant get my NotInList event working properly, I have an alternate way to make sure the typed route ID is recorded with the rest of the form info.
RowSource for Combo box is this:
SELECT DISTINCT ROUTE_NUMBER.ROUTE_NUMBER, ROUTE_NUMBER.PK_ROUTE_NUMBER FROM ROUTE_NUMBER WHERE ROUTE_NUMBER.ROUTE_NUMBER IS NOT NULL ORDER BY ROUTE_NUMBER.ROUTE_NUMBER;
and bound column is 1.
ŠaŠeVi£
|
|
Reply By:
|
sdilucca
|
Reply Date:
|
8/11/2005 2:39:26 PM
|
have you tried to requery on Before Update?
Julio Cesar
-"Bueller.......... Bueller.......... Bueller.......... Bueller.........."
|
|
Reply By:
|
sdilucca
|
Reply Date:
|
8/11/2005 2:44:18 PM
|
actually have you tried to requery right at the end of your code under the NotInList event?
Julio Cesar
-"Bueller.......... Bueller.......... Bueller.......... Bueller.........."
|
|
Reply By:
|
DaDeViL
|
Reply Date:
|
8/11/2005 2:54:31 PM
|
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: 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£
|
|
Reply By:
|
DaDeViL
|
Reply Date:
|
8/15/2005 10:37:47 AM
|
Ok, I figured it out I think!! I am sure this would be useful to others in the future.
I used the code below:
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/forums/showthread.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:
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£
|