How set focus to a field (1 Viewer)

Marinus

I'm learning... :)
Local time
Today, 10:24
Joined
Jun 16, 2010
Messages
140
Hi Guys,

Perhaps someone could shake a solution out of the sleeve that I am not able to find. on my form I have a command button to create a new record.
When pressed I need to move my cursor to the name field (called c_name), I think I need to SetFocus but get lost from there.

The code under the button is;

Private Sub New_Docket_Click()
On Error GoTo New_Docket_Click_Err

On Error Resume Next
DoCmd.GoToRecord , "", acNewRec
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If

New_Docket_Click_Exit:
Exit Sub

New_Docket_Click_Err:
MsgBox Error$
Resume New_Docket_Click_Exit


Sorry for the question but I think I learn quickly..

Thanks guys for all your help.

Kind regards,

Reece
 

vbaInet

AWF VIP
Local time
Today, 10:24
Joined
Jan 22, 2010
Messages
26,374
Is this code something that the wizard created? There seems to be conflicting error handlers. Maybe you copied and pasted it from somewhere?
 

missinglinq

AWF VIP
Local time
Today, 06:24
Joined
Jun 20, 2003
Messages
6,423
Code:
Private Sub New_Docket_Click()
On Error GoTo Err_New_Docket_Click

    DoCmd.GoToRecord , , acNewRec
    [B]c_Name.SetFocus[/B]
     
Exit_New_Docket_Click:
    Exit Sub

Err_New_Docket_Click:
    MsgBox Err.Description
    Resume Exit_New_Docket_Click
    
End Sub
 

Marinus

I'm learning... :)
Local time
Today, 10:24
Joined
Jun 16, 2010
Messages
140
Is this code something that the wizard created? There seems to be conflicting error handlers. Maybe you copied and pasted it from somewhere?

You are right, the wizard in Access 2007 converted the Macros I had to code, I had Macros first but then they were less functional, so converted they came out like this, they all work perfectly and I have been able to put more functions behind buttons, just can't shift my cursor to the new field..

Okay, when checked the EndSub is missing as last line..

But any advice as taken on-board.

P.S. it is not code copied from another program
 

vbaInet

AWF VIP
Local time
Today, 10:24
Joined
Jan 22, 2010
Messages
26,374
You are right, I had Macros first but then they were less functional, so converted they came out like this, they all work perfectly and I have been able to put more functions behind buttons, just can't shift my cursor to the new field..

Okay, when checked the EndSub is missing as last line..

But any advice as taken on-board...
missinglinq has tidied it up for you above ^^^
 

Marinus

I'm learning... :)
Local time
Today, 10:24
Joined
Jun 16, 2010
Messages
140
Code:
Private Sub New_Docket_Click()
On Error GoTo Err_New_Docket_Click

    DoCmd.GoToRecord , , acNewRec
    [B]c_Name.SetFocus[/B]
     
Exit_New_Docket_Click:
    Exit Sub

Err_New_Docket_Click:
    MsgBox Err.Description
    Resume Exit_New_Docket_Click
    
End Sub

Thanks MissingLinq, tried that and got compile error "Method or data member not found"

Private Sub New_Docket_Click()
On Error GoTo Err_New_Docket_Click

DoCmd.GoToRecord , , acNewRec
C_Name.SetFocus

Exit_New_Docket_Click:
Exit Sub

Err_New_Docket_Click:
MsgBox Err.Description
Resume Exit_New_Docket_Click
End Sub

SetFocus is highlighted when debugger kicks in..
 

boblarson

Smeghead
Local time
Today, 03:24
Joined
Jan 12, 2001
Messages
32,059
You should disambiguate by using

Me.c_name.setfocus

but you use the CONTROL name not the field name. So, if they are different you would use the control name instead.
 

Marinus

I'm learning... :)
Local time
Today, 10:24
Joined
Jun 16, 2010
Messages
140
Hi Bob, Still no luck, again compiler error, when I use the Name option in the property list, the top item, I get error invalid qualifier. When using the Controlname C-Name I get compiler error method or data member not found..
 

boblarson

Smeghead
Local time
Today, 03:24
Joined
Jan 12, 2001
Messages
32,059
Hi Bob, Still no luck, again compiler error, when I use the Name option in the property list, the top item, I get error invalid qualifier. When using the Controlname C-Name I get compiler error method or data member not found..

Because you have a special character in the name you will need to use square brackets.

Me.[C-name]

Sorry I thought it was an underscore.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 21:24
Joined
Jan 20, 2009
Messages
12,849
use the CONTROL name not the field name.

I am came to this thread just to say what Bob has just implied.

Focus cannot be set to a field. Those boxes on a form are not "fields". They are "controls". Fields are in tables and queries.

A form can have fields but they are in its Recordset (the data defined by its RecordSource.)

It is important to understand this and it becomes vital when the controls on the form do not have the same name as their ControlSource. It is also important to know the difference in VBA.

[Rant alert]
Unfortunately many developers, even those who should know better, continue to refer to controls as fields. We should all be using correct terminology.

It is bad enough having Microsoft referring to "fields" as "columns" in tables and queries. Columns are in listboxes and combos. And of course spreadsheets where I assume the incorrect terminology that crept into Access and SQL Server originated.

[Rant over]
 

Marinus

I'm learning... :)
Local time
Today, 10:24
Joined
Jun 16, 2010
Messages
140
Because you have a special character in the name you will need to use square brackets.

Me.[C-name]

Sorry I thought it was an underscore.

Sorry Bob, it was a underscore, typo on my side, but will try with brackets as well, good way to learn.. Thanks for the tip..
 

Marinus

I'm learning... :)
Local time
Today, 10:24
Joined
Jun 16, 2010
Messages
140
Thanks Bob and Galaxiom, another lesson learned, The first one is that the controlname had the same name as my field name. This did not work probably because of the usage of the same names. The second lesson I learned is not to use common names for Controls, after renaming my control name from Name to frmC_Name it did work. Again thanks to both of you and for the Rant Galaxiom, it is help like this that builds up my knowledge in the right way.. Next time my debugger comes up with an error I have something to check first....

Regards,

Reece

A 1000 mile journey always starts with the first step
 

missinglinq

AWF VIP
Local time
Today, 06:24
Joined
Jun 20, 2003
Messages
6,423
...The second lesson I learned is not to use common names for Controls...
Not common names, Reserved Words! Here a link to a list of them:

http://www.databasedev.co.uk/ms-access-reserved-words.html

Any addition to a Reserved Word will make it acceptable to Access. The word Date is problematic, but txtDate or StartDate or even DDate are acceptable.

The default practice of naming controls on forms the same as the fields they are bound to is another example of poor choices sometimes made by the boys in Redmond!
 

Marinus

I'm learning... :)
Local time
Today, 10:24
Joined
Jun 16, 2010
Messages
140
Thanks Missinglinq, As per above, thanks for all your input and giving me the ability to learn proper programming, this is my first time programming and all of you Guys have made it possible to develop a proper application, I have learn't so much..
 

Users who are viewing this thread

Top Bottom