Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Jan 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    enabling command buttons on a form (2000)

    Question! Is it possible to to enable a command button on a form depending on what appears in a field on the same form? If so how do i do this? I have set up a database for booking accomodation which prints fax booking confirmations in reports. I have several reports with different payment details which you print from a command button. the report is not based on a table or query it prints from the data that is on view EG=[FORMS]![BOOKINGS]![HOTELNAME]) What i need to do is enable the button for booking a travel lodge if the [hotelname] field says "travel lodge" and disable all other buttons and so on this is so the users cannot make a mistake by sending the wong payment details to any hotel. If you see what i mean.......... <img src=/S/confused.gif border=0 alt=confused width=15 height=20>

  2. #2
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Sint Niklaas, Belgium
    Posts
    2,778
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: enabling command buttons on a form (2000)

    Steven,

    In the On Current event of the form insert this code :
    <pre>If Me.hotelname = "travel lodge" Then
    Me.MyButton.Enabled = False
    Else
    Me.MyButton.Enabled = True
    EndIf</pre>


    Replace hotelname with the name of the textbox containing the hotelname field and MyButton with the name of your command button.
    Francois

  3. #3
    New Lounger
    Join Date
    Jan 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: enabling command buttons on a form (2000)

    Thanx Francois that worked great now how do i code this for 5 command buttons on the same form with different criteria? ie [hotel name] "travel Inn", "Premier Lodge," Guest House" etc...........

  4. #4
    3 Star Lounger
    Join Date
    Feb 2001
    Posts
    369
    Thanks
    2
    Thanked 1 Time in 1 Post

    Re: enabling command buttons on a form (2000)

    You've a number of choices:

    1) Repeat above code 5 times - could also do with select case [hotel name], though if you wanted to make sure that inappropriate buttons were disabled, you'd either have to have a bit of code to disable all the buttons to begin with, or you'd have to disable 4 buttons and enable 1 in each case, so you might not save much typing.

    2) Have one button that does different things depending on the value of [hotel name]

    3) name your buttons after the contents of the field - eg btnTravelLodge, btnHolidayInn, and then do this

    For each ctl in Me.controls
    If ctl.ControlType=acCmdButton then
    If right(ctl.Name,Len(ctl.Name)-3)=Me![hotel name] Then ctl.Enabled=True Else ctl.Enabled=False
    End If
    Next ctl

    Of course this only works if the values of [hotel name] don't have spaces in them, or [hotel name] is a combo or list box with the bound value without spaces, and if you're form is stacked full of controls, it might be quite inefficient, and if you change any of your values you have to remember to change the name of the command button and vise versa, but it appeals to lazy people like me.

    4) Put the possible values of [hotel name] and the names of the corresponding buttons in an array, or, if you're lazy, two variant arrays (less typing to define)

    Dim vArray1 As Variant, vArray2 As Variant, counter As Byte
    vArray1=Array("Travel lodge", "Holiday Inn",.......)
    vArray2=Array("btnTL","btnHI",.....)
    For counter=0 to UBound(vArray2)
    If Me![Hotel Name]=vArray1(counter) Then Me.Controls(vArray2(counter)).Enabled=True Else Me.Controls(vArray2(counter)).Enabled=False
    Next counter

    This has the advantage that you can make any changes easily and in one place - but still have to keep the information in the arrays current and matching with one another.

Posting Permissions

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