+ Reply to Thread
Results 1 to 9 of 9

Passing field name to form to set focus

  1. #1
    Registered User
    Join Date
    04-14-2017
    Location
    Leeds, England
    MS-Off Ver
    2007
    Posts
    5

    Passing field name to form to set focus

    Is there a way I can pass the name of a field to a routine which opens a VBA form with the focus set on that field, please?

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Passing field name to form to set focus

    Hi and welcome.

    Were are you passing the variable from? A worksheet? Another form?

    BSB

  3. #3
    Registered User
    Join Date
    04-14-2017
    Location
    Leeds, England
    MS-Off Ver
    2007
    Posts
    5

    Re: Passing field name to form to set focus

    Thankyou BadlySpelledBuoy
    A VBA subroutine runs through several rows on my worksheet, detects any errors in the cells on the row. So for example, it might find an error in the "Surname" column, at which point I will display my form which has numerous fields on it, but In this case I want to position focus on the "Surname" control (a textbox) within the form. Another time it might find an error in the "Occupation" column, etc etc.

  4. #4
    Registered User
    Join Date
    04-14-2017
    Location
    Leeds, England
    MS-Off Ver
    2007
    Posts
    5

    Re: Passing field name to form to set focus

    I could employ a Select structure testing a string that points to the right field and set the SetFocus for each possibility, but that is a little bit cumbersome I think you'll agree.
    Eg

    Error detected in column "Surname".....

    Select Case Field_To_Focus
    Case "fSurname"
    fSurname.SetFocus
    Case "fOccupation"
    fOccupation.SetFocus
    Case "fReviewDays"
    fReviewDays.SetFocus
    End Select

  5. #5
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,303

    Re: Passing field name to form to set focus

    Welcome to the forum

    Please attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

    Lot easier to give a precise solution with the workbook - saves much time crystal ball gazing.
    torachan.

  6. #6
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Passing field name to form to set focus

    It all depends on how your current code is identifying these errors.

    As torachan suggests, attach a sample workbook and it'll be much easier for someone to help.

    BSB

  7. #7
    Registered User
    Join Date
    04-14-2017
    Location
    Leeds, England
    MS-Off Ver
    2007
    Posts
    5

    Re: Passing field name to form to set focus

    Here you go.
    There should be a file attached (Example.xlsm) with a single routine which illustrates what I am trying to do.
    Thankyou
    Attached Files Attached Files

  8. #8
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Passing field name to form to set focus

    What is wrong with the approach in your example workbook? Perhaps a little clunky if you have LOTS of controls on the form...

    Other than that I'd give each control on the form a .Tag value equal to the relevant column number, exit the loops when an error is identified then loop through the form controls' .Tag properties until the corresponding one is found and set focus on that.

    Very much a case of preference. Both would require a little effort during setup but I don't think there would be much in the way of a speed advantage over either approach.

    BSB

  9. #9
    Registered User
    Join Date
    04-14-2017
    Location
    Leeds, England
    MS-Off Ver
    2007
    Posts
    5

    Re: Passing field name to form to set focus

    OK, thanks.
    The whole point is that it is extremely clunky because I am dealing with over 30 controls.
    It's probably my fault for not explaining it properly....
    I was imagining a tiny subroutine that recognised a literal that represented the name of the field to be able to set the value of that field itself!
    Then there would be no need for a Select statement at all.

    Into this subroutine I would pass the string representing the name of the textbox eg "txtbox1" and the function would set focus to that field.

    The pseudocode for this subroutine (not real code, 'cos I can't see how!) would be
    Sub AchieveFocus(fieldname_as_string)
    fieldname.setfocus
    End Sub


    So AcheiveFocus("txtbox1") would set the focus to the field txtbox1.

    That is subtly different to the following which is merely a straight setting:

    Sub AchieveFocusBasic(field)
    field.setfocus
    End Sub


    AcheiveFocusBasic txtbox1

    ... which gives the same result but is incapable of being called programmatically for many different fields without quoting the field literally.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Why doesn't set focus set focus on user form?
    By dflak in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-22-2019, 02:24 PM
  2. MailItem Focus Field
    By ldoodle in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 06-07-2018, 09:51 AM
  3. Multipage form set focus
    By wpryan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-02-2015, 07:20 AM
  4. Form field focus
    By Nathan323 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-09-2013, 10:27 PM
  5. moving focus to other field
    By johnyjj2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-27-2011, 08:28 AM
  6. set focus on txt field
    By tkaplan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-27-2005, 06:05 PM
  7. [SOLVED] Form Focus and SelStart
    By Geoff in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-04-2005, 03:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1