473,383 Members | 1,805 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,383 software developers and data experts.

Add Items Not In List

sueb
379 256MB
This question is related to the database found in How to make a newly-added record show up in an existing form?.

What I have seems like a hot mess to me, while the thing I'm trying to achieve seems fairly simple. Here's how I would like this to behave:

1. The user puts a number in the "Jump To" field. If the number is already in the database, it should just go to that record, and it now does that.

2. If, however, the number is NOT in the database, I want a Yes/No window to pop up displaying the number that was entered, and the question "Add the new chart number?".

3. Clicking "Yes" would add a record to the database, with the new number in the ChartNum field, close the popup, and cause the parent form to go to the new record.

4. Clicking "No" would close the "Yes/No" window, and leave the parent form exactly as it was, with the "Jump To" field blank.

Currently, the user has to re-enter the number if they do want to add it, and if they don't want to add anything, closing the popup window (the current one) generates another "Item not in list" error box. And the user has to go to the new record herself, but it is also not always available to "jump to".

I just seem to have a lot of little scraps of code that don't play well together, and I don't know enough about the different events involved to know where is the best place to do all this. Somehow, it seems that I shouldn't have to have a separate form-based-on-the-database just to add a new record, but I don't know any other way to do that. Here are the different chunks of code, in the order they are happening:

The unbound field Jump To on the form Patient_IUR_Overview has the following 2 routines:

Jump_To_AfterUpdate jumps to the record that matches the user's input, and seems to work correctly:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Jump_To_AfterUpdate()
  2. On Error GoTo Err_Jump_To_AfterUpdate
  3.  
  4.     ' Find the record that matches the control.
  5.     Dim rs As Object
  6.  
  7.     Set rs = Me.Recordset.Clone
  8.     If Jump_To > "" Then
  9.         rs.FindFirst "[Patient Index] = " & Str(Nz(Me![Jump_To], 0))
  10.         If Not rs.EOF Then Me.Bookmark = rs.Bookmark
  11.     End If
  12.     Me![Jump_To] = ""
  13.  
  14. Exit_Jump_To_AfterUpdate:
  15.     Exit Sub
  16.  
  17. Err_Jump_To_AfterUpdate:
  18.     MsgBox Err.Description
  19.     Resume Exit_Jump_To_AfterUpdate
  20.  
  21. End Sub
Jump_To_NotInList is triggered when the user enters a number not yet in the database, and it calls Add_New_Chart_Click regardless of what the user clicks, so I know I'm not capturing their choice correctly on line 7:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Jump_To_NotInList(NewData As String, Response As Integer)
  2. On Error GoTo Err_Jump_To_NotInList
  3.  
  4.     Dim AddNew As Boolean
  5.  
  6.     Me![Jump_To] = ""
  7.     AddNew = MsgBox("Add a new chart number?", vbYesNo)
  8.     If AddNew Then Add_New_Chart_Click
  9.     AddNew = False
  10.  
  11. Exit_Jump_To_NotInList:
  12.     Exit Sub
  13.  
  14. Err_Jump_To_NotInList:
  15. '    MsgBox Err.Description
  16.     Resume Exit_Jump_To_NotInList
  17.  
  18. End Sub
The call to Add_New_Chart_Click brings up the Patients_Add_New form, which accepts entry (a second time!) of the chart number, adds the record, and closes itself (I included the OnClose event property code):
Expand|Select|Wrap|Line Numbers
  1. Private Sub Default_Invisible_Click()
  2. On Error GoTo err_Default_Invisible_AfterUpdate
  3.  
  4.     Call DoCmd.RunCommand(Command:=acCmdSaveRecord)
  5.     Call DoCmd.Close
  6.  
  7. exit_Default_Invisible_AfterUpdate:
  8.     Exit Sub
  9.  
  10. err_Default_Invisible_AfterUpdate:
  11. '    MsgBox Err.Description
  12.     Resume exit_Default_Invisible_AfterUpdate
  13.  
  14. End Sub
  15.  
  16. Private Sub Form_Close()
  17.     Requery_Open_Forms
  18. End Sub
In a public module:
Expand|Select|Wrap|Line Numbers
  1. Function Requery_Open_Forms()
  2. On Error GoTo Err_Requery_Open_Forms
  3.     Dim lngKt As Long
  4.     Dim lngI As Long
  5.     Dim frm As Access.Form
  6.  
  7. '    RequeryACollection clnOverviewClient
  8.     If CurrentProject.AllForms("Patient_IUR_Overview").IsLoaded Then
  9.         For lngI = 1 To lngKt
  10.             Set frm = clnOverviewClient.Item(lngI)
  11.             frm.Requery
  12.         Next
  13.     End If
  14.     RequeryACollection clnIURClient
  15.     RequeryACollection clnGISchedulingClient
  16.     If CurrentProject.AllForms("Clinic_Notification").IsLoaded Then
  17.         Forms("Clinic_Notification").Requery
  18.     End If
  19.  
  20. Exit_Requery_Open_Forms:
  21.    Exit Function
  22.  
  23. Err_Requery_Open_Forms:
  24.     MsgBox Err.Description
  25.     Resume Exit_Requery_Open_Forms
  26.  
  27. End Function
A curious thing is that the new record is not always available to navigate to, although the Jump To field now knows that it is "in the list".

You can see that this is just a tangle. What can I do to correct and streamline it?
Feb 28 '11 #1
10 3266
NeoPa
32,556 Expert Mod 16PB
The approach you suggest is one I like, and it can certainly be implemented (although I wouldn't say it's as straightforward as you seem to think).

With the code working (I need the OCX file for that) I may be able to test it better and it may well be that your code/design isn't too far off. If it doesn't involve too much work I may well help you to resolution of this situation. I'll look again in more detail when I have the file available.
Mar 1 '11 #2
TheSmileyCoder
2,322 Expert Mod 2GB
In regards to capturing the user's answer on lines:
Expand|Select|Wrap|Line Numbers
  1. AddNew = MsgBox("Add a new chart number?", vbYesNo)
  2.   If AddNew Then Add_New_Chart_Click
if you try to type this in your immediate window:
Expand|Select|Wrap|Line Numbers
  1. ? vbYes=True
You will see that access returns false. vbYes is a constant with a value of 6. True is a constant with a value of -1. False is 0.

An easier and cleaner approach (in my oppinion) is:
Expand|Select|Wrap|Line Numbers
  1. If vbYes=MsgBox("Chart Number: [" & NewData & "] does not exist." & vbnewline & vbnewline & "Would you like to add it now?", vbYesNo,"Chart number not found") Then
  2.   docmd.OpenForm "Patients_Add_New ",acNormal,,,acFormAdd,acDialog,NewData
  3.   Response=acDataErrContinue
  4. Else
  5.   Response=acDataErrContinue
  6.   Me![Jump_to]=Null
  7. End If
About the acDataErrContinue see:
The NotInList event occurs when the user enters a value in the text box portion of a combo box that isn't in the combo box list.

Private Sub object_NotInList(NewData As String, Response As Integer)
Object The name of a combo box control.

NewData A string that Microsoft Access uses to pass the text the user entered in the text box portion of the combo box to the event procedure.

Response The setting indicates how the NotInList event was handled. The Response argument can be one of the following intrinsic constants:

Constant Description
  • acDataErrDisplay
    (Default) Displays the default message to the user. You can use this when you don't want to allow the user to add a new value to the combo box list.
  • acDataErrContinue
    Doesn't display the default message to the user. You can use this when you want to display a custom message to the user. For example, the event procedure could display a custom dialog box asking if the user wanted to save the new entry. If the response is Yes, the event procedure would add the new entry to the list and set the Response argument to acDataErrAdded. If the response is No, the event procedure would set the Response argument to acDataErrContinue.
  • acDataErrAdded
    Doesn't display a message to the user but enables you to add the entry to the combo box list in the NotInList event procedure. After the entry is added, Microsoft Access updates the list by requerying the combo box. Microsoft Access then rechecks the string against the combo box list, and saves the value in the NewData argument in the field the combo box is bound to. If the string is not in the list, then Microsoft Access displays an error message.

You can pass the NewData (The new chart number) in the OpenArgs to your form, so that you dont have to retype the information. Examply would be as I have posted above:
Expand|Select|Wrap|Line Numbers
  1. docmd.OpenForm "Patients_Add_New ",acNormal,,,acFormAdd,acDialog,NewData
And in your Load Event of Patients_Add_New:
Expand|Select|Wrap|Line Numbers
  1. If OpenArgs & ""<>"" then
  2.   Me.tb_ChartNumber=Me.OpenArgs
  3. End If
The next part is when the form closes we want to go to the new record in your main form
Expand|Select|Wrap|Line Numbers
  1. 'Record Chart Number
  2. Dim lngChart as long
  3. lngChart=Me.tb_ChartNumber
  4.  
  5. Call DoCmd.RunCommand(Command:=acCmdSaveRecord)
  6.     Call DoCmd.Close
  7.     Forms("Frm_YourFormName").Requery
  8.     Forms("Frm_YourFormName").Recordset.FindFirst("ChartNumber=" & lngChart)
  9.  

I think this covers must of what you requested, allthough it might not be my must structured reply to date.... Try implementing it and come back with any issues/questions should they arise.

And NeoPa, sorry for "hi-jacking".
Mar 1 '11 #3
NeoPa
32,556 Expert Mod 16PB
Not a Hijack Smiley :-) That's when you change the subject of the thread.

Jumping-in maybe, but I couldn't complain about that after such a good answer (even if I had that right - which I don't). I'll still keep my eye on this thread but I think you've already broken the back of it. I just wanted to make sure Sue appreciated that the approach she suggested is very sensible and workable, if it does take a little bit of coding to implement within Access.
Mar 1 '11 #4
TheSmileyCoder
2,322 Expert Mod 2GB
Yes. The main part of the work was allready done by sueb, and described very well in the question.

I've just made slight modifications/suggestions here and there.
Mar 1 '11 #5
NeoPa
32,556 Expert Mod 16PB
I'd echo that :-) It seems Sue has progressed quite a bit since starting to post questions here last April (which is no disrespect to your starting position Sue - just that you seem to have progressed somewhat since).
Mar 1 '11 #6
sueb
379 256MB
@TheSmileyCoder: Thanks so much for such a detailed response! I might be prevented from trying these suggestions today, but I'll certainly be able to get to it by tomorrow, and will post back how it all went.

@NeoPa: I appreciate the good words! It's true that when I first started posting, I had really never worked with Access before, but I do have a couple of decades of software development in another field and with other tools under my belt, so it's really just a matter of "matching up" Access's ways/vocabulary/etc. with what I already know. The excellent advice on this forum (I no longer try any other) has really boosted this process. It's hard to express how much I owe to you all.
Mar 1 '11 #7
sueb
379 256MB
@TheSmileyCoder: I've finally got some time to try implementing your suggestions. I got it all put in, but when I ran it, I got a "Compile error: Method or data member not found" pointing to "tb_ChartNumber" in line 3:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.     If OpenArgs & "" <> "" Then
  3.         Me.tb_ChartNumber = Me.OpenArgs
  4.     End If
  5. End Sub
  6.  
and again to the same object in line 5:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Close()
  2. '    Requery_Open_Forms
  3.     ' Record Chart Number
  4.     Dim lngChart As Long
  5.     lngChart = Me.tb_ChartNumber
  6.  
  7.     Call DoCmd.RunCommand(Command:=acCmdSaveRecord)
  8.     Call DoCmd.Close
  9.     Forms("Patient_IUR_Overview").Requery
  10.     Forms("Patient_IUR_Overview").Recordset.FindFirst ("ChartNumber=" & lngChart)
  11.  
  12. End Sub
  13.  
I did have a question about what this is. I don't think I have a field named that, so I wondered where it came from
Mar 11 '11 #8
TheSmileyCoder
2,322 Expert Mod 2GB
I was presuming you had your chartnumber (whatever that is) in a textbox called tb_chartnumber.
Mar 11 '11 #9
sueb
379 256MB
@TheSmileyCoder, no, my field is still (because of my original approach) bound to a field in the database. Should I just change it to an unbound text box or something?
Mar 11 '11 #10
NeoPa
32,556 Expert Mod 16PB
I think Smiley is just saying he guessed at your control name. The point here is that you need to tweak the code to match the actual names you are using for your fields and controls.

NB A field is found within a recordset, but a control is found on Form and Report objects. Your sentence is a perfect example of why the two terms shouldn't be mixed up as you use field for both in the same sentence, thus leaving the reader somewhat confused as to what you're actually intending to convey.
Mar 14 '11 #11

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

Similar topics

33
by: Steven Bethard | last post by:
I feel like this has probably been answered before, but I couldn't find something quite like it in the archives. Feel free to point me somewhere if you know where this has already been answered. ...
10
by: bearophile | last post by:
This is my first Python program (it's an improvement of a recursive version by Luther Blissett). Given a list like this: , ]]] It produces the "flatted" version: I think this operation is...
6
by: Hrcko | last post by:
How to get a list of items from listBox into array ? Hrcko
2
by: George | last post by:
Using VS.NET 2002\Web Form\VB I have a dropdown list that will contain letters of the alphabet. At startup, it has only the letter A in the list. I add letters to the items list, as needed....
0
by: Brian Henry | last post by:
Since no one else knew how to do this I sat here all morning experimenting with this and this is what I came up with... Its an example of how to get a list of items back from a virtual mode list...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
0
by: Andrew | last post by:
I have created a Component called a BOConnector that implements IBindingList so it can provide access to a list of business objects to be bound to controls. At design time there is no live list...
13
by: PetterL | last post by:
I writing a program where I read menu items from a file. But I have problem when I click an menu item i want it to mark that one as checked but I cant access the menu object of that item to see...
2
by: =?Utf-8?B?RHJEQkY=?= | last post by:
I understand that the Value put into a DataGridViewComboBoxCell has to be a member of the Items list or an exception is thrown. I also understand that you can override that exception by handling...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.