Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    on event? (XP, 2000)

    I have a form in which I have several fields. I would like to place a checkbox in the form and have the form operate such that if the checkbox is checked, several other fields and controls will appear. How would I do this? Can't wait for the search function to return - sorry if this has been asked before.

  2. #2
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,623
    Thanks
    3
    Thanked 60 Times in 60 Posts

    Re: on event? (XP, 2000)

    As long as you are working with a form that displays a single record, this is fairly easy. However continuous forms that display multiple records it is almost impossible, and at a minimum requires using some tricks to make the desired fields invisible.

    So for a single record form, you want to run a small VBA routine from the OnCurrent event that checks the condition of the checkbox, and if it is not checked, make the controls for those fields not visible, otherwise make them visible. Some simple aircode would something like:
    <font face="Georgia"><font color=blue>
    If Me!CheckBox Then
    Me!txtField1.Visible = True
    Me!txtField2.Visible = True
    . . .
    Else
    Me!txtField1.Visible = False
    Me!txtField2.Visible = False
    . . .
    End If
    </font color=blue></font face=georgia>
    Of course you would want to add some error handling and so on. Hope this helps.
    Wendell

  3. #3
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: on event? (XP, 2000)

    Thank you Wendell. My intention is to have a form for data entry/display. The data entered for each record will pertain to a single (usually) geographic location. However, there will be times when as many as three other areas need to be entered. What I hoped to accomplish with the checkbox was to prevent the form from showing the additional fields until they were needed. The checkbox is not a field in the table, simply a form control. Given that, do you think I am on the right track or is there a better way to do this. One problem I anticipate is being able to display those additional fields when looking at records previously entered (since the checkbox will not be checked - hence the field do not display). Maybe I should make a field in the table for the checkbox.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: on event? (XP, 2000)

    You can add code in the On Current event of the form to test whether the check box should be ticked for an existing record. Again, it will only work properly in single form, not in a continuous form.

    Private Sub Form_Current()
    Me.chkExtraLocations = Not IsNull(Me.txtLocation2)
    chkExtraLocations_AfterUpdate
    End Sub

    The code checks if the second location txtLocation2 is not empty; if so, the check box chkExtraLocations is ticked, otherwise cleared. Then, the After Update routine for the check box is invoked, to show/hide the text boxes for the additional locations.

  5. #5
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: on event? (XP, 2000)

    I think I understand the concept. What is the "me" used by both Wendell and Hans? Why does one use "Me!" and the other "Me."? I suspect I need to replace "me" with something else... but what? the form name?

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: on event? (XP, 2000)

    In code behind a form or report, you can refer to this form or report as "Me". By Me. you indicate a property of the form/report, and by Me! a control on the form/report (to be more precise, a member of the Controls collection). Since the Controls collection is the default property of a form/report, Me.txtSomething and Me!txtSomething are both valid ways to refer to a text box named txtSomething.

    If you want to refer to a control on another open form than the one running the code, you can use Forms!frmMyForm!txtSomething or Forms!frmMyForm.txtSomething.

  7. #7
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: on event? (XP, 2000)

    Okay, I give up- where would I find the oncurrent event? Looking in the properties, I see everything EXCEPT on current.

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: on event? (XP, 2000)

    The On Current event is the first event in the Event tab of the Properties window. Make sure that you are looking at the properties for the form as a whole. See screenshot.
    Attached Images Attached Images
    • File Type: png x.PNG (9.7 KB, 0 views)

  9. #9
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: on event? (XP, 2000)

    Thnaks, now I see it. I was looking at the properties of the checkbox and the other related fields. When I enter the code, can I simply copy/paste into thr property oncurrent field?

    Also, can I have more than one procedure for oncurrent? For axample, I would like to use your procedure to check status of the fields and also use Wendell's procedure to display/hide the fields based on the checkbox status.

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: on event? (XP, 2000)

    No, you shouldn't type the code in the Properties window.

    You must select Event Procedure from the dropdown list in the On Current event.
    Next, click the builder button (the three dots ... to the right of the dropdown arrow.)
    This will take you to the Visual Basic Editor, with the first and last line of the procedure already created for you:

    Private Sub Form_Current()

    End Sub

    Type or paste the code you need between Sub ... and End Sub. There is only one On Current event procedure for the form, but it can contain as many lines of code as you need, and you can call other procedures in it.

  11. #11
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: on event? (XP, 2000)

    I am still struggling to control the visible=yes/no based on the condition of the check box control. I have 22 controls (fields) (and 22 labels) that I need to be able to:

    1) make visible if any of them contain data
    2) toggle visible/not visible with the checkbox
    3) toggle the checkbox on if any of those fields contain data
    4) toggle the checkbox off if they do not.

    I have having a lot of trouble ( no success) making any of the code work.

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: on event? (XP, 2000)

    Perhaps I don't read it correctly, but 2) seems incompatible with the others. And although appropriate to the season, I don't like the idea of a form like a Christmas tree, with controls blinking on and off.

    Perhaps we could help if you would explain more clearly what you want to accomplish.

  13. #13
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: on event? (XP, 2000)

    Well.... it is a holiday.

    Seriously, This is a subform. It will have data input about geological sites. Each site is a record. Also, each site will definitely have one state and county as the location. However, in some cases there will be as many as four additional states and potentially five counties within each state to be entered. My objective here is to present a form that is usually uncluttered with the additional fields but when the multiple location checkbox is checked, the additional fields will appear so the information can be entered therein.

    I was hoping that the user could check the checkbox and the fields will appear. Then, if the user made a mistake, he could simply uncheck the box and the fields would disappear. Also, when the form opens, the fields should appear (all of them) if any of the additional locations contains data.

    Is this a better explanation?

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: on event? (XP, 2000)

    Thanks, your description makes things a lot clearer. You shouldn't store all this information in one table.

    Keep the general information about the site in the main table, and put the information about the location(s) in another table, linked to the main table by some kind of SiteID.

    This will result in a main form containing the general site info, and a continuous subform containing the location(s).

  15. #15
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: on event? (XP, 2000)

    Okay, that makes sense to me from a DB design perspective but how do I present it in a form? My intention was for the user to not have to re enter the same state for each of the five counties associated with that state (especially given that there may be four states-each with five counties). That is why I had fields like State3, County3A, County3B, etc. If I use a subform (and I will admit I have never used a continuous subform), won't the user have to do exactly that? I lack the imagination to see how it would be presented.

    Below is how I originally envisioned the form - remember all but one state and one county would be hidden until needed.

    One other form consideration is the size... I am trying to keep the entire form visible on the screen without having to use scroll bars.
    Attached Images Attached Images

Page 1 of 2 12 LastLast

Posting Permissions

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