Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2007
    Posts
    6

    Changing Text Box BackColor

    Hey guys, i got a enquiry here, cant figure it out myself.

    I got a textbox field that validates what users input, if the users enters, lets say, less than 10, then the back color of the texfield will turns red, and if the users enter more than 10, it will be reverted back to normal(white background). I wanted the color be be reverted back to normal when the users click to a fresh next record(sort of like clear every fields) and the color will still be red when i previous back the record which fails the validation rule(<10).


    Private Sub textbox_AfterUpdate()

    if textbox < 10 then
    textbox.BackColor = vbRed
    else
    text box.BackColor = vbWhite
    End if

    End Sub

    Currently i am stuck at when users click a fresh new record, the background is still red in the fresh new record.

    Thank you for the patience.

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,079
    Would this do the trick? It may need putting on the On Current event.
    Code:
    If Isnull(textbox.value) Or textbox.value >= 10 Then
     textbox.BackColor = vbWhite
    Else
     textbox.BackColor = vbRed
    End If
    George
    Home | Blog

  3. #3
    Join Date
    Aug 2007
    Posts
    6
    i tried but it still doesn't work or i did it incorrectly.
    Alright here is the exact code

    I have 2 declarations at Module

    Codes:
    Public Const LowerLimit As Integer = 90
    Public Const UpperLimit As Integer = 100

    'So it means any point of time, my values for the textbox must be from 90 to 100. if not the backcolor will change to red.

    At Form Page Level

    Codes:

    Private Sub Textbox_AfterUpdate()

    if Textbox < LowerLimit or Textbox > Upperlimit
    Textbox.BackColor = vbRed
    Else
    Textbox.BackColor = vbWhite
    End If
    End Sub

    So what do i put in form_current as suggested? Thanks again.

  4. #4
    Join Date
    Jun 2007
    Location
    Maitland NSW,Australia
    Posts
    388
    How do you navigate between the records?

    On the form have two command buttons

    One button to add a new record, the other to go to the previous record. Turn off the navigations buttons down at the bottom of the form so that the user can only use the command buttons for navigation.

    The Add new record button your code should look like this

    Private Sub new_record_Click()
    On Error GoTo Err_new_record_Click

    DoCmd.GoToRecord , , acNewRec
    test_field.BackColor = vbWhite

    Exit_new_record_Click:
    Exit Sub

    Err_new_record_Click:
    MsgBox Err.Description
    Resume Exit_new_record_Click

    End Sub


    The Previous record coding will take you to the previous record

    Now the On Current event will need to have the following coding
    Private Sub Form_Current()
    If (test_field < LowerLimit) Or (test_field > UpperLimit) Then
    test_field.BackColor = vbRed
    Else
    test_field.BackColor = vbWhite
    End If

    End Sub

    Your textbox coding will remain the same, I have added some brackets to group the conditions, just a safety feature that I use.

    Private Sub Textbox_AfterUpdate()

    if (Textbox < LowerLimit) or (Textbox > Upperlimit) then
    Textbox.BackColor = vbRed
    Else
    Textbox.BackColor = vbWhite
    End If
    End Sub

  5. #5
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,536
    Are we dealing with Access 97 here? I don't see anything here to that effect. If not, why not simply use Conditional Formatting?
    Hope this helps!

    The Devil's in the Details!!

    All posts/responses based on Access 2003/2007

  6. #6
    Join Date
    Aug 2007
    Posts
    6
    I tried but it cant work. I am using Access 2003 here.
    Last edited by DbFanatics; 08-19-07 at 08:28.

  7. #7
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,536
    If you're using ACC2003 then use the built in Conditional Formatting!

    In Design View select the textbox.

    On the Menu Bar goto Format - Conditional Formatting

    For Condition1:
    Field Value Is Less Than 90
    Click on the bucket icon (BackFill) and click on red.

    Click on Add>>

    For Condition2:
    Field Value Is Greater Than 100
    Click on the bucket (BackFill) and click on red.

    Click on OK

    You're done!

    Linq
    Hope this helps!

    The Devil's in the Details!!

    All posts/responses based on Access 2003/2007

  8. #8
    Join Date
    Aug 2007
    Posts
    6
    Yes! Thats exactly what i want, i will navigate more on the menu-bar functions. Thank you Missinglinq!!!

  9. #9
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,536
    Glad you got it working!

    Linq
    Hope this helps!

    The Devil's in the Details!!

    All posts/responses based on Access 2003/2007

Posting Permissions

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