473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

111 100+
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?
Oct 30 '09 #1

✓ answered 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.

24 2140
ajalwaysus
266 Expert 100+
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
Oct 30 '09 #2
ADezii
8,834 Expert 8TB
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.
Oct 30 '09 #3
AccessBeetle
111 100+
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.
Oct 30 '09 #4
topher23
234 Expert 100+
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.
Oct 30 '09 #5
ajalwaysus
266 Expert 100+
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: @topher23
Ditto =)

-AJ
Oct 30 '09 #6
AccessBeetle
111 100+
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??
Oct 30 '09 #7
topher23
234 Expert 100+
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!).
Oct 30 '09 #8
ADezii
8,834 Expert 8TB
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]?
Oct 30 '09 #9
AccessBeetle
111 100+
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.
Oct 30 '09 #10
AccessBeetle
111 100+
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;
Oct 30 '09 #11
ADezii
8,834 Expert 8TB
@AccessBeetle
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.
Oct 30 '09 #12
ChipR
1,287 Expert 1GB
What is the & " " & doing in the rowsource? Should that be a comma?
Edit: Nevermind, I see what it's doing now.
Oct 30 '09 #13
ChipR
1,287 Expert 1GB
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.
Oct 30 '09 #14
NeoPa
32,556 Expert Mod 16PB
@topher23
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 :)
Oct 31 '09 #15
NeoPa
32,556 Expert Mod 16PB
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?
Oct 31 '09 #16
AccessBeetle
111 100+
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.
Nov 2 '09 #17
ChipR
1,287 Expert 1GB
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)
Nov 2 '09 #18
topher23
234 Expert 100+
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.
Nov 2 '09 #19
AccessBeetle
111 100+
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
Nov 2 '09 #20
NeoPa
32,556 Expert Mod 16PB
@topher23
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.
Nov 2 '09 #21
AccessBeetle
111 100+
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.
Nov 3 '09 #22
NeoPa
32,556 Expert Mod 16PB
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?
Nov 3 '09 #23
AccessBeetle
111 100+
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.
Nov 3 '09 #24
NeoPa
32,556 Expert Mod 16PB
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.
Nov 3 '09 #25

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ken Fine | last post by:
I have a menu system that has nodes that can be opened or closed. In an effort to make my code more manageable, I programmed a little widget tonight that keeps track of the open/active item and...
5
by: Nils Petter Vaskinn | last post by:
I'm using an enum that's declared within a function (since I only need it within that function.) I can't find anything about this in "The C++ Programming Language" by Stroustroup and I don't...
4
by: Ryan Ternier | last post by:
I have a section of my project that is Driving me nuts. No one has been able to help that much on it, so i thought of posting it here in hopes someone could help. I need to print out an...
26
by: Simon Jefferies | last post by:
Hello, I am trying to add an item to a checked list box, like: clbList.Items.add("Hello",true) I get an error back: Run-time exception thrown: System.ArgumentOutOfRangeException -...
0
by: Kevin Blount | last post by:
I need to create a page that lists a users Content, and allow them to add subscribers to or remove subscribers from that Content. The list may contain just one Content item, but it could just as...
4
by: hanseymoon | last post by:
Dear newsgroup: I've got this long function, which works good overall to spell check words from a dictionary and I am not in a position to replace it. Can someone please see where or how it...
4
by: Tony Lownds | last post by:
(Note: PEPs in the 3xxx number range are intended for Python 3000) PEP: 3107 Title: Function Annotations Version: $Revision: 53169 $ Last-Modified: $Date: 2006-12-27 20:59:16 -0800 (Wed, 27 Dec...
19
by: Adam | last post by:
Hi, I'd like to return an (arbitrary length) string array from a function so that after calling the array I've got a list of strings I can access. After much perusing of the internet I found a...
2
by: =?Utf-8?B?TWFobW91ZCBTaGFiYW4=?= | last post by:
i have a problem in adding new listbox items i don't need to allow adding multible items with the same textvalue ex: if current items are: green red
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.