Consulting

Results 1 to 2 of 2

Thread: Form to gather results of coin flips

  1. #1
    VBAX Newbie
    Joined
    Dec 2013
    Posts
    2
    Location

    Form to gather results of coin flips

    Hi

    I currently have a form in access that I can manually enter the results of 50 coin flips into. I want to be able to have a button on the form that, when clicked, will display the percentage that resulted in heads. I am trying to make the results show up in a text box or a message box. I am not great with VBA so help with the code would be appreciated.

    Thanks!

  2. #2
    VBAX Regular Lister's Avatar
    Joined
    Aug 2004
    Location
    Napier, New Zealand
    Posts
    8
    Location
    So you have two field where you store, EG: "Heads" = 102, "Tails" = 86, and you want to show on the form 54% Heads or 46% Tails, or both?

    I'm going to assume this is about right, for the example below I'm going to call the Heads field txtHeads and Tails txtTails

    If this is the case, create a new field call it txtPercentHeads and add it to your form.
    In the property for the new field you need to set the Format to percentage, then enter a bit of code in the "Control Source"... such as.

    =txtHeads / sum([txtHeads]+[txtTails])

    I hope that makes sense, now all you have to do is make it refresh each time you enter a value.
    Assuming you are a beginner, I'm going to get you to add a button (you could add this to the code behind the text boxes for Heads and Tails, but I think you will understand it better with a button)

    Create a button, go to the properties pain in design view for the button, select the "OnClick" event from the "Event" tab in the property pain.

    Click the Epenthesis "..." at the right of the On Click Even and the VB code editor will open.
    It will have all the stuff already written for you, just need the bit of code for refresh the text box txtPercentHeads, type...

    Me.txtPercentHeads.Requery

    So your code for the button should look like.....

    Private Sub Command2_Click()
    On Error GoTo Err_Command2_Click
    
    Me.txtPercentHeads.Requery
    
    Exit_Command2_Click:
        Exit Sub
    Err_Command2_Click:
        MsgBox Err.Description
        Resume Exit_Command2_Click
        
    End Sub
    Give that a go and let us know how you get on. Or tell us a bit more about what you are trying to do.
    Good luck mate.

Posting Permissions

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