Solved

Combo box Not in List

Posted on 2014-04-15
7
410 Views
Last Modified: 2014-04-15
I have combo box based on field from table.  I want to add new item that is not In my combo box. In combo box cboProductType on Not in List event I have this code:

On Error GoTo ErrorH
   
      Dim strTmp As String
      Dim dbs As DAO.Database
   
    'Get confirmation that this is not just a spelling error.
    strTmp = "Add '" & NewData & "' as a new Expense Type?"
    If MsgBox(strTmp, vbYesNo + vbDefaultButton2 + vbQuestion, "Not in list") = vbYes Then
   
        'Append the NewData as a record in the tblProduct table.
        strTmp = "INSERT INTO tblProduct ( ProductTypeDescription ) " & _
            "SELECT """ & NewData & """ AS ProductTypeDesription;"        
                dbs.Execute strTmp, dbFailOnError        
        'Notify Access about the new record, so it requeries the combo.
        Response = acDataErrAdded
    Else
        Response = acDataErrDisplay
    End If
   
ErrorH:
     MsgBox "When trying to process this new record add, error happened" & _
           vbCrLf & "Please contact the program designer and " & _
           "report the error as follows" & vbCrLf & _
           "Error #:     " & Err.Number & vbCrLf & _
           "Description: " & Err.Description
    Resume Next


When I enter new item I got message from error handler Error#91
Description: Object variable or With block variable not set

Any Idea?
0
Comment
Question by:Taras
  • 4
  • 3
7 Comments
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
You haven't included a SET statement for your dbs Object.  try this:


On Error GoTo ErrorH
    
      Dim strTmp As String
      Dim dbs As DAO.Database
       Set dbs = CurrentDB   '<<<<<---- add this line

Open in new window

0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
Comment Utility
You'll also need an Exit statement before your error handler to make the code exit normally if there are no errors:


On Error GoTo ErrorH
    
      Dim strTmp As String
      Dim dbs As DAO.Database
      Set dbs = CurrentDB   '<<<<<---- add this line

    'Get confirmation that this is not just a spelling error.
    strTmp = "Add '" & NewData & "' as a new Expense Type?"
    If MsgBox(strTmp, vbYesNo + vbDefaultButton2 + vbQuestion, "Not in list") = vbYes Then
    
        'Append the NewData as a record in the tblProduct table.
        strTmp = "INSERT INTO tblProduct ( ProductTypeDescription ) " & _
            "SELECT """ & NewData & """ AS ProductTypeDesription;"         
                dbs.Execute strTmp, dbFailOnError        
        'Notify Access about the new record, so it requeries the combo.
        Response = acDataErrAdded
    Else
        Response = acDataErrDisplay
    End If
    Exit Sub  ' (or function) <<<<----- add this
 
ErrorH:
     MsgBox "When trying to process this new record add, error happened" & _
           vbCrLf & "Please contact the program designer and " & _
           "report the error as follows" & vbCrLf & _
           "Error #:     " & Err.Number & vbCrLf & _
           "Description: " & Err.Description
    Resume Next

Open in new window

0
 

Author Comment

by:Taras
Comment Utility
mbizup I applied that and got:

Microsoft Access
? The text you entered ins’t an Item in the list
Select an item from the list, or enter text that matches one of the listed itmes.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:Taras
Comment Utility
I can see that new  value is seating in table field, should I do some kind requery ?
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
<<Response = acDataErrAdded >>

This line should automatically force the requery.


Did you try the full code I posted at ID http:#a40001608  ?
0
 

Author Closing Comment

by:Taras
Comment Utility
Thanks a lot.
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
What is the Row Source property of your combo box?
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now