Connecting Tech Pros Worldwide Forums | Help | Site Map

OnNotInList - tweaking pasted code

AccessIdiot's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: Stumptown
Posts: 402
#1: Feb 23 '07
I know I know, I'm the worst kind of coder. I am working on my first big Access project and with no formal training I am sort of stumbling my way through. I haven't worked with VB since college about 5 years ago.

I am trying to add a new item to a combo box by opening a new form. Here is my setup:
Specimen form has combo box Species_Code. Type something into Species_Code and if it's not there you get the pop up "Want to add?" if Yes then open Species form, type in a new species including other fields in that table (common name, scientific name, genus, etc). Then close the form and go on your merry way with the Specimen form.

Anyway, I copied this from a Microsoft suggested code page:

On the Species_Code combo box on the Specimen form:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Species_Code_NotInList(NewData As String, Response As Integer)
  2. Dim Result
  3. Dim Msg As String
  4. Dim CR As String
  5.  
  6.     CR = Chr$(13)
  7.  
  8.     If NewData = "" Then Exit Sub
  9.  
  10.     Msg = "'" & NewData & "' is not in the list." & CR & CR
  11.     Msg = Msg & "Do you want to add it?"
  12.     If MsgBox(Msg, vbQuestion + vbYesNo) = vbYes Then
  13.        DoCmd.OpenForm "Species", , , , acAdd, acDialog, NewData
  14.     End If
  15.  
  16.     Result = DLookup("[Species_Code]", "Species", _
  17.              "[Species_Code]='" & NewData & "'")
  18.     If IsNull(Result) Then
  19.        Response = acDataErrContinue
  20.        MsgBox "Please try again!"
  21.     Else
  22.        Response = acDataErrAdded
  23.     End If
  24. End Sub
  25.  

And on the Species form
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.    If Not IsNull(Me.OpenArgs) Then
  3.       Me![Species_Code] = Me.OpenArgs
  4.    End If
  5. End Sub
  6.  
Here's what happens: everything seems to work fine except that when the Species form opens it doesn't go to the last/new record in the table (there are already 100 species in the table, hence the drop down that you can add to). Instead it shows record 1 or 1 and the autonumber primary key is something like 1304579 instead of 101.

When I open the Species table the record was added but again the autonumber primary key is that weird number and not the logical next increment.

Any ideas?

And if I may be so bold - is there any way to prevent someone from continuing to work on the Specimen form if the add new species function/form is firing?

thanks, i love communities like this one.

melissa (the Access Idiot)

Rabbit's Avatar
Expert
 
Join Date: Jan 2007
Location: California
Posts: 3,835
#2: Feb 23 '07

re: OnNotInList - tweaking pasted code


Quote:

Originally Posted by AccessIdiot

I know I know, I'm the worst kind of coder. I am working on my first big Access project and with no formal training I am sort of stumbling my way through. I haven't worked with VB since college about 5 years ago.

I am trying to add a new item to a combo box by opening a new form. Here is my setup:
Specimen form has combo box Species_Code. Type something into Species_Code and if it's not there you get the pop up "Want to add?" if Yes then open Species form, type in a new species including other fields in that table (common name, scientific name, genus, etc). Then close the form and go on your merry way with the Specimen form.

Anyway, I copied this from a Microsoft suggested code page:

On the Species_Code combo box on the Specimen form:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Species_Code_NotInList(NewData As String, Response As Integer)
  2. Dim Result
  3. Dim Msg As String
  4. Dim CR As String
  5.  
  6.     CR = Chr$(13)
  7.  
  8.     If NewData = "" Then Exit Sub
  9.  
  10.     Msg = "'" & NewData & "' is not in the list." & CR & CR
  11.     Msg = Msg & "Do you want to add it?"
  12.     If MsgBox(Msg, vbQuestion + vbYesNo) = vbYes Then
  13.        DoCmd.OpenForm "Species", , , , acAdd, acDialog, NewData
  14.     End If
  15.  
  16.     Result = DLookup("[Species_Code]", "Species", _
  17.              "[Species_Code]='" & NewData & "'")
  18.     If IsNull(Result) Then
  19.        Response = acDataErrContinue
  20.        MsgBox "Please try again!"
  21.     Else
  22.        Response = acDataErrAdded
  23.     End If
  24. End Sub
  25.  

And on the Species form
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.    If Not IsNull(Me.OpenArgs) Then
  3.       Me![Species_Code] = Me.OpenArgs
  4.    End If
  5. End Sub
  6.  
Here's what happens: everything seems to work fine except that when the Species form opens it doesn't go to the last/new record in the table (there are already 100 species in the table, hence the drop down that you can add to). Instead it shows record 1 or 1 and the autonumber primary key is something like 1304579 instead of 101.

When I open the Species table the record was added but again the autonumber primary key is that weird number and not the logical next increment.

Any ideas?

And if I may be so bold - is there any way to prevent someone from continuing to work on the Specimen form if the add new species function/form is firing?

thanks, i love communities like this one.

melissa (the Access Idiot)

It's acFormAdd, not acAdd.

The thing about autonumbers is that they don't renumber themselves. If you had a 100 records, then deleted all the records, the next record you add will be 101 and not 1. It's just the way autonumbers work in Access.
AccessIdiot's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: Stumptown
Posts: 402
#3: Feb 23 '07

re: OnNotInList - tweaking pasted code


Thanks for that, but still the same problem.

So there is no way to launch the Species form to add a new species and have it go to the next new record (101)?
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,216
#4: Feb 23 '07

re: OnNotInList - tweaking pasted code


Quote:

Originally Posted by AccessIdiot

I know I know, I'm the worst kind of coder. I am working on my first big Access project and with no formal training I am sort of stumbling my way through. I haven't worked with VB since college about 5 years ago.

I am trying to add a new item to a combo box by opening a new form. Here is my setup:
Specimen form has combo box Species_Code. Type something into Species_Code and if it's not there you get the pop up "Want to add?" if Yes then open Species form, type in a new species including other fields in that table (common name, scientific name, genus, etc). Then close the form and go on your merry way with the Specimen form.

Anyway, I copied this from a Microsoft suggested code page:

On the Species_Code combo box on the Specimen form:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Species_Code_NotInList(NewData As String, Response As Integer)
  2. Dim Result
  3. Dim Msg As String
  4. Dim CR As String
  5.  
  6.     CR = Chr$(13)
  7.  
  8.     If NewData = "" Then Exit Sub
  9.  
  10.     Msg = "'" & NewData & "' is not in the list." & CR & CR
  11.     Msg = Msg & "Do you want to add it?"
  12.     If MsgBox(Msg, vbQuestion + vbYesNo) = vbYes Then
  13.        DoCmd.OpenForm "Species", , , , acAdd, acDialog, NewData
  14.     End If
  15.  
  16.     Result = DLookup("[Species_Code]", "Species", _
  17.              "[Species_Code]='" & NewData & "'")
  18.     If IsNull(Result) Then
  19.        Response = acDataErrContinue
  20.        MsgBox "Please try again!"
  21.     Else
  22.        Response = acDataErrAdded
  23.     End If
  24. End Sub
  25.  

And on the Species form
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.    If Not IsNull(Me.OpenArgs) Then
  3.       Me![Species_Code] = Me.OpenArgs
  4.    End If
  5. End Sub
  6.  
Here's what happens: everything seems to work fine except that when the Species form opens it doesn't go to the last/new record in the table (there are already 100 species in the table, hence the drop down that you can add to). Instead it shows record 1 or 1 and the autonumber primary key is something like 1304579 instead of 101.

When I open the Species table the record was added but again the autonumber primary key is that weird number and not the logical next increment.

Any ideas?

And if I may be so bold - is there any way to prevent someone from continuing to work on the Specimen form if the add new species function/form is firing?

thanks, i love communities like this one.

melissa (the Access Idiot)

If you are getting 'wierd' numbers that are also 'not in sequence' in an AutoNumber Field, then check the New Values property for the Field in the Grid below. Instead of being set to Increment, you may have it set to Random.
Rabbit's Avatar
Expert
 
Join Date: Jan 2007
Location: California
Posts: 3,835
#5: Feb 23 '07

re: OnNotInList - tweaking pasted code


I'm stumped, no idea why it won't go to a new record. As far as I can tell, it should. Maybe someone else will know why.
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,885
#6: Feb 23 '07

re: OnNotInList - tweaking pasted code


If you run compact and Repair on the database the numbers should resequence and give you 101.

Mary
AccessIdiot's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: Stumptown
Posts: 402
#7: Feb 23 '07

re: OnNotInList - tweaking pasted code


Quote:

Originally Posted by ADezii

If you are getting 'wierd' numbers that are also 'not in sequence' in an AutoNumber Field, then check the New Values property for the Field in the Grid below. Instead of being set to Increment, you may have it set to Random.


Hi, I did check and it is set to increment.

mmccarthy - is that just a temporary solution? or will that fix it so that it returns a correct number any time a new record is added?

thanks everyone!
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,885
#8: Feb 23 '07

re: OnNotInList - tweaking pasted code


Quote:

Originally Posted by AccessIdiot

Hi, I did check and it is set to increment.

mmccarthy - is that just a temporary solution? or will that fix it so that it returns a correct number any time a new record is added?

thanks everyone!

As long as you don't start adding and deleting records it should be fine.

Mary
Rabbit's Avatar
Expert
 
Join Date: Jan 2007
Location: California
Posts: 3,835
#9: Feb 23 '07

re: OnNotInList - tweaking pasted code


Quote:

Originally Posted by mmccarthy

As long as you don't start adding and deleting records it should be fine.

Mary

Didn't know Compact and Repair did that. Good to know.

I suppose you could set up the database to compact on close. Tools > Startup.
AccessIdiot's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: Stumptown
Posts: 402
#10: Feb 23 '07

re: OnNotInList - tweaking pasted code


Okay I figured out what I was doing wrong but not how to fix it. See, there is good reason why I am called Access Idiot! :-)

This is so basic I know that I am embarrassed to be asking but hey, I guess we all gotta start somewhere.

So what I am doing wrong has to do with the control and row source for the combo box. I have this form for "Specimen". On the Specimen form I want to have a drop down for Gender. I want the values for the dropdown to come from the Gender table but I want the gender chosen for that particular specimen to be held in the Specimen table. Pretty basic right? So I have the primary key of the Gender table as a foreign key in the Specimen table with a one-to-many relationship (many specimens can be male).

What I'm getting caught up in is how to set this in the properties of the stupid form. How do I set the Record Source for the form? What is the control source for the combo box? What is the row source? I'm getting very confused on how to set this all up.

But I think that solving this problem will solve my weird autonumber problem. Previously I had the control source as the gender field from the gender table, so anytime I chose "male" it would add a row for male to the gender table when I already had one in there.

I tried setting the record source for the form to * of the Specimen table. Then I set the control source for the combo box to be gender_id (since that is what the specimen table holds) and the row source to gender (so you'd actually see male, female, etc in the dropdown) but even though you see the text in the drop down it is looking for a number because the gender_id is an autonumber field. Argh!

I hope this makes sense and I hope you don't think I'm so ignorant that I don't deserve an answer!
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,885
#11: Feb 23 '07

re: OnNotInList - tweaking pasted code


Quote:

Originally Posted by AccessIdiot

Okay I figured out what I was doing wrong but not how to fix it. See, there is good reason why I am called Access Idiot! :-)

This is so basic I know that I am embarrassed to be asking but hey, I guess we all gotta start somewhere.

So what I am doing wrong has to do with the control and row source for the combo box. I have this form for "Specimen". On the Specimen form I want to have a drop down for Gender. I want the values for the dropdown to come from the Gender table but I want the gender chosen for that particular specimen to be held in the Specimen table. Pretty basic right? So I have the primary key of the Gender table as a foreign key in the Specimen table with a one-to-many relationship (many specimens can be male).

What I'm getting caught up in is how to set this in the properties of the stupid form. How do I set the Record Source for the form? What is the control source for the combo box? What is the row source? I'm getting very confused on how to set this all up.

But I think that solving this problem will solve my weird autonumber problem. Previously I had the control source as the gender field from the gender table, so anytime I chose "male" it would add a row for male to the gender table when I already had one in there.

I tried setting the record source for the form to * of the Specimen table. Then I set the control source for the combo box to be gender_id (since that is what the specimen table holds) and the row source to gender (so you'd actually see male, female, etc in the dropdown) but even though you see the text in the drop down it is looking for a number because the gender_id is an autonumber field. Argh!

I hope this makes sense and I hope you don't think I'm so ignorant that I don't deserve an answer!

Speciman form based on Speciman table with GenderID as a foreign key from speciman table. You don't need the Gender table included by query (unless you need if for something else).

Set up combo box with control source property set to GenderID, which (since Gender table isn't included in the query) will store the value in the Speciman table.

Set the following Properties of the combo box.

Under Format tab
Column Count = 2
Column Widths = 0cm; 2cm

Under Data tab
Row Source = SELECT GenderID, GenderName FROM Gender
Bound Column = 1

Mary
AccessIdiot's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: Stumptown
Posts: 402
#12: Feb 23 '07

re: OnNotInList - tweaking pasted code


Okay this is something that really ticks me off. See, I DID all that and it wouldn't work. When I erased the combo box and used the toolbox to draw one in and go through the wizard it DID work. Why? That is incredibly frustrating.

I should mention that when I created the form I went through the wizard and added all the fields from the Specimen table, then went into each field, changed it to a combo box, and then went into the properties to set the row source, bound column, etc. Mary when I change the settings to what you suggested the combo box displays the gender id, not the gender text which is in column 2. Any ideas why?

Seems like it should work if the properties are exactly the same no matter how you get there.

thanks all for your help, i'm glad i finally fixed the problem but I'm not happy with the method.
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,885
#13: Feb 23 '07

re: OnNotInList - tweaking pasted code


Quote:

Originally Posted by AccessIdiot

Okay this is something that really ticks me off. See, I DID all that and it wouldn't work. When I erased the combo box and used the toolbox to draw one in and go through the wizard it DID work. Why? That is incredibly frustrating.

If you haven't set the lookup list for the genderID in the table it will only come in as a textbox.

Quote:

Originally Posted by AccessIdiot

Mary when I change the settings to what you suggested the combo box displays the gender id, not the gender text which is in column 2. Any ideas why?

Did you set the column widths I gave you.
AccessIdiot's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: Stumptown
Posts: 402
#14: Feb 23 '07

re: OnNotInList - tweaking pasted code


Quote:

Originally Posted by mmccarthy

If you haven't set the lookup list for the genderID in the table it will only come in as a textbox.

Did you set the column widths I gave you.

I did set the column widths, thank you. That's another thing I don't get. Why do you have to set the column count to 2 and hide one? Why not just set it to the one you want to display? Kind of a weird quirk isn't it?

I did not set the lookup list. Question about that - if I set it in the table properties does that mean I can't add to it using the NotInList property?

again, thanks for all the patience and help - i am slowly getting it down!
AccessIdiot's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: Stumptown
Posts: 402
#15: Feb 24 '07

re: OnNotInList - tweaking pasted code


Okay I am making progress. I can open a form and have it go to a blank new record and the autonumber increments just fine. I have a combo box on this form. I type something into my combo box and it opens up another form to add the data to the underlying table. However, on THAT form the auto number is huge again and not incremental. Also, when I try to add data to a combo box on THAT form I get the dreaded standard Access pop up that my data wasn't chosen from the list.

So in other words:

Specimen Form - autonumber works great; has combo box called "species" - type in and if not in list launch Species Form.

Species Form - autonumber does not work but can fill in fields; has combo box called "genus" - type in and if not in list I get standard Access error message.

Any help?

Code on Specimen Form:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Species_NotInList(NewData As String, Response As Integer)
  2. Dim Result
  3. Dim Msg As String
  4. Dim CR As String
  5.  
  6.     CR = Chr$(13)
  7.  
  8.     ' Exit this subroutine if the combo box was cleared.
  9.     If NewData = "" Then Exit Sub
  10.  
  11.     ' Ask the user if he or she wishes to add the new customer.
  12.     Msg = "'" & NewData & "' is not in the list." & CR & CR
  13.     Msg = Msg & "Do you want to add it?"
  14.     If MsgBox(Msg, vbQuestion + vbYesNo) = vbYes Then
  15.        ' If the user chose Yes, start the Species form in data entry
  16.        ' mode as a dialog form, passing the new species code name in
  17.        ' NewData to the OpenForm method's OpenArgs argument. The
  18.        ' OpenArgs argument is used in Species form's Form_Load event
  19.        ' procedure.
  20.        DoCmd.OpenForm "Species", , , , acFormAdd, acDialog, NewData
  21.     End If
  22.  
  23.     ' Look for the species code name the user created in the Specimen form.
  24.     Result = DLookup("[Species_Code]", "Species", _
  25.              "[Species_Code]='" & NewData & "'")
  26.     If IsNull(Result) Then
  27.        ' If the species code was not created, set the Response argument
  28.        ' to suppress an error message and undo changes.
  29.        Response = acDataErrContinue
  30.        ' Display a customized message.
  31.        MsgBox "Please try again!"
  32.     Else
  33.        ' If the species code was created, set the Response argument to
  34.        ' indicate that new data is being added.
  35.        Response = acDataErrAdded
  36.     End If
  37.  
  38. End Sub
Code on Species Form
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.    If Not IsNull(Me.OpenArgs) Then
  3.       Me![Species_Code] = Me.OpenArgs
  4.    End If
  5. End Sub
  6.  
  7. Private Sub Genus_NotInList(NewData As String, Response As Integer)
  8. Dim db As DAO.Database
  9. Dim rs As DAO.Recordset
  10. Dim strMsg As String
  11.  
  12.     strMsg = "'" & NewData & "' is not an available AE Name " & vbCrLf & vbCrLf
  13.     strMsg = strMsg & "Do you want to associate the new Name to the current DLSAF?"
  14.     strMsg = strMsg & vbCrLf & vbCrLf & "Click Yes to link or No to re-type it."
  15.  
  16.     If MsgBox(strMsg, vbQuestion + vbYesNo, "Add new name?") = vbNo Then
  17.         Response = acDataErrContinue
  18.     Else
  19.         Set db = CurrentDb
  20.         Set rs = db.OpenRecordset("Genus", dbOpenDynaset)
  21.         On Error Resume Next
  22.         rs.AddNew
  23.             rs!AEName = NewData
  24.         rs.Update
  25.  
  26.         If Err Then
  27.             MsgBox "An error occurred. Please try again."
  28.             Response = acDataErrContinue
  29.         Else
  30.             Response = acDataErrAdded
  31.         End If
  32.  
  33.     End If
  34.  
  35. rs.Close
  36. Set rs = Nothing
  37. Set db = Nothing
  38. End Sub
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,730
#16: Feb 26 '07

re: OnNotInList - tweaking pasted code


Quote:

Originally Posted by AccessIdiot

I did set the column widths, thank you. That's another thing I don't get. Why do you have to set the column count to 2 and hide one? Why not just set it to the one you want to display? Kind of a weird quirk isn't it?

I did not set the lookup list. Question about that - if I set it in the table properties does that mean I can't add to it using the NotInList property?

again, thanks for all the patience and help - i am slowly getting it down!

Melissa (I can't call you AccessIdiot - it doesn't work for me ;)),
If you have a ComboBox where you want to return 1 value and show another, then it needs to have both the shown and the returned fields as columns. You can show both and return one, but you can't either show or return a column unless it's included in the data.
I'm afraid I didn't really follow the other question - sorry.
AccessIdiot's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: Stumptown
Posts: 402
#17: Feb 26 '07

re: OnNotInList - tweaking pasted code


Thank you NeoPa, that makes perfect sense now. :-)

Mary check your email and thank you!

cheers,
melissa
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,730
#18: Feb 26 '07

re: OnNotInList - tweaking pasted code


No Problem.
We'll make an expert of you yet. I can just see the introductory thread :
'Welcome to our newest Access Expert AccessIdiot.' :D
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,885
#19: Feb 27 '07

re: OnNotInList - tweaking pasted code


Expand|Select|Wrap|Line Numbers
  1. Private Sub Genus_NotInList(NewData As String, Response As Integer)
  2. Dim db As DAO.Database
  3. Dim rs As DAO.Recordset
  4. Dim strMsg As String
  5.  
  6.     strMsg = "'" & NewData & "' is not an available AE Name " & vbCrLf & vbCrLf
  7.     strMsg = strMsg & "Do you want to associate the new Name to the current DLSAF?"
  8.     strMsg = strMsg & vbCrLf & vbCrLf & "Click Yes to link or No to re-type it."
  9.  
  10.     If MsgBox(strMsg, vbQuestion + vbYesNo, "Add new name?") = vbNo Then
  11.         Response = acDataErrContinue
  12.     Else
  13.         Set db = CurrentDb
  14.         Set rs = db.OpenRecordset("Genus", dbOpenDynaset)
  15.         On Error Resume Next
  16.         rs.AddNew
  17.             rs!Genus = NewData
  18.         rs.Update
  19.  
  20.         If Err Then
  21.             MsgBox "An error occurred. Please try again."
  22.             Response = acDataErrContinue
  23.         Else
  24.             Response = acDataErrAdded
  25.         End If
  26.  
  27.     End If
  28.  
  29.    rs.Close
  30.    Set rs = Nothing
  31.    Set db = Nothing
  32.    Me.Genus.Requery
  33.  
  34. End Sub
  35.  
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,885
#20: Feb 27 '07

re: OnNotInList - tweaking pasted code


The main problem with the NotInList code was you were trying to update a non existant field.

Your code had

Expand|Select|Wrap|Line Numbers
  1.  
  2. rs!AEName = NewData
instead of

Expand|Select|Wrap|Line Numbers
  1.  
  2. rs!Genus = NewData
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,885
#21: Feb 27 '07

re: OnNotInList - tweaking pasted code


Problem regarding checkbox on the form controlling the subform was that it was unbound. Therefore when you set it to true on one form record it was remaining true for all records. I've gotten around this by creating a field in the speciman table to store the value of this checkbox. It will now change with the forms on current event. The other problem was that the subforms data entry property was set to yes so you couldn't view existing records but only add new records. I've changed this to no.

Mary
AccessIdiot's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: Stumptown
Posts: 402
#22: Feb 27 '07

re: OnNotInList - tweaking pasted code


Quote:

Originally Posted by mmccarthy

The main problem with the NotInList code was you were trying to update a non existant field.

Your code had

Expand|Select|Wrap|Line Numbers
  1.  
  2. rs!AEName = NewData
instead of

Expand|Select|Wrap|Line Numbers
  1.  
  2. rs!Genus = NewData

I never would have seen that in a million years - thank you!


Quote:

Originally Posted by mmccarthy

Problem regarding checkbox on the form controlling the subform was that it was unbound. Therefore when you set it to true on one form record it was remaining true for all records. I've gotten around this by creating a field in the speciman table to store the value of this checkbox. It will now change with the forms on current event. The other problem was that the subforms data entry property was set to yes so you couldn't view existing records but only add new records. I've changed this to no.

Mary

I knew it was unbound but didn't know how to bind it. I never would have thought to add a field to the table. I also didn't realize that the data entry property set to yes meant you couldn't view existing records? That doesn't seem intuitive. Thanks for catching that and fixing my checkbox problem as well!

I'm sure I'll be back with more problems as our forms grow. This has been a great learning experience for me though - thanks to everyone for the help and especially to Mary!
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,885
#23: Feb 27 '07

re: OnNotInList - tweaking pasted code


No problem!

Glad to help.

Mary
AccessIdiot's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: Stumptown
Posts: 402
#24: Feb 27 '07

re: OnNotInList - tweaking pasted code


Okay I'm back. :-) That didn't last long did it?

I've been playing with the code a bit, trying to break it (because I know the end user eventually will and they'll be stuck on a boat in another state and calling me for help on how to fix it) and of course I did. I was getting a compile error on the code on the 2nd combo box, the one for Genus on the Species form. So I got rid of it and found simpler code that I am using instead:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Genus_NotInList(NewData As String, Response As Integer)
  2.     Dim strSQL As String
  3.     Dim i As Integer
  4.     Dim Msg As String
  5.  
  6.     'Exit this sub if the combo box is cleared
  7.     If NewData = "" Then Exit Sub
  8.  
  9.     Msg = "'" & NewData & "' is not currently in the list." & vbCr & vbCr
  10.     Msg = Msg & "Do you want to add it?"
  11.  
  12.     i = MsgBox(Msg, vbQuestion + vbYesNo, "Unknown Genus Category...")
  13.     If i = vbYes Then
  14.         strSQL = "Insert Into Genus ([Genus]) " & _
  15.                  "values ('" & NewData & "');"
  16.         CurrentDb.Execute strSQL, dbFailOnError
  17.         Response = acDataErrAdded
  18.     Else
  19.         Response = acDataErrContinue
  20.     End If
  21. End Sub
  22.  
This works great! Except I am still having problem with the autonumber not incrementing and giving me huge weird numbers, both for the new species that was added to the combo box on Specimen and to the new genus that was added to the combo box on Species.

Any ideas why this is so?

Also, when I close the form that adds a new Species the Specimen form advances to a new record. But I would like it to remain on the current record in case the user hasn't filled out all the information on the form. I want them to have to advance to a new record when they are ready. Is there any way to set this up?

Note that I do have some code on the Specimen form that causes it to open to a new/blank record when it is launched (the onload event) but I was hoping that wouldn't fire when the form is already opened. Could it be interfering?
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,885
#25: Feb 27 '07

re: OnNotInList - tweaking pasted code


Quote:

Originally Posted by AccessIdiot

Okay I'm back. :-) That didn't last long did it?

I've been playing with the code a bit, trying to break it (because I know the end user eventually will and they'll be stuck on a boat in another state and calling me for help on how to fix it) and of course I did. I was getting a compile error on the code on the 2nd combo box, the one for Genus on the Species form. So I got rid of it and found simpler code that I am using instead:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Genus_NotInList(NewData As String, Response As Integer)
  2.     Dim strSQL As String
  3.     Dim i As Integer
  4.     Dim Msg As String
  5.  
  6.     'Exit this sub if the combo box is cleared
  7.     If NewData = "" Then Exit Sub
  8.  
  9.     Msg = "'" & NewData & "' is not currently in the list." & vbCr & vbCr
  10.     Msg = Msg & "Do you want to add it?"
  11.  
  12.     i = MsgBox(Msg, vbQuestion + vbYesNo, "Unknown Genus Category...")
  13.     If i = vbYes Then
  14.         strSQL = "Insert Into Genus ([Genus]) " & _
  15.                  "values ('" & NewData & "');"
  16.         CurrentDb.Execute strSQL, dbFailOnError
  17.         Response = acDataErrAdded
  18.     Else
  19.         Response = acDataErrContinue
  20.     End If
  21. End Sub
  22.  
This works great! Except I am still having problem with the autonumber not incrementing and giving me huge weird numbers, both for the new species that was added to the combo box on Specimen and to the new genus that was added to the combo box on Species.

Any ideas why this is so?

Also, when I close the form that adds a new Species the Specimen form advances to a new record. But I would like it to remain on the current record in case the user hasn't filled out all the information on the form. I want them to have to advance to a new record when they are ready. Is there any way to set this up?

Note that I do have some code on the Specimen form that causes it to open to a new/blank record when it is launched (the onload event) but I was hoping that wouldn't fire when the form is already opened. Could it be interfering?

Leave this with me for a while. I'll play around a bit and get back to you tomorrow.

Mary
AccessIdiot's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: Stumptown
Posts: 402
#26: Feb 27 '07

re: OnNotInList - tweaking pasted code


Thanks Mary!
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,730
#27: Feb 27 '07

re: OnNotInList - tweaking pasted code


I go away for five minutes and come back to find 490 (odd) new posts :(
I presume from the back-and-forth that the two of you are working on an e-mailed version of the actual database rather than intuitively from what's been posted?
Good work anyway Mary - not been idle again I see :)
AccessIdiot's Avatar
Needs Regular Fix
 
Join Date: Feb 2007
Location: Stumptown
Posts: 402
#28: Feb 27 '07

re: OnNotInList - tweaking pasted code


Yes I emailed a copy to Mary but nothing much has been changed from what has been posted. I am happy to send you a copy if you have any ideas! :-D
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,730
#29: Feb 27 '07

re: OnNotInList - tweaking pasted code


Thanks for the offer Melissa, but I'll take a pass.
I just wanted to make it plain to any other readers of the thread that this had happened and I'm not remotely 'jealous' of the attention Mary has deservedly got from her work.
One of her answers was posted which referenced information which wasn't publicly available, so I guessed, and thought it a good idea to make it clear.
I'll help where I can of course, but if you have Mary on your thread you don't often need anyone else ;)
Reply