Somethings are more difficult with Macros. This is one of them. They don't
have the power to do what can be done with VBA. Here is how it would be done
in VBA. It will also do away with the error message you are getting. It
will also present the user with a message asking them if they want to add the
new entry or not. Since I don't know the names of your controls, fields, or
forms, you will have to substitue your actual names.
If MsgBox(NewData & " Is not in the list - Add " & NewData, vbQuestion + _
vbYesNo + vbDefaultButton2, "Not Found") = vbYes Then
'Change cboClientSearth to the name of your combo box
Me.cboClientSearch.Undo
Docmd.OpenForm "Name Of Your Form", , , , acFormAdd, acDialog
Response = acDataErrAdded
Me.Requery
With Me.RecordsetClone
'SearchFieldName is the name of the field in the form's recordset you used
to do the original combo box lookup
.FindFirst "[SearchFieldName] = " & NewData & "
If Not .NoMatch Then
Me.Bookmark = .Bookmark
End If
End With
Else
'Change cboClientSearth to the name of your combo box
Me.cboClientSearch.Undo
Response = acDataErrContinue
End If
This line of code:
.FindFirst "[SearchFieldName] = " & NewData & "
assumes the table field you are using is a numeric data type. If it is
text, userthis syntax:
.FindFirst "[SearchFieldName] = """ & NewData & """"
Now, how to use this.
Open your form in design mode.
Select the combo box.
Open the properties dialog.
Select the events tab.
Delete the reference to the macro.
Click on the small button to the right of the Not In List event's text box
Select Code Builder.
The VB Editor will open
Paste the code above between the lines:
Private Sub NameOfCombo_NotInList(NewData As String, Response As Integer)
<<< The code goes here >>>
End Sub
I expect you may have some problems or questions before you get this
working, but once you have it, it will work much better for you and you have
learned a new trick. Please feel free to post back with any questions you
have.
--
Dave Hargis, Microsoft Access MVP
toolman74 said:
Not exactly sure what "calling the Macro" means so I'll give you what I can
and hopefully you can get what you need. I created the Macro off of the
Product Name combo box under Properties, Events, NotInList. There is a
dropdown arrow with the Macro option. In that Macro form, I chose OpenForm
and selected Product Form, Data Mode: Add and Dialog mode. That portion works
- the Product Form opens upon a NotInList entry. I enter the new Product
in the Product form, exit it and then back on the original form get an error
that the text entered isn't an item in the list. I checked the code screen
but didn't see anything about this Macro to which to add the Requery
procedure you gave me. I really appreciate your help on this!!
Pamela
:
I didn't suggest any changes in your query.
By Dialog mode, I mean in your Macro that opens the form, select Dialog in
the Window Mode property.
As to the requery, that is done in VBA after you call the macro.
How are you calling the Macro?
--
Dave Hargis, Microsoft Access MVP
:
Thanks, but I'm a little lost on this suggestion. After reading your post I
created a new query based on the product table but when I went back to my
table to have the Lookup Wizard use values from the product qry vs. the
product table, I got an error that it can't be changed due to the
relationships it has. I then went and deleted the relationships from that
table in the Relationships page but I still got the same error. Now what?
Is that what it means to "requery?" Also, I know you wrote the code in, but
as I originally stated, I have no idea what that means or where it goes.
Also, what is dialog mode? Thanks for your continued help!!
:
You requery just the combo box.
You should open the product form in dialog mode so the code in the Not In
List event will pause until the form is closed.
Then the next line of code should be:
Me.MyCombo.Requery
--
Dave Hargis, Microsoft Access MVP
:
I have a combo box based off of a product table but there are regular times
that the list needs to be updated. I created a macro upon the NotInList
event that opens the product form on an add only basis but once the info is
added under products it doesn't show up in the combo box - even with a
refresh of the records. When I close out of the form and opened the product
table table, the new info is there and upon reopening my original form &
combo box the new info is there...but how I can get it to add it or update it
without having to close out and reopening? I've seen many things about
writing in code but that is way over the head of this newbie. Thanks, in
advance!!
Pamela