Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2005
    Location
    Delaware
    Posts
    139

    Unanswered: Get current value of control on form

    Hello... I'm using the following code in an attempt to notify users of required fields. The code seems to be not recognizing that all controls contain user data. In an attempt to debug, I placed debut.print statements in the code to see the actual value entered into the control. However its not working. When the code is run I receive an "Object doesn't support this property or method" error on the first Debug.Print line. The other debug.print lines return nothing either. Can any one point me in the right direction?

    Thanks...

    For Each ctl In Me.Detail.Controls
    Debug.Print ctl.Name & ":" & ctl.ControlType & "" & ctl.value
    Select Case ctl.ControlType
    Case acTextBox, acComboBox
    If ctl.value = "" Or IsNull(ctl.value) = True Then
    Debug.Print ctl.value
    GoTo CheckToQuitWithoutSave
    End If
    Case acListBox
    If ctl.value = "" Or IsNull(ctl.value) = True Then
    Debug.Print ctl.value
    GoTo CheckToQuitWithoutSave
    End If
    Case acCheckBox
    If ctl.value = "" Or IsNull(ctl.value) = True Then
    Debug.Print ctl.value
    GoTo CheckToQuitWithoutSave
    End If

    End Select

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    do all controls support the value property, I thought some only supported the text.
    however you could try and see if your code will work if you remove the value all together and just specify the control. I seem to remember that VB & VBA have a default property which is the text or vlaue.
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Aug 2005
    Location
    Delaware
    Posts
    139
    when I remove the [& ":" & ctl.value], the code produces the control name and type without error. Additionally, the [Debug.Print ctl.value] within the indicidual Case statements produces nothing at all. I thought placing the debug.print statement there would overcome any conflicts between different control types.

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    so set a breakpoint on the debug statement and then in the intermediate window examine the default value of ctl


    ?<expression> evaluates the expression and displays the result
    <expression> assigns the value of the expression



    so
    ?ctl
    will display the default value for ctl
    myvariable = 42
    will set myvariable to 42, assumign that you had a variable called myvariable that value would be available to the rest of the program
    Google

    ACC&#58; Tips for Debugging Visual Basic for Applications Code
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    One possibility to know if a control has a value property is:
    Code:
    If Eval(ctl.ControlType & " IN ( 105, 106, 107, 109, 122, 123 )") = True Then   ' Control has a Value property
    Have a nice day!

  6. #6
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Quote Originally Posted by Larryg View Post
    ...The code seems to be not recognizing that all controls contain user data...
    Unless your form is very unusual, all controls probably don't contain user data. Your "Object doesn't support this property or method"is probably being generating because Labels do not have Value Properties!

    You need to either exclude Labels from your code or specifically include only controls that do have Value Properties.

    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  7. #7
    Join Date
    Aug 2005
    Location
    Delaware
    Posts
    139
    Thanks everyone ! Unless your form is very unusual, all controls probably don't contain user data... I think this may be the problem. I do have other command buttons and labels on the form, so I guess the code is seeing them as blank and firing the message to the user about required data. Thanks... I guess I will have to list the specific controls I want checked. I have several user input forms that need this check, and just thought there was a way to make the code to work for each user form (global if you will). I went the route of a check on mouse down, however discovered that only worked if the user actually landed on the field. If they clicked over it, no check was processed.

    Larry

  8. #8
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    One solution that can be systematically applied whatever the form can be consists in gathering all controls that have a Value property into a Collection, then using the collection to refer to such a control. In every form module:
    Code:
    Option Compare Database
    Option Explicit
    
    Private m_colControls As Collection
    
    Private Sub Form_Open(Cancel As Integer)
    
        Dim ctl As Control
        
        Set m_colControls = New Collection
        For Each ctl In Me.Controls
            If Eval(ctl.ControlType & " IN ( 105, 106, 107, 109, 122, 123 )") = True Then   ' Control has a Value property
                 m_colControls.Add ctl, ctl.Name
            End If
        Next ctl
    
    End Sub
    Then, you can refer to a control with a value property through the collection:
    Code:
    Function EnumControlValues()
    
        Dim i As Integer
        
        For i = 1 To m_colControls.Count
            Debug.Print m_colControls(i).Name, m_colControls(i).Value
        Next
    
    End Function
    
    Function GetText1Value()
    
        GetText1Value = m_colControls("Text1").Value
        
    End Function
    Have a nice day!

  9. #9
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Quote Originally Posted by Larryg View Post
    ...I do have other command buttons and labels on the form, so I guess the code is seeing them as blank and firing the message to the user about required data.
    That's not correct. It's not the fact that Access sees them as "blank", i.e. not having data, it's the fact that you're trying to access their Value Property, which they do not have, hence the "Object doesn't support this property..." error. You can't check something that doesn't exist.
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Posting Permissions

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