Full Version: Docmd.GoToControl Doesn't Work
UtterAccess Forums > Microsoft® Access > Access Forms
mfronczak
I have a part entry form that requires that the user enter a part number. If the number already exists, I have a message box that pops up and tells the user. I also want the part to be "erased", and I want the cursor to go back to that field so the user can type in a new part. Here is my code in the AfterUpdate event of my text box [/list] :
If DCount("*", "tblPartDetail", "[Basepart] = [Forms]![frmAddNewPart]![Basepart]") > 0 Then
Response = MsgBox(Msg, Style, Title)
DoCmd.GoToControl "basepart"
Me.BasePart = Null
End If
The message box part works fine, as does the Null part, but the cursor always ends up in the next field no matter what I do. I've tried changing the order of the code and I've tried using the GoToControl or SetFocus and I can't get it to work. Any thoughts?
dannyseager
try...

me.basepart.setfocus

and

me.basepart = ""
mfronczak
I've tried both. With the me.basepart = "", I get an error saying that a zero-length string is not allowed.
When I do the me.basepart.setfocus, the cursor still jumps to the next control.
dannyseager
Can you post your DB...?
r_cubed
Ok, you are ALMOST there ...... the reason that the cursor is STILL moving is that "after" it has done the AFTERUPODATE event , Access STILL needs to perform the action that caused the action to occur , and THAT was the fact that you were moving ( tabbing ) away from the field in the first place.
You NEED to force the cursor to "temporarily move" to "another" control (other than the one that it would move to vai the tab keyboard action) on your form, BEFORE doing the .SetFocus (Personally, I always have an Exit button on my forms from where the user clicks to close a form, sot I tend to use THAT (exit) control as the control to "temporarily move to" in this type of scenario)
Therefore, the AfterUpdate event should read like this:
If DCount("*", "tblPartDetail", "[Basepart] = [Forms]![frmAddNewPart]![Basepart]") > 0 Then
Response = MsgBox(Msg, Style, Title)
ME.<SomeOtherFormControl.SETFOCUS
ME.BasePart.SETFOCUS
Me.BasePart = Null
End If
Next, the reason that you are getting the error when you try to do the Me.BasePart = "" is because at the TABLE DESIGN level you have the option for Allow Zero Length Field set to NO.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.