Results 1 to 4 of 4

Thread: Loop thru controls in form - what's wrong?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Loop thru controls in form - what's wrong?

    This is an Access 97 db. I want to loop through the controls in the detail section of a form only, and have the code below. Access throws an Application or Object defined error at the indicated spot. If I highlight "ctl", Access gives the value that's in the textbox which is the first control, e.g. the date. I'm basically trying to make sure the user has entered at least some data in the detail area of the form before it's saved. Thanks.
    VB Code:
    1. Private Function CheckForData() As Boolean
    2.     Dim ctl As Control
    3.     Dim i As Integer
    4.    
    5.     For Each ctl In Me.Controls   <--- highlighting ctl = value in that control
    6.         [COLOR=Red][B]i = Forms![Survey Review].ctl.Section   [/B] [/COLOR] <--- error here
    7.         If i = acDetail Then
    8.         If TypeOf ctl Is CheckBox Then
    9.             If ctl.Value = True Then
    10.                 CheckForData = True
    11.                 Exit Function
    12.             End If
    13.         ElseIf TypeOf ctl Is TextBox Then
    14.             If Len(ctl.Text) > 0 Then
    15.                 CheckForData = True
    16.                 Exit Function
    17.             End If
    18.         End If
    19.         End If
    20.     Next
    21. End Function
    Tengo mas preguntas que contestas

  2. #2
    Junior Member llkhoutx's Avatar
    Join Date
    Jan 2005
    Location
    Houston, TX
    Posts
    19

    Re: Loop thru controls in form - what's wrong?

    A control does not have a section property.

    Your function does not give you

    user has entered at least some data in the detail area of the form before it's saved
    Your function misses bound combo box value.

    If Me.Dirty = True then - data has changed in a form.

    VBA uses acCheckBox and acTextBox, not CheckBox nd TextBox.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Loop thru controls in form - what's wrong?

    Ok, I'll try that, thanks. Help for Section Property contains this, though:
    You can use the Section property to identify a section of a form or report and provide access to the properties
    of that section. You can also identify controls by the section of a form or report where the control appears....

    For controls, you can use the Section property to determine which section of a form or report a control is in.
    The following example uses the Section
    property to determine which section contains the CustomerID control.

    Dim intSectionNumber As Integer
    intSectionNumber = Forms!Customers!CustomerID.Section
    And I still don't know why ctl is picking up the value in the control, rather than the control itself.
    Me.Dirty doesn't do quite what I want. Initial data is entered on a different form, and this form is used for reviewing a/o editing. But it's possible that the user will make no changes, so the Dirty property never gets set. That's why I want to iterate through the controls in the Detail section (the page header section other data - name, department, etc, that the user doesn't normally enter or edit). No particular control has to have data, but there has to be some data before they save it.
    Last edited by salvelinus; Mar 21st, 2005 at 11:23 AM.
    Tengo mas preguntas que contestas

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Loop thru controls in form - what's wrong?

    me.controls - all controls on form.

    I;m not sure here but I think the sections come before the controls, so you are looking at only one section. ie

    me.sections(0).controls(<loopnum>)

    Its been a while since I bothered having sections on forms... so this may be wrong

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

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



Click Here to Expand Forum to Full Width