Add name to combo box

G

Guest

I would like to program my OnNotList property to prompt users to add a new
name to the Employees table (the source table for my Project Manager combo
box) if it is not on the list. Is there a library of VBA code somewhere that
I could cut and paste from?

Nicole
 
G

Guest

I feel like this is almost getting me there, but I keep getting the "An Error
Occurred" message box. I'm thinking that it may be because I'm working with
a split database and the table can't be accessed.
 
F

Fred Boer

Hi Nicole:

Well, let's get to work trying to solve the problem!
Can you please post the exact code that you are using? You would, of course,
have had to modify that code...
Can you set a breakpoint and step through the code?

Cheers!
Fred

P.S. My internet is flaky (technician coming tomorrow) so apologies in
advance if I go offline or if there is a delay in response...
 
G

Guest

Fred:

I am a VBA dummy and don't know how to set a breakpoint and step through the
code. I did modify the code, the name of my table (or data set) is
Employees. An earlier attempt at debugging highlighted the line, rs.Close.
However removing it didn't change the behavior of the program. I'm ashamed
of my ignorance. Here's the modified code.

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

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strMsg As String

strMsg = "'" & NewData & "' is not an available Employee Name " & vbCrLf
& vbCrLf
strMsg = strMsg & "Do you want to associate the new Name to the current
Employee List?"
strMsg = strMsg & vbCrLf & vbCrLf & "Click Yes to link or No to re-type
it."

If MsgBox(strMsg, vbQuestion + vbYesNo, "Add new name?") = vbNo Then
Response = acDataErrContinue
Else
Set db = CurrentDb
Set rs = db.OpenRecordset("Employees", dbOpenDynaset)
On Error Resume Next
rs.AddNew
rs!Employees = NewData
rs.Update

If Err Then
MsgBox "An error occurred. Please try again."
Response = acDataErrContinue
Else
Response = acDataErrAdded
End If

End If

rs.Close
Set rs = Nothing
Set db = Nothing

End Sub
 
F

Fred Boer

Nicole:

Try putting the line "rs.close" on the line above the last "End If" (i.e.,
cut and paste it...). That should fix it. The line "rs.close" is trying to
close a recordset, but if you selected "No" the recordset hasn't been
created...


HTH
Fred Boer
 
F

Fred Boer

Dear Nicole:

I'm very sorry, but a personal situation has come up and I won't be able to
work with you on this problem. Besides, I don't think I'm helping, either!
:)

I am sure someone else will pick up the pieces for me - if not, please
repost, since a thread which has responses is sometimes ignored.

Again, my apologies...


Fred
 
G

Guest

Hi Nicole,

First, does your code compile without any errors? With the VBE (Visual Basic
Editor) open, click on Debug > Compile ProjectName, where ProjectName is the
name of your VBA project (likely the same as the name of your database). You
should not get any compile errors when you do this. If you do, then you need
to fix those first. If the option to compile becomes greyed out, then that is
a very good sign--it means that your code compiled without any errors.

Note: Errors can include compile-time errors and run-time errors. The two
are different. A clean compile does not mean that all is well, but it's
certainly a good start.
I am a VBA dummy and don't know how to set a breakpoint and step through the
code.

To set a break point, click your mouse into the grey margin area on the
left-hand side, when you have the VBE open. Note: You cannot set a break
point on any Dim statements; you'll need to pick a different line of code.
When you've successfully set a break point, you should see a maroon colored
dot in the grey area. In addition, the line of code will be highlighted with
the maroon color. (The maroon color assumes that you have not set a different
color, under your VBE Options dialog). You can set break points of different
lines of code, so that you have multiple break points added.

Using a break point
Start the procedure {a procedure is a subroutine or function} using the
normal method. In this case, it would involve entering a name into your combo
box that is not in the list. This should fire the Not-In-List event
procedure. The code should stop at whatever line you set the break point on.
You can now single-step through the code, one line at a time, using the F8
key. Alternatively, you can advance to the next break point (or to the end of
the procedure) by pressing the F5 button.

Note: In order for break points to work, you must have the option in Access,
under Tools > Startup: "Use Access Special Keys" checked. If this option is
unchecked, the code will not stop at a break point.

Single step through your code until you can determine exactly which line of
code is causing the error. Your code indicates that you have a field name
that is exactly the same as your table name:

rs.AddNew
rs!Employees = NewData <-------------
rs.Update

Does your Employees table include a field named Employees? I suspect that
you might have a field named something like EmployeeName instead.

It will be helpful to add error handling to your code. Try pasting in this
revised version of the code, replacing your existing version. Save the
project after pasting in the code. Then make sure to compile your code, as
discussed earlier:

Private Sub Proj_Tech_Lead_NotInList _
(NewData As String, Response As Integer)

On Error GoTo ProcError

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strMsg As String

strMsg = "'" & NewData & "' is not an available Employee Name." _
& vbCrLf & vbCrLf & "Do you want to associate the new " _
& "Name to the current Employee List?" & vbCrLf & vbCrLf _
& "Click Yes to link or No to re-type it."

If MsgBox(strMsg, vbQuestion + vbYesNo, "Add new name?") = vbNo Then
Response = acDataErrContinue
Else
Set db = CurrentDb
Set rs = db.OpenRecordset("Employees", dbOpenDynaset)
'On Error Resume Next '<-----Comment out for now **************
rs.AddNew
rs!EmployeeName = NewData '<---Use the correct field name in your
case.
rs.Update

If Err Then
MsgBox "An error occurred. Please try again."
Response = acDataErrContinue
Else
Response = acDataErrAdded
End If

End If

ExitProc:
'Cleanup
On Error Resume Next
rs.Close: Set rs = Nothing
Set db = Nothing
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, _
"Error in Proj_Tech_Lead_NotInList event procedure..."
Resume ExitProc
Resume
End Sub


I have commented out a line of code shown above, which reads On Error
Resume Next. Also, note the extra Resume statement added at the very end:

Resume ExitProc
Resume <--------------
End Sub

Normally, this line of code will never be executed. However, in break mode,
you can drag the yellow arrow to this line of code, bypassing the normal
Resume ExitProc. If you now hit the F8 key one more time, generally, the
offending line of code that had the problem will be highlighted. If you find
the offending line of code, please post back indicating which line is
involved.

This procedure uses DAO (Data Access Objects) code. Therefore, you must have
a reference set to the "Microsoft DAO 3.6 Object Library" (use version 3.51
for Access 97). Here is more information on references in Access databases:

Solving Problems with Library References (Allen Browne)
http://allenbrowne.com/ser-38.html

Access Reference Problems (Doug Steele)
http://www.accessmvp.com/djsteele/AccessReferenceErrors.html

Finally, there is an outside chance that your DAO library is not correctly
registered on your PC. To re-register this library, click on Start > Run.
Enter the following command, and then press the Enter key:

Regsvr32 "C:\Program Files\Common Files\Microsoft Shared\DAO\Dao360.dll"

You should see a message indicating success. It does not hurt anything to
re-register this file even if it is already registered properly.

**Remember to get in the habit of clicking on Debug > Compile, whenever you
are editing VBA code. You'll want to correct any compile-time errors as they
occur, instead of letting them build up.

Good Luck, and please let us know how it goes!

Tom Wickerath
Microsoft Access MVP
https://mvp.support.microsoft.com/profile/Tom
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 
G

Guest

Tom:

After carefully reading your response, before following your detailed
instructions, I corrected the line of code that did not have the correct
field name. The code now works and I was successfully able to add Hello
Kitty (my Jane Doe) as an employee to my list. I did notice however, that
the news of this addition was not given to other combo boxes that draw info
from the same table. I know I have to give these combo boxes the same code,
but I'm now concerned about duplicating names on my Employees table.

By the way, are you like an angel or something? I'm not one to blaspheme,
but only in the bible and under the power of the Holy Spirit does one get
such careful attention and complete resolutions to ones problems when calling
out for help. Thank you so much. I am in awe. I must give you and Fred yo
props!

Nicole
 
G

Guest

Hi Nicole,
I did notice however, that the news of this addition was not given to
other combo boxes that draw info from the same table.

It sounds to me like you may need to requery the affected combo boxes. You
can try adding statements like Me.cboName.Requery (replace "cboName" with
the actual name of the combo box) at the end of the NotInList event
procedure, but before the ExitProc label in my example. Add a similar
statement for each combo box that is in need of requerying.

Were you able to add a break point and step through the code?


Sign me,
Angel Tom :)

Microsoft Access MVP
https://mvp.support.microsoft.com/profile/Tom
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 
G

Guest

Tom:

I was able to add a break point and step through the code. Your suggestion
to re query sent back the error message that I must save the record before a
requery. So, I tried to add save code (DoCmd.Save acForm, "Status Reports 3")
right before the requery code, but this didn't do anything. Then I noticed
that if I chose to refresh, the other combo boxes were updated too. This led
me to try to find some code to refresh only (Me.Refresh). When stepping
through the code, I realized that right after this command, the program was
starting from the top, and if the user keeps pressing "yes" to add the name,
they'll add it over and over and over. I tried, but now I'm stuck again
(this time in a continuous loop).
 
G

Guest

Hi Nicole,

Now that I've re-read your sentence a bit more carefully:

"I did notice however, that the news of this addition was not given
to other combo boxes that draw info from the same table"

it occurs to me that you may need to open a form to add data to other fields
in the new record, instead of just adding the EmployeeName using VBA code.
Your other combo boxes may very well be seeing nulls for the field(s) that
are in their rowsources that represent the newly added record.

Try opening a form as a result of the NotInList event procedure firing, so
that you can add all of the data to the new record. The message that I posted
as a PS followup to my initial message includes a link to a sample I
previously prepared that opens a form.


Tom Wickerath
Microsoft Access MVP
https://mvp.support.microsoft.com/profile/Tom
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 

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

Top