Results 1 to 13 of 13
  1. #1
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Toggle Button (A2000)

    On my form I have a series of 20 toggle buttons. When one is pressed the others are set to false, ensuring more than one selection cannot be made.
    My code is a bit naive and wondered if there was any way to simplify this.
    Basically, when one is pressed, the code looks at all the other toggles and sets them to false if they are true.

    Private Sub tglUnit01_Click()
    Me.txtVehicleID = tglUnit01.Caption
    Me.tglUnit02 = False
    Me.tglUnit03 = False
    Me.tglUnit04 = False
    Me.tglUnit05 = False
    Me.tglUnit06 = False
    Me.tglUnit07 = False
    Me.tglUnit08 = False
    Me.tglUnit09 = False
    Me.tglUnit10 = False
    Me.tglUnit11 = False
    Me.tglUnit12 = False
    Me.tglUnit13 = False
    Me.tglUnit14 = False
    Me.tglUnit15 = False
    Me.tglUnit16 = False
    Me.tglUnit17 = False
    Me.tglUnit18 = False
    Me.tglUnit19 = False
    Me.tglUnit20 = False
    End Sub

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

    Re: Toggle Button (A2000)

    If you have a series of toggle buttons (or radio buttons) that should act as a group, you must put them in an option group. The easiest way to do this is to place a group box on the form with the Control Wizard activated (the magic wand buttton on the Control Toolbox). The Wizard will take care of all the details.

    If you want to convert the existing toggle buttons to a group, that is possible too, although it takes a bit of work:
    1. <LI>Place a group box on the form without the Control Wizard activated (or cancel the Wizard immediately).
      <LI>Select the toggle buttons you want to group.
      <LI>Cut the selected toggle buttons to the clipboard (Edit/Cut or Ctrl+X or the Cut toolbar button).
      <LI>Select the group box.
      <LI>Paste the toggle buttons (Edit/Paste or Ctrl+V or the Paste toolbar button).
      <LI>Deselect the toggle buttons, then select them one by one and set the Option Value property to 1 for tglUnit01, 2 for tglUnit02, etc.
      <LI>If you want a specific toggle button to be on (down) by default, set the Default Value property of the group box to the Option Value of that toggle button.
    You will have accomplished two things:
    <UL><LI>Only one toggle button can be on (down) at a time. So there is no need for code to switch the other buttons off when you click one of them.
    <LI>To check which toggle button is down, you don't have to check each one; in runtime, the Value propertyof the group box is the Option Value of the selected toggle button.[/list]So you see, an option group (group box) does a lot of the work for you.

  3. #3
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle Button (A2000)

    Hans.
    I see the logistics behind this but my form gives an error saying Access cannot add any more controls to this form when I try to paste.
    Thanks anyway

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

    Re: Toggle Button (A2000)

    Dave,

    There is a maximum to the number of controls you can add to a form (or report) during its lifetime: the limit is 754 controls, see for example ACC2000: Errors Converting or Importing with 754 or More Controls. Note: this is not the number of controls that can exist simultaneously on a form, but the number of controls that can ever be created on it! So your form has probably reached this limit.

    If that is the case, I guess the only thing you can do is create a new blank form, and then copy the properties, controls and code from your present form to the new form. If you have a really large number of controls on your form, consider putting them in several subforms.

  5. #5
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle Button (A2000)

    Hans
    See Attached.
    This is the form in question.
    Loads of controls.
    Attached Files Attached Files

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

    Re: Toggle Button (A2000)

    Hi Dave,

    Yes, I think you are near the limit of 754 controls on one form. There are certainly more than 700 visible controls on your form; invisible controls and sections count too...

    Perhaps the following works: create a new form. Copy all controls from your existing form except for tglUnit01 ... tglUnit20 and paste them to the new form. Place a group box on the new form. Copy tglUnit01 ... tglUnit02 and paste them into the group box on the new form.

    If that doesn't work either, you'll probably have to reduce the number of rows in the grid - each row contains 31+2=33 controls, so for instance 15 rows instead of 20 saves 5x33 = 165 controls. You'll still have to create a new form, because of the limitation of 754 controls during the lifetime of a form.

  7. #7
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle Button (A2000)

    I've caused my own problem here.
    the form is only meant to display 10 grid lines, I spent all Saturday morning updating this to show 20.
    We use this system currently on paper format to organise our courtesy car fleet which could be any thing from 28 to 70 vehicles.
    So the need for 20 was less than adequate but the most I could fit on.
    Perhaps it would be better to stick to my original code.
    The code behind this form is far too complicated (for me anyway) to mess with.

  8. #8
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle Button (A2000)

    Just going away from this problem for a minute.
    On the form as attached (prev), I wanted to lookup from tblCourtesyCar the UnitID value of txtVehicleID.
    in the tblCourtesyCar each vehicle has a UnitID and a Unit field.
    So
    Unitid = 1 Unit = KM51KTA
    Unitid =2 Unit = DV01NHY

    When a toggle is pressed, the caption value is pushed to txtVehicleID (Unbound) :- Me.txtVehicleID = tglUnit01.Caption

    So to get the numerical UnitID, I placed an unbound text named txtUnitID on the form with the following dlookup.

    = DLookup("UnitID", "tblCourtesyCar", "Unit=" & [Forms]![frmCourtesyCarPlanner]![txtVehicleID])

    All I get is #Error? or #Name?

    I thought this was because txtVehicleID didn't have a text value until a toggle was pressed, so doing a requery would do the trick.
    It doesn't.

    Can you see why this DLookup won't work.

    The current form is frmCourtesyCarPlanner

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

    Re: Toggle Button (A2000)

    Since Unit is a text field, you must enclose the value in quotes:

    =DLookup("UnitID", "tblCourtesyCar", "Unit=" & Chr(34) & [Forms]![frmCourtesyCarPlanner]![txtVehicleID] & Chr(34))

    (34 is the ASCII code for a double quote)

  10. #10
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle Button (A2000)

    Spot on as ever .

    Thanks

  11. #11
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle Button (A2000)

    Because the 3 text's are unbound and used only temporarily,does the following require the fields to be associated with a table.?
    This returns 0 rows to update, or does the CHr(34) come in to rule again.

    Dim strSQL As String
    strSQL = "INSERT INTO tblPeriod ( UnitID, FromDate, ThruDate )SELECT tblPeriod.UnitID, tblPeriod.FromDate, tblPeriod.ThruDate FROM tblPeriod WHERE (((tblPeriod.UnitID)=[forms]![frmCourtesyCarPlanner]![txtUnitID])AND((tblPeriod.FromDate)=[forms]![frmCourtesyCarPlanner]![txtDateFrom]) AND ((tblPeriod.ThruDate)=[forms]![frmCourtesyCarPlanner]![txtDateThru]));"
    DoCmd.RunSQL strSQL
    Attached Files Attached Files

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

    Re: Toggle Button (A2000)

    Dave, this SQL statement essentially says: select a record from tblPeriod that satisfies certain criteria and then insert that record into tblPeriod. I suspect that that is not what you intend. I would guess that you want to insert a record with the values for UnitID, FromDate and ThruDate taken from the form. If that is correct, try this:

    strSQL = "INSERT INTO tblPeriod ( UnitID, FromDate, ThruDate ) VALUES ( [Forms]![frmCourtesyCarPlanner]![txtUnitID], [Forms]![frmCourtesyCarPlanner]![txtDateFrom], [Forms]![frmCourtesyCarPlanner]![txtDateThru] )"

  13. #13
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle Button (A2000)

    Thanks Again Hans

Posting Permissions

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