Connecting Tech Pros Worldwide Help | Site Map

apped2table function for adding an item not already in the list.

Newbie
 
Join Date: Jul 2009
Location: IN
Posts: 18
#1: 3 Weeks Ago
I am using this "append2table" function from Allen Browne to add an item to the combobox that is not already in the list.
Here is the function, with my fields and table names
Expand|Select|Wrap|Line Numbers
  1. Function Append2Table(cbo As ComboBox, NewData As Variant) As Integer
  2. On Error GoTo Err_Append2Table
  3. ' Purpose:   Append NotInList value to combo's recordset.
  4. ' Assumes:   ControlSource of combo has the same name as the foreign key field.
  5. ' Return:    acDataErrAdded if added, else acDataErrContinue
  6. ' Usage:     Add this line to the combo's NotInList event procedure:
  7. '                Response = Append2Table(Me.MyCombo, NewData)
  8.     Dim rst As DAO.Recordset
  9.     Dim sMsg As String
  10.     Dim ProjectID As Variant      ' Name of the field to append to.
  11.  
  12.     Append2Table = acDataErrContinue
  13.     ProjectID = cbo.ControlSource
  14.     If Not (IsNull(ProjectID) Or IsNull(NewData)) Then
  15.         sMsg = "Do you wish to add the entry " & NewData & " for " & cbo.Name & "?"
  16.         If MsgBox(sMsg, vbOKCancel + vbQuestion, "Add new value?") = vbOK Then
  17.             Set rst = CurrentDb.OpenRecordset("qry_SubmissionActive", dbOpenDynaset, dbSeeChanges)
  18.             rst.AddNew
  19.                           rst(ProjectDescription) = NewData
  20.             rst.Update
  21.             rst.Close
  22.             Append2Table = acDataErrAdded
  23.         End If
  24.     End If
  25.  
  26. Exit_Append2Table:
  27.     Set rst = Nothing
  28.     Exit Function
  29.  
  30. Err_Append2Table:
  31.     MsgBox "Error " & Err.Number & ": " & Err.Description, vbInformation, "Append2Table()"
  32.     Resume Exit_Append2Table
  33. End Function
  34.  
ProjectID is an Autonumber and ProjectDescription is a string.
I want to add an item to the field ProjectDescription.
Error: Item not found in this collection.
Any ideas?
best answer - posted by topher23
NeoPa makes a good point. Could this be accomplished using code along these lines?

Expand|Select|Wrap|Line Numbers
  1. DOCmd.RunSQL "INSERT INTO tblProject (ProjectDescription) VALUES ('" & NewData & "')"
  2. Me!cboProjects.Requery
  3.  
Sorry for the misleading info about ADO. I was under the impression that whenever you used a DBMS server, ADO had to be used instead of DAO. My bad.
Expert
 
Join Date: Jul 2009
Location: KY
Posts: 244
#2: 3 Weeks Ago

re: apped2table function for adding an item not already in the list.


I am not sure, but don't you need to wrap double quotes around it.

Like:
Expand|Select|Wrap|Line Numbers
  1. rst("ProjectDescription") = NewData
Let us know,
-AJ
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,212
#3: 3 Weeks Ago

re: apped2table function for adding an item not already in the list.


Personally, if you don't care whether or not this code is reusable, you can reduce the code and make it more readable. Assuming a Table Name of tblProject, and a Combo Box named cboProjects, which consists of a Single Bound Column whose Control Source is [ProjectDescription], then the code would be:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboProjects_NotInList(NewData As String, Response As Integer)
  2. Dim rst As DAO.Recordset
  3. Dim sMsg As String
  4.  
  5. Response = acDataErrContinue
  6.  
  7. If Not IsNull(NewData) Then
  8.   sMsg = "Do you wish to add the entry " & NewData & " for " & Me![cboProjects].Name & "?"
  9.     If MsgBox(sMsg, vbOKCancel + vbQuestion, "Add new value?") = vbOK Then
  10.       Set rst = CurrentDb.OpenRecordset("tblProject", dbOpenDynaset)
  11.         rst.AddNew
  12.           rst!ProjectDescription = NewData
  13.         rst.Update
  14.           rst.Close: Set rst = Nothing
  15.           Response = acDataErrAdded
  16.     End If
  17. End If
  18. End Sub
NOTE: The Limit To List Property of cboProjects must be set to Yes.
Newbie
 
Join Date: Jul 2009
Location: IN
Posts: 18
#4: 3 Weeks Ago

re: apped2table function for adding an item not already in the list.


ajalwaysus,

I get an error saying "Field cannot be updated" if I make change you suggested.

Adezil,

Tried the limited code, once mre. the error is now "ODBC call failed"
Note:comboboxname: cboProjectID, The records are to be inserted into "qry_SubmissionActive" .

still looking for the answer.
Expert
 
Join Date: Oct 2008
Location: Cedar City, Utah, USA
Posts: 86
#5: 3 Weeks Ago

re: apped2table function for adding an item not already in the list.


I'd check qry_SubmissionActive to make sure it's actually updatable. Open it in datasheet view, and if you can't add anything there, add your new data to the underlying table rather than the query.
Expert
 
Join Date: Jul 2009
Location: KY
Posts: 244
#6: 3 Weeks Ago

re: apped2table function for adding an item not already in the list.


The error you are getting using my code is most likely because your query is not "updateable". An easy way to test that is to go into your query and try to manually add a record. Let me know f this is the issue,and if it is, then you need a better way of getting your data.

EDIT:
Quote:

Originally Posted by topher23 View Post

I'd check qry_SubmissionActive to make sure it's actually updatable. Open it in datasheet view, and if you can't add anything there, add your new data to the underlying table rather than the query.

Ditto =)

-AJ
Newbie
 
Join Date: Jul 2009
Location: IN
Posts: 18
#7: 3 Weeks Ago

re: apped2table function for adding an item not already in the list.


Completely missed out that part. You both are correct. The query is not Updateable. So I replace that query with one of underlying table (tbl_Submission) which has the field ProjectDescription. And now I am getting "ODBC call failed" error.
P.S: I am using linked tables in access. DB actually is sitting under SQL server 2005. May be this is the problem.
I run the
"Step into" and the error occurs at this step:
rst.update

What should be the solution??
Expert
 
Join Date: Oct 2008
Location: Cedar City, Utah, USA
Posts: 86
#8: 3 Weeks Ago

re: apped2table function for adding an item not already in the list.


Well, Beetle, I think you've found your problem. This code is using DAO to create and update the recordset, whereas you need to use ADO to connect properly to SQL server.

If you're going to be using a back-end other than Jet (.mdb) or 2007's Access Engine (.accdb) you're going to need to learn about ADO. Check out w3schools.com, they have a free online course on it. http://www.w3schools.com/ado/default.asp

I'd love to give you a quick and simple answer, but I know very little about ADO, as I have no access to a server for my applications (blasted corporate IT boneheads!).
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,212
#9: 3 Weeks Ago

re: apped2table function for adding an item not already in the list.


Equivalent ADO Code would be (substitute your Combo Box Name for cboProjects):
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboProjects_NotInList(NewData As String, Response As Integer)
  2. Dim rst As ADODB.Recordset
  3. Dim sMsg As String
  4.  
  5. Set rst = New ADODB.Recordset
  6.  
  7. Response = acDataErrContinue
  8.  
  9. If Not IsNull(NewData) Then
  10.   sMsg = "Do you wish to add the entry " & NewData & " for " & Me![cboProjects].Name & "?"
  11.     If MsgBox(sMsg, vbOKCancel + vbQuestion, "Add new value?") = vbOK Then
  12.       With rst
  13.         .Source = "tbl_Submission"
  14.         .ActiveConnection = CurrentProject.Connection
  15.         .CursorType = adOpenKeyset
  16.         .LockType = adLockOptimistic
  17.           .Open
  18.  
  19.         .AddNew
  20.           !ProjectDescription = NewData
  21.         .Update
  22.           rst.Close: Set rst = Nothing
  23.           Response = acDataErrAdded
  24.       End With
  25.     End If
  26. End If
  27. End Sub
  1. Does tbl_Submission have other, Required Fields, besides [ProjectDescription]?
  2. Are you positive the Bound, Displayed Column in your Combo is [ProjectDescription]?
Newbie
 
Join Date: Jul 2009
Location: IN
Posts: 18
#10: 3 Weeks Ago

re: apped2table function for adding an item not already in the list.


Expand|Select|Wrap|Line Numbers
  1. Private Sub cboProjectID_NotInList(NewData As String, Response As Integer)
  2. Dim conn As New ADODB.Connection
  3. Dim rst As ADODB.Recordset
  4. Dim sMsg As String
  5.  
  6. Set conn = CurrentProject.Connection
  7.  
  8. Response = acDataErrContinue
  9.  
  10. If Not IsNull(NewData) Then
  11.   sMsg = "Do you wish to add the entry " & NewData & " for " & Me![cboProjectID].Name & "?"
  12.     If MsgBox(sMsg, vbOKCancel + vbQuestion, "Add new value?") = vbOK Then
  13.     rst.Open "qry_SubmissionActive", conn, adOpenDynamic, adLockOptimistic
  14.  
  15.  Do While Not rst.EOF
  16.  
  17.         rst.Fields("ProjectDescription") = NewData
  18.         rst.Update
  19.         Response = acDataErrAdded
  20.  
  21.  Loop
  22.         rst.Close
  23. End If
  24. End If
  25. End Sub
  26.  
Ok I am trying to write code for Adodb. but getting runtime error 91: Object Variable not set.
any ideas?

adezil: may be we chriscrossed in replying let me try your code and I will let you know.
thanks for the help.
Newbie
 
Join Date: Jul 2009
Location: IN
Posts: 18
#11: 3 Weeks Ago

re: apped2table function for adding an item not already in the list.


To Adezil,
I replaced it with your suggested code but the same error at [.Update] saying
ODBC call failed.
Yes, another required field in Project ID, but that is an autonumber

the combobox has rowsource =
Expand|Select|Wrap|Line Numbers
  1. SELECT qry_SubmissionActive.ProjectID, qry_SubmissionActive.PropertyName & "  " & qry_SubmissionActive.ProjectDescription AS Property
  2. FROM qry_SubmissionActive
  3. ORDER BY qry_SubmissionActive.PropertyName, qry_SubmissionActive.ProjectDescription;
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,212
#12: 3 Weeks Ago

re: apped2table function for adding an item not already in the list.


Quote:

Originally Posted by AccessBeetle View Post

To Adezil,
I replaced it with your suggested code but the same error at [.Update] saying
ODBC call failed.
Yes, another required field in Project ID, but that is an autonumber

the combobox has rowsource =

in your Combo Box:
  1. What is your Bound Column?
  2. What is the Column Count?
  3. What are the Column Widths?
  4. If all else else fails,can you send me the Front End Database? I may be able to get a picture of what is going on.
Expert
 
Join Date: Jul 2008
Location: Maryland
Posts: 1,158
#13: 3 Weeks Ago

re: apped2table function for adding an item not already in the list.


What is the & " " & doing in the rowsource? Should that be a comma?
Edit: Nevermind, I see what it's doing now.
Expert
 
Join Date: Jul 2008
Location: Maryland
Posts: 1,158
#14: 3 Weeks Ago

re: apped2table function for adding an item not already in the list.


If you're still using the code in post number 10, you seem to have a
Expand|Select|Wrap|Line Numbers
  1. Do While Not rst.EOF
which doesn't really make sense.
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,672
#15: 3 Weeks Ago

re: apped2table function for adding an item not already in the list.


Quote:

Originally Posted by topher23 View Post

Well, Beetle, I think you've found your problem. This code is using DAO to create and update the recordset, whereas you need to use ADO to connect properly to SQL server.

I don't mean to contradict, but this isn't technically true.

If a SQL Server table or cursor is linked to, then using DAO to the linked table should work fine.

If you wish to use a recordset connecting directly to the SQL server (or even a Pass-Thru query) then you certainly could ONLY use ADODB.

Thank you anyway for posting Topher. It's really good to see you getting involved :)
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,672
#16: 3 Weeks Ago

re: apped2table function for adding an item not already in the list.


This thread does seem to be meandering somewhat & I'm not sure you've found your way yet.

It seems to me that once you have sorted out exactly what you want to add where, then adding it can be simply accomplished with some basic SQL, and ensuring it is included in the ComboBox can be done by calling ComboBox.Requery.

Does it need to be more complicated than this?
Newbie
 
Join Date: Jul 2009
Location: IN
Posts: 18
#17: 2 Weeks Ago

re: apped2table function for adding an item not already in the list.


Sorry everyone for replying late. but the problem is still there and it is the same. the error I am getting is ODBC call failed. at
Expand|Select|Wrap|Line Numbers
  1. .AddNew
  2.           !ProjectDescription = NewData
  3.         .Update <-- Error here
Ok here is the answers for NeoPa's (** Edit: Read ADezii's **) questions.
1)bound colum is 1
2)the Column count is 2
3)Column width: 0";4.0625"
4) I might not be able to send the database. Sorry for that.

ChipR,
I tried
Expand|Select|Wrap|Line Numbers
  1. Do While Not rst.EOF 
this but nothing still same error.

Thanks everyone for help.
Expert
 
Join Date: Jul 2008
Location: Maryland
Posts: 1,158
#18: 2 Weeks Ago

re: apped2table function for adding an item not already in the list.


My point was that it doesn't make sense that you have a Do While loop at all. What is the purpose of the loop? (not to mention that fact that it can never end)
Expert
 
Join Date: Oct 2008
Location: Cedar City, Utah, USA
Posts: 86
#19: 2 Weeks Ago

re: apped2table function for adding an item not already in the list.


NeoPa makes a good point. Could this be accomplished using code along these lines?

Expand|Select|Wrap|Line Numbers
  1. DOCmd.RunSQL "INSERT INTO tblProject (ProjectDescription) VALUES ('" & NewData & "')"
  2. Me!cboProjects.Requery
  3.  
Sorry for the misleading info about ADO. I was under the impression that whenever you used a DBMS server, ADO had to be used instead of DAO. My bad.
Newbie
 
Join Date: Jul 2009
Location: IN
Posts: 18
#20: 2 Weeks Ago

re: apped2table function for adding an item not already in the list.


Oh ok. I thought you want me to try it. I am using code in post # 9. ya that does not make any sense though so I intitally did not have it.
Thanks
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,672
#21: 2 Weeks Ago

re: apped2table function for adding an item not already in the list.


Quote:

Originally Posted by topher23 View Post

Sorry for the misleading info about ADO. I was under the impression that whenever you used a DBMS server, ADO had to be used instead of DAO. My bad.

No worries.

It's not far off. The one exception is when you use linked tables.

BTW. I doubt the OP is trying to be rude in ignoring so many posts. Many are simply too overwhelmed with the concepts that keeping focus on anything but the stuff that makes sense to them is difficult.

Most don't realise that a simple post explaining they're confused would bring a response of helpful explanations.
Newbie
 
Join Date: Jul 2009
Location: IN
Posts: 18
#22: 2 Weeks Ago

re: apped2table function for adding an item not already in the list.


If anyone is looking at this thread I am still looking for an answers.
Adezil, Here is the answers for the questions you asked in post #12
1)bound colum is 1
2)the Column count is 2
3)Column width: 0";4.0625"
4) I might not be able to send the database. Sorry for that.
Error: ODBC call Failed
Thanks.
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,672
#23: 2 Weeks Ago

re: apped2table function for adding an item not already in the list.


If you're still interested in a solution, then you should consider responding to all those who have already posted first.

Why would anyone go to the trouble of committing further time to your problem when you fail even to respond to their posts?
Newbie
 
Join Date: Jul 2009
Location: IN
Posts: 18
#24: 2 Weeks Ago

re: apped2table function for adding an item not already in the list.


NeoPa,

Good Point.

I am not ignoring their answers. I am trying to put them into a try. But by doing that if something pops up in my mind then I also try to do that to fix it. Afterall I am after this continuously for more than week.

Anyways,

I already found the answer. The problem was one of the Concatenated (ProjectDescription+PropertyName)field which did not allowed to enter the data (Meaning other fields of that query allowed to enter or update). So I created one subform, which pops up when somebody wants to enter a data that is not in the list. The user has to enter Project Description and Property name separately and hit submit. Them Me.Requery does the refreshing work to the combobox and the newly entered value is appeared in the combobox. It was not easy but that is the only way I could figure it out.

Thanks again everyone for help.
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,672
#25: 2 Weeks Ago

re: apped2table function for adding an item not already in the list.


I hear what you're saying, yet a simple post in response - even before you've fully tested it out - goes a long way to stop people feeling they're being ignored.

I'm pleased you found something in the end that works for you, but considering the similarity of your solution to what was suggested in post #16 (and repeated in post #19) it's clear that this could have been resolved much earlier with full interaction on your part. Your version has fleshed this out somewhat though, which is always good.
Reply