Results 1 to 13 of 13
  1. #1
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Dear Loungers,

    I'd am writing a generic macro that would, amongst other things, go to the first avialable field on a form. So I don't want to name the specific field, and if this is possible what happens if the first field is not enabled?

    Thank you.......... liz

  2. #2
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,623
    Thanks
    3
    Thanked 60 Times in 60 Posts
    A form has something called a tab order that determines the order of fields, and when the form is opened the first field with the tab stop property set to 'Yes' will have focus. Note that you can set the tab order in form design mode. However the procedure for doing that varies depending on the version of Access you are using, so let us know which version you are using. Also, under what circumstances do you want the generic macro to be executed?
    Wendell

  3. #3
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Dear Wendel,

    Yes, I am used to tab order and hoped that I would be able to say the equivalent of GoTo Control where TabOrder = 0. There are a cople of places where I would use this the most obvious is the macro that will allow the user to discard the current record and go to a new one, in which case I want the cursor to be in the first available field. I don't want to write this for every form, hence the generic requirement.

    thanks......... liz

  4. #4
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,623
    Thanks
    3
    Thanked 60 Times in 60 Posts
    By discard the current record, do you meant to not save it, or to simply move to a new record? If it's the latter, moving to a new record will automatically put the cursor in the control that is first in the tab-order. Is your form bound to data, or are you trying to create an unbound form?
    Wendell

  5. #5
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Wendell,

    In this case they don't want the changes they have applied - so the record will remain as is - and want a new record... I know it will sometimes go to the first field (i.e. tab-index =0 ) will it do that under all circumstances, I thought that sometimes the cursor would stay in the last edited or selected control? And the form is bound.

    thank you.......liz

  6. #6
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,623
    Thanks
    3
    Thanked 60 Times in 60 Posts
    OK - I may have mislead you in terms of the New Record. I was referring to adding a new record from scratch rather than going from one existing record to another. If it's the latter scenario, if you move from record to record using the navigationi controls at the bottom of a form, it will stay in the current selected control - at least it does with a continuous form. What mechanism is being used to go to a new record?
    Wendell

  7. #7
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts
    Quote Originally Posted by lizat View Post
    Dear Loungers,

    I'd am writing a generic macro that would, amongst other things, go to the first avialable field on a form. So I don't want to name the specific field, and if this is possible what happens if the first field is not enabled?

    Thank you.......... liz
    You would have to cycle through all the controls on a form to find the control (which is in the Detail section) that has a TabIndex value = 0. But if this control is not enabled, you would get an error when you tried to set focus to it. Of course, you could check the enabled property and start all over again looking for the control with TabIndex = 1.
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  8. #8
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Wendell et al,

    The other scenario that this applies to is when the user has selected to cancel a save & exit so will be returned to the form, I would like to place him at the first sensible field. So I imagines some way of addressing the controls using a subscript something like this "Control(0)". Is this possible somehow? The idea will be applied everywhere so it needs to be generic.

    thanks........... liz

  9. #9
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts
    Quote Originally Posted by lizat View Post
    Wendell et al,

    The other scenario that this applies to is when the user has selected to cancel a save & exit so will be returned to the form, I would like to place him at the first sensible field. So I imagines some way of addressing the controls using a subscript something like this "Control(0)". Is this possible somehow? The idea will be applied everywhere so it needs to be generic.

    thanks........... liz
    Yes, you can do this. but the "0" in your example doesn't refer to the Tab Index, it is just the first control added to the form. There is just no way to reference a control directly based on its tab index.
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  10. #10
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    371
    Thanked 1,457 Times in 1,326 Posts
    Liz,

    Here is some code that will do what you want, I think. It isn't elegant but it does get the job done.
    If a call to this routine is placed in the Form_Current event it will place the cursor in the first field (based on tab stop numbers) that is both Visible and Enabled when you use the navigation bar controls. You may have to place calls to it in other event handlers as required to meet all your needs.

    Calling:
    Code:
    Option Compare Database
    Option Explicit
    
    Private Sub Form_Current()
       Dim bX As Boolean
      bX = FocusOnFirstEnabledField(Forms(Me.Name))
    End Sub
    Code - can be placed in a standard module for calling from any form:
    Code:
    Function FocusOnFirstEnabledField(frmMyForm As Form) As Boolean
    
      Dim oControl As Control
      Dim iCntr    As Integer
      Dim iCtrlArr() As Integer
      Dim zCtrlName() As String
      
      On Error GoTo ErrorHandler
      
      ReDim iCtrlArr(frmMyForm.Controls.Count, 2)
      ReDim zCtrlName(frmMyForm.Controls.Count)
      
      For Each oControl In frmMyForm.Controls
         With oControl.Properties
              If .Item("Section") = 0 And .Item("TabStop").Value = True Then
                iCtrlArr(.Item("TabIndex").Value, 0) = .Item("Enabled").Value
                iCtrlArr(.Item("TabIndex").Value, 1) = .Item("Visible").Value
                zCtrlName(.Item("TabIndex").Value) = .Item(0)
    NoTabStopProperty:
              End If
         End With
      Next oControl
      
      For iCntr = 0 To UBound(iCtrlArr)
         If iCtrlArr(iCntr, 0) And iCtrlArr(iCntr, 1) Then
           frmMyForm.Controls(zCtrlName(iCntr)).SetFocus
           FocusOnFirstEnabledField = True
           Exit Function
         End If
      Next iCntr
      
    ErrorHandler:
    
       Select Case Err.Number
          Case 2455
              Resume NoTabStopProperty
          Case Else
          'Place default error message here
       End Select
     
      
    End Function        '*** FocusOnFirstEnabledField ***
    I hope you find this useful.

    Edited to incorporate Mark Liquorman's comment and replace code I initially deleted while removing my debugging code!
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  11. #11
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts
    I'm assuming you want to set the focus to the first control in the Detail section. If the form has a form header and/or footer, then it can have multiple controls with a Tab Index property =0.

    You need to check the .Section property of the control before you process it. .Section=0 would be the Detail section, =1 for Header, =2 for footer.
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  12. #12
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    371
    Thanked 1,457 Times in 1,326 Posts
    Mark,

    Nice catch. I've adjusted the original post to take this into consideration.

    Regards.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  13. #13
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Guys,

    Thank you so much for perservering with this for me!!!

    liz

Posting Permissions

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