Not in list

S

SG

I have a form called frmacq which has a sub for called
frmtbleacqdetailssubform on the subform I have a combo box where the user
types in an itemcode if the itemcode if the itemcode the user types in does
not exist I have setup a macro to display a message advising the user the
itemcode does not exist. BUT access continues to display a message staing
the items enter is not in the list and then also displays the following
message The Microsfot Engine cannot find in the table PRODUCTS with the key
matching fields itemcode. I would prefer these message not be displayed and
if the itemcode is not found then my macro error message be displayed and
the enter new product details form be diplayed where the user can then enter
the new itemcode, description and cost.

Any sugestions much appreciated!

Many Thanks

S
 
A

Albert D. Kallal

The typical code that you would use is:

Private Sub Distributee_NotInList(NewData As String, Response As Integer)

if MsgBox("Do you want to add this value to the list?", _
vbYesNo) = vbYes then
DoCmd.OpenForm "frmAddClient", , , , acFormAdd, acDialog, NewData
Response = acDataErrAdded
Else
Response = acDataErrContinue
End If

End Sub

Note that by setting response = acDataErrAdded, then ms-access does a
re-load, and a re-query of the comb box list for you. In other words, quite
a bit of stuff happens to ensue that the combo box is re-loaded, and re-set.

The above is actualy all you need...less then 10 lines of code.....

However, we can refine this futher....

, in the forms on-load event, I go:


if isnull(me.OpenArgs) = false then

me.CompanyName = me.Openargs

endif

It is important to note that while you are saving the id in the combo box
list, the newdata is in fact the actual text used, and if you change that
text, then you need to modify the newdata to MATCH what your user typed in
that form.

The solution is to simply grab what value the user entered into the form.
The
trick in doing this is to simply modify the above code as:

strF = "frmAddClient"

if MsgBox("Do you want to add this value to the list?", _
vbYesNo) then
DoCmd.OpenForm strF, , , , acFormAdd, acDialog, NewData

if isloaded(strF) = true then
Response = acDataErrAdded
NewData = forms(strF)!CompanyName
docmd.Close acForm,strF
else
Response = acDataErrContinue
endif

Else
Response = acDataErrContinue
End If

To see how you get a form to RETURN values, but wait for user input, simply
read the following article of mine:

http://www.members.shaw.ca/AlbertKallal/Dialog/Index.html


To make life a bit easier to your users,
there are a good number of things you should set in frmAddClent to make life
better.

They are:

Set the forms "cycle" property to current. This means the cursor when on the
bottom of the screen at the last field does not jump to another record. It
is horrible when a user bumps the tab key, and access goes to another
record, so, set the forms cycle property (other tab) to current. In fact,
this is a good setting for most forms, and NOT just this add example.

Since this is just a add form, then turn off the record navigation at the
bottom, again this serves only to confuse the user (since you have a
frmAddClient, I bet you already did this too). Why confuse the user!

Further, turn off the forms ability to "add new" records. Yes, you read this
100% correct. You want turn off the forms allow additions property. The
reason for this is that the above "open" form will OVER RIDE this setting.
This is ideal, since once again, hitting page/down key, or even the mouse
wheel will not cause the form to jump to another record, and confuse the
heck out of the user (this is along the same lines as the tab key setting
above as per "cycle" setting).

Note if you need this form to do "double duty" and want to use it in other
places in the application to allow the user will add MORE THEN ONE record at
a time, then put the following in the forms on open

if isnull(me.OpenArgs) = false then

me.Company = me.Openargs
me.AllowAdditions = false
Me.NavigationButtons = False

endif

That way, this form can be used else where in the program, but for our combo
box, the navigation buttons, and accidental adding of MORE THEN one record
will not occur.

All in all, you don't have to do any of the additional things I mention
above, but can *just* use the short sample code, but all of the above
together makes a real slick app.
 
S

SG

Albert,

Thank you for all of the information it is much appreciated!

I have one further question if I may.....

I have adapted the code which you kindly provided me and this is working
well apart from one thing.

Within my main form I have a subform which is in datasheet view.

If I start entering an Itemcode a primary key is created say number 50 for
example if the itemcode does no exist and the user selects not to add the
new itemcode and then tries to close the form they keep getting asked if
they want to add the itemcode. To resolve this the user has to delete the
itemcode number which they typed in out and click close the user then gets
the message "The Microsoft Jet database engine canot find a record in the
table 'products' with key matcching field(s) 'itemcode'

I really just want the page to close, any suggestions would be much
appreciated.

Many Thanks

S
 
A

Albert D. Kallal

The problem is that user has started typing into the sub-form record, and
the record is now created (I reasonably sure this is what is going on).

so, that point, the user not only has to erase what they typed into the
combo box, but you have now remove that record.

Try going edit->undo at that point......

I don't think this is as much a combo box problem as much as you already
added a record in the sub-form, but are trying to exit, and don't have a
legal item entered.....

This is much a 90% - 10% situation. The case where the user types in the
wrong itemcode, and does not add it..but ,t hen selects another item
code...you don't get the message...

The only case you get is when the user types in something...answers "no" to
the combo box..and THEN tries to exit. In this "rare" case, you might just
try try trapping the error (use the forms error event), and see if you
ignore this. However, ignoring the message is not really the soltion because
you have a reocrd sitting there...and need to either set the itemcode...or
not add the reocrd. If using edit->undo (might have do it 2 times), then
you might undo the blank reocrd add. If you can undo this, then try using
the forms error event..and thow in a me.undo for that error.........
 
S

SG

Albert,

I have found the error code which is 3101 I'm not very hot at all on
coding, could you please suggest how I may incorporate the me.undo command?

Thank you in advance


S
 
A

Albert D. Kallal

Somthing like:


if error code = 3101 then

me.Undo

end if

This all is based on my "assuming" that the sub-form reocrd is
created....but does not have a value....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top