Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Oct 2002
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Command button on a form (Access 97)

    I have a command button on a form which runs an append query. I would like the users to click on the command button only once. How would I modify in the onclick even procedure that would either hide the button or disable it once it has been clicked on?

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

    Re: Command button on a form (Access 97)

    You must set the focus to another control before disabling or hiding the command button, otherwise you'll get an error. Let's say that your command button is named cmdRunQuery and that you want to set focus to a text box named txtData after clicking the command button. The OnClick handler for your button could look like this:

    Private Sub cmdRunQuery_Click
    ' command(s) to run append query go here
    ' ...
    ' Set focus to another control before disabling/hiding command button
    [txtData].SetFocus
    ' Use one of the following statements only
    [cmdRunQuery].Enabled = False
    [cmdRunQuery].Visible = False
    End Sub

  3. #3
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Command button on a form (Access 97)

    You can make the control invisible (set its visible property = false) or disable it (set its enabled property = false). However, you have to first move the focus off this control to some other control. Let's call this command button cmdQuery, in its Click event (after your code to run the query) you would put:
    DoCmd.GotoControl "cmdClose" 'substituate some other control name on your form.
    cmdQuery.Enabled = False

    It is also a good idea to put Cancel=True in your command button's Double Click event.
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  4. #4
    New Lounger
    Join Date
    Oct 2002
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Command button on a form (Access 97)

    Thanks! This works...but I when I reopen the form the button there. I would like to have this button disabled or hidden forever once it has been click on.

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

    Re: Command button on a form (Access 97)

    In that case, you must store information on whether the button should be active somewhere in the database, and read this information when opening the form.

    For example, you could create a table with just one field ButtonEnabled (type Yes/No). Create one record in the table and set the initial value of ButtonEnabled to True (so that the button will be enabled the first time the form is opened). For illustration purposes, I'll name the table tblSettings.

    In the OnLoad or OnOpen event of the form, do something like this:

    Dim blnEnable As Boolean
    blnEnable = DLookup("ButtonEnabled", "tblSettings")
    Me.cmdRunQuery.Enabled = blnEnable

    In the OnClick event handler of the command button, set ButtonEnabled to false, so that from then on, the button will always be disabled.

    Dim strSQL As String
    strSQL = "UPDATE tblSettings SET ButtonEnabled = False"
    CurrentDb.Execute strSQL

    If you're experimenting with this, you can "reset" the status by setting the value of ButtonEnabled to true directly in the table.

Posting Permissions

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