Results 1 to 15 of 15
  1. #1
    New Lounger
    Join Date
    Oct 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding Text Boxes (2000)

    I have five text boxes, txtB1 through txtB5. I want to input four values in B1throughB4 and display the total in B5. What is the best way of doing it? When I say txtB1 + txtB2.... +txtB4 inside the control of txtB5, it just concatenates all the values together and displays them in the box.

  2. #2
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Adding Text Boxes (2000)

    Access is treating the content of your text boxes as text rather than as numbers.

    Change the format property of each text box to a numerical format.

    Post back if you don't know how to do that.
    Regards
    John



  3. #3
    2 Star Lounger
    Join Date
    Oct 2004
    Location
    Evansville, Indiana, USA
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding Text Boxes (2000)

    That works. Thanks.
    Now I need help with the input validation. I want them to be able to input any two digit integer or a number with only one decimal place (.5 is the only decimal they can input). For example they can input 3, 4, 5 etc. or 1.5, 7.5, 10.5. Here is what I have now, !99.9;9;_. This doesn't work because when I put just 1 integer, it defaults to the right of the decimal place.

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

    Re: Adding Text Boxes (2000)

    An input mask is probably not the ideal tool for this. You won;t be able to enforce .5 as the only allowed decimal fraction through an input mask. It might be better to use the Before Update event of the text box to validate the value entered by the user, and issue a warning if it doesn't fit the rules. For example, for a text box named txtNumber:

    Private Sub txtNumber_BeforeUpdate(Cancel As Integer)
    If (Me.txtNumber < 0) Or (Me.txtNumber >= 100) Or (2 * Me.txtNumber - Int(2 * Me.txtNumber) <> 0) Then
    MsgBox "Invalid entry", vbExclamation
    Cancel = True
    End If
    End Sub

    You can adapt this to your needs.

  5. #5
    2 Star Lounger
    Join Date
    Oct 2004
    Location
    Evansville, Indiana, USA
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding Text Boxes (2000)

    That works great! Is there a way I can make that a generic procedure that I can call by several textboxes, or do I need to generate that code for every box?

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

    Re: Adding Text Boxes (2000)

    Put this function in a module.
    <pre>Function RoundToHalf(ToRound As Single) As Boolean
    If (ToRound < 0) Or (ToRound >= 100) Or (2 * ToRound - Int(2 * ToRound) <> 0) Then
    MsgBox "Invalid entry", vbExclamation
    RoundToHalf = False
    Else
    RoundToHalf = True
    End If
    End Function</pre>

    In the before update of the textbox use :<pre>Private Sub txtNumber _BeforeUpdate(Cancel As Integer)
    If RoundToHalf(Me.txtNumber ) = False Then
    Cancel = True
    End If
    End Sub</pre>

    Francois

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

    Re: Adding Text Boxes (2000)

    You could do something like this: create a function in the form module:

    Private Function BadInput(ctl As Control) As Boolean
    If (ctl.Value < 0) Or (ctl.Value >= 100) Or (2 * ctl.Value - Int(2 * ctl.Value) <> 0) Then
    MsgBox "Invalid entry", vbExclamation
    BadInput = True
    End If
    End Sub

    Call this function from the Before Update event procedure for each of the controls to which it applies:

    Private Sub txtSomething_BeforeUpdate(Cancel As Integer)
    Cancel = BadInput(Me.txtSomething)
    End Sub

    Private Sub txtOther_BeforeUpdate(Cancel As Integer)
    Cancel = BadInput(Me.txtOther)
    End Sub

    etc.

  8. #8
    2 Star Lounger
    Join Date
    Oct 2004
    Location
    Evansville, Indiana, USA
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding Text Boxes (2000)

    Everything works great!!
    After I get the input, I add all the text boxes and display the answer in another text box. ( = [txtB1] + [txtB2] + [txtB3] + [txtB4]). That works as long as there is something in the all the text boxes. I am assuming this is because there is a null value in one of the text boxes. Can I prevent this or do I have to set the default value to zero?

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

    Re: Adding Text Boxes (2000)

    Use
    = nz([txtB1]) + nz([txtB2]) + nz([txtB3]) + nz([txtB4])
    Francois

  10. #10
    2 Star Lounger
    Join Date
    Oct 2004
    Location
    Evansville, Indiana, USA
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding Text Boxes (2000)

    How do I store the values in txtB1 to txtB5 in a table?

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

    Re: Adding Text Boxes (2000)

    Are you saying that you want to store the total of the fields in a table ?
    Why should you do that ?
    You can always get the total in a query.
    Francois

  12. #12
    2 Star Lounger
    Join Date
    Oct 2004
    Location
    Evansville, Indiana, USA
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding Text Boxes (2000)

    This is a sample of the larger picture. I am going to have around one hundred text boxes that the user will input values into. I want those values to be put in a table so that way when the form is opened later, the values entered before will appear, and the form can be updated.

  13. #13
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Adding Text Boxes (2000)

    In that case, just capture each of the values entered and bind the form to that. Then you can recalculate the total as needed. It is very bad design to store a total because it can so easily get out of sync with the values it is based on.
    Charlotte

  14. #14
    2 Star Lounger
    Join Date
    Oct 2004
    Location
    Evansville, Indiana, USA
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Adding Text Boxes (2000)

    I don't mean to sound stupid... but how to I capture each of the values entered and bind the form to that?

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

    Re: Adding Text Boxes (2000)

    In the record source of the form, enter the table name you want to use.
    In the control source of the textboxes enter the name of the field you want to use to store the value of that textbox.
    Francois

Posting Permissions

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