Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SetFocus Does Not Work

Status
Not open for further replies.

Greaser

Technical User
Aug 1, 2001
84
CA
Hi,
I have the following code in my ComboBox On Not In List:

...
'Ge rid of the "Not In List"error message.
Response = acDataErrContinue

'Clear the ComboBax
SendKeys "{ESC}"
SendKeys "{ESC}"
SendKeys "{ESC}"

' Move focus to the Contact's title
Forms!mailfrm!Title.SetFocus

I get a run time error message when I get to the SetFocus:
"MS Access can't move the focus to the Title focus"
SetFocus works fine anywhere else on the form.

Thanks for your help with this problem.
Cheers,
John
 
I found a title property that only exists for the Mac in help... Try renaming the control. I've run into trouble with a field name of line as in invoice.line.
 
Thanks for your reply.
Title.SetFocus works from any control in the form except from my ComboBox.
Any ideas?
Thanks,
John
 
From the sound of your post, your title field
Forms!mailfrm!Title.SetFocus
is on a separate form to your combo box. If this is correct, you need to set the focus to the form first, then set the focus to the control

Forms!mailfrm.SetFocus
Forms!mailfrm!Title.SetFocus

Of course, your second form must be open for this to work.

HTH
Lightning
 
No. All in one form. I tried to set focus on the form any way, but it still doesn't work. I suspect that my problem may have something to do with SendKeys .
Thanks,
John
 
Are the sendKeys statements required only to clear the new entry in the combobox?

If so, try replacing them with

ComboBoxName = Null

and then
Me.Title.Setfocus

This code worked for me in a quick test form.

HTH
Lightning
 
While discussing SendKeys, you may want to read the reasons to not use SendKeys. You'll also find a replacement for SendKeys that works better.

API: Replacement for Sendkeys
Terry L. Broadbent
faq183-874 contains some tips and ideas for posting questions in these forums. Please review it and comment if you have time.
NOTE: Reference to the FAQ is part of my signature and is not directed at any individual.
 
Thank you for your reply, but SetFocus is still not working.
Here is more information about the problem:
Here is the NotInList code:

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

Dim Msg As String
Dim CR As String

CR = Chr$(13)

' Exit this subroutine if the combo box was cleared.
If NewData = "" Then Exit Sub

' Confirm that the user wants to add the new customer.
Msg = "'" & NewData & "' is not in the list." & CR & CR
Msg = Msg & "Do you want to add it?"
If MsgBox(Msg, vbQuestion + vbYesNo) = vbNo Then
' If the user chose not to add a customer, set the Response
' argument to suppress an error message and undo changes.
Response = acDataErrContinue
' Display a customized message.
MsgBox "Please try again."
Else
'Ge rid of the "Not In List"error message.
Response = acDataErrContinue

'Clear the ComboBox
mySendKeys "{ESC}"
mySendKeys "{ESC}"

' Move focus to the Contact's title
Forms!mailFrm.SetFocus
' Forms!mailFrm!Title.SetFocus
If Err Then
' If a run-time error occurred while attempting to add a new
' record, set the Response argument to suppress an error
' message and undo changes.
Response = acDataErrContinue
' Display a customized message.
MsgBox Error$ & CR & CR & "Please try again.", vbExclamation
End If
End If
End Sub

When I uncomment the SetFocus code, the MsgBox(Msg, vbQuestion + vbYesNo) is generated each time I press the Yes button.
When I press the No button (after having pressed the Yes button), a run-time error occurs and the following message is displayed:

Run-time error '2110':
Microsoft Asccess can't move the focus to the control Title

Note that I tried changing the control name to Title1. Didn't work.
I tried setting the focus to another control.
Didn't work.
SetFocus works from any control other than my ComboBox.

Thank's for your help.
John
 
Just a hunch, but try moving to a record before you use setfocus.
 
A new record was created prior to reaching the ComboBox.
 
Hi!

I think the problem you are having stems from the fact that you are not adding the new data to the row source of the combo box. At least, if you are, you left that part of the code out of your post. Where does your combo box get its row information? Once we have that information, we can get this problem solved.

Jeff Bridgham
 
Hi Greaser

Try this code. It is cut directly from a test form and seems to do what you want. Note that I've added code to add the new entry to a table as well.

Good Luck.
******************************************************
Private Sub Contact_NotInList(NewData As String, Response As Integer)
'This event procedure is called from the ComboBox's NotInList event.
'The procedure adds the Contact Name to the underlying table upon confirmation from the user

Dim Msg As String
Dim CR As String

CR = Chr$(13)

'Exit this subroutine if the combo box was cleared.
If NewData = "" Then Exit Sub

' Confirm that the user wants to add the new customer.
' If the user chose not to add a customer, set the Response
' argument to suppress an error message and undo changes.
Msg = "'" & NewData & "' is not in the list." & CR & CR
Msg = Msg & "Do you want to add it?"
If MsgBox(Msg, vbQuestion + vbYesNo) = vbNo Then
' If the user chose not to add a customer, set the Response
' argument to suppress an error message and undo changes.
Response = acDataErrContinue
Contact = Null
Me.Title.SetFocus
' Display a customized message.
MsgBox "Please try again."
'If MsgBox("Add " & NewData & " to list?", 33, "Type of Application") = 1 Then
'Inform Event Procedure we're handling the error
Else
Response = acDataErrAdded
'Declare the database and Table
Dim Application As String
Dim db As Database
Dim TB As Recordset
Set db = CurrentDb
Application = NewData
'Add to table "tblApplication":
Set TB = db.OpenRecordset("tblTestContacts", dbOpenTable)

'Prepare Table for new record being added
TB.AddNew

'Write Data to fields
TB("ContactList") = Application
TB.Update
TB.Close

End If
End Sub
*****************************************************

HTH
Lightning
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top