Combobox throwing error the first time you use it, but not later times (1 Viewer)

cricketbird

Registered User.
Local time
Today, 07:07
Joined
Jun 17, 2013
Messages
108
I have a combobox that looks up the employeeid (user sees a list of employee names) to navigate to that record. Many folks who know the employee's ID number just type the ID number itself rather than scroll, and that works great too.

However, some folks occasionally type the user's name ("Jones") in instead, which obviously doesn't work.

I played around with adding some code to allow this, which ALMOST works great.

If you open the form and type a text string into the combo box you will immediately (before any code executes) get the error "The value you entered isn't valid for this field".

However, once you USE the combo box to navigate somewhere (anywhere), it will then work fine (allowing text searches) forever until you re-open the form. Somehow, after using the combobox once, Access no longer verifies that you've entered a number and allows my code to run. You can navigate between records, refresh, requery, and the combo box will still work great, but only after you use it (in the traditional way) once each time you open the form.

Any ideas on why it throws an error if you enter text the first time you use the combo box after opening the form, but doesn't later? And how to get that "later" behavior at the start?

Thank you!

Code:
Private Sub Combo1_AfterUpdate()

Dim myVal As String
On Error GoTo Err_Handler

myVal = Me!Combo1


If IsNumeric(myVal) Then
    <open form to appropriate employeeid>
Else
   <open form to the first user where their name contains the search string>
End If

Err_Handler:
   MsgBox "There is no user with that ID or name."
   Exit Sub   

End Sub
 

jdraw

Super Moderator
Staff member
Local time
Today, 07:07
Joined
Jan 23, 2006
Messages
15,379
I suggest you post a copy of your database with instructions to highlight the issue.
What is the rowsource of your combo?
You would normally select a combo entry, you would not be typing a value into the combo.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 07:07
Joined
May 21, 2018
Messages
8,529
It sounds like your control is bound. Navigation controls should be unbound. Seperae data entry an record navigation.
 

cricketbird

Registered User.
Local time
Today, 07:07
Joined
Jun 17, 2013
Messages
108
Thanks all!
The control is not bound.
The rowsource is a query.
I cannot post the database, unfortunately.
 

Gasman

Enthusiastic Amateur
Local time
Today, 12:07
Joined
Sep 21, 2011
Messages
14,304
I have a combobox that looks up the employeeid (user sees a list of employee names) to navigate to that record. Many folks who know the employee's ID number just type the ID number itself rather than scroll, and that works great too.

However, some folks occasionally type the user's name ("Jones") in instead, which obviously doesn't work.

I played around with adding some code to allow this, which ALMOST works great.

If you open the form and type a text string into the combo box you will immediately (before any code executes) get the error "The value you entered isn't valid for this field".

However, once you USE the combo box to navigate somewhere (anywhere), it will then work fine (allowing text searches) forever until you re-open the form. Somehow, after using the combobox once, Access no longer verifies that you've entered a number and allows my code to run. You can navigate between records, refresh, requery, and the combo box will still work great, but only after you use it (in the traditional way) once each time you open the form.

Any ideas on why it throws an error if you enter text the first time you use the combo box after opening the form, but doesn't later? And how to get that "later" behavior at the start?

Thank you!

Code:
Private Sub Combo1_AfterUpdate()

Dim myVal As String
On Error GoTo Err_Handler

myVal = Me!Combo1


If IsNumeric(myVal) Then
    <open form to appropriate employeeid>
Else
   <open form to the first user where their name contains the search string>
End If

Err_Handler:
   MsgBox "There is no user with that ID or name."
   Exit Sub 

End Sub
Well that cannot be the code?. It will fall through to the error message every time? :(
 

cricketbird

Registered User.
Local time
Today, 07:07
Joined
Jun 17, 2013
Messages
108
Well that cannot be the code?. It will fall through to the error message every time? :(
It works fine every time except upon opening the form. The error isn't being thrown by the code - it's being thrown by Access doing some sort of typecheck on the entry, but it doesn't seem to do that after you use the combobox.
 

jdraw

Super Moderator
Staff member
Local time
Today, 07:07
Joined
Jan 23, 2006
Messages
15,379
I cannot post the database, unfortunately.
I think there is something else in your set up you are not telling us.
 

GPGeorge

Grover Park George
Local time
Today, 04:07
Joined
Nov 25, 2004
Messages
1,867
Thanks all!
The control is not bound.
The rowsource is a query.
I cannot post the database, unfortunately.
It can be difficult to know what is important and what isn't when first learning to troubleshoot code, so please take Jack's comment to heart. Overshare the context and details.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 07:07
Joined
May 21, 2018
Messages
8,529
Code:
It works fine every time except upon opening the form. The error isn't being thrown by the code - it's being thrown by Access doing some sort of typecheck on the entry, but it doesn't seem to do that after you use the combobox.
Before you select a value from the combo its value is Null. Any code or query that relies on this could fail. Do you have either a query or code that references this combo?
 

LarryE

Active member
Local time
Today, 04:07
Joined
Aug 18, 2021
Messages
591
The combox Bound Column property will allow either numbers or text to be entered but not both. If you want the user to use the combox to search numbers, then the Bound Column property number needs to be a number in the RowSource.

Given the users are searching employee id's, I would not allow anything to be entered in the combo box. Allow the user to select an employee ID number from the drop-down list and set the combo box Limit To List property to Yes. That way, users cannot enter anything into the box, but must use the drop-down numerical vlaue.

You can also refresh the combo box when a user enters the box:

Private Sub combo1_Enter()
Me.combo1.Requery
End Sub
 

Users who are viewing this thread

Top Bottom