Consulting

Results 1 to 6 of 6

Thread: change backcolor field on opening forrm

  1. #1
    VBAX Regular
    Joined
    Jan 2016
    Posts
    35
    Location

    change backcolor field on opening forrm

    Hello,

    In my form there is a textfield. In the property control source the next line:

    =DSum("Bedrag";"tblWareHouse1"). Its a textfield, but it gives me the correct amount I need to see.
    What I would like to know, if I open my form, and the field shows me a positive amount, the backcolor of the field must be green. For a negative amount, the color red.
    The name of the field is txtSaldoWH1.

    I’ve tried to convert the textfield into a several functions as such as: Val() and CDbl() , but nothing seems to work.

    my code until now,
    Private Sub Form_Open(Cancel As Integer)
           
            If Me.txtSaldoWH1.value >= 0 Then
            Me.txtSaldoWH1.BackColor = RGB(0, 255, 0)
            Else
            Me.txtSaldoWH1.BackColor = RGB(255, 0, 0)
            End If
     
    End sub
    Any help please.
    TonC

  2. #2
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    .
    Option Explicit
    
    
    
    
    Sub TextBox1_Change()
        On Error Resume Next
        Select Case TextBox1.Value
            Case Is > 0:
                TextBox1.BackColor = vbGreen
            Case Is < 0:
                TextBox1.BackColor = vbRed
        End Select
    End Sub

  3. #3
    VBAX Regular
    Joined
    Jan 2016
    Posts
    35
    Location
    Hello Logit,

    I tried your partial solution, It gave an error message: Variabel not defined!
    questions, what is the color of the textbox, when I open the form? Where is "Öption Compare Database"?
    "On Error Resume Next" Next to what? Sorry I'am a novice for VBA code, but I'am certain you will come up with a simple solution.

  4. #4
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    Try this.
    Attached Files Attached Files

  5. #5
    VBAX Regular
    Joined
    Jan 2016
    Posts
    35
    Location
    Okay, you send, I Learned.
    You brought me a idea to solve this problem I saw there was a button in the excel sheet. It was working real fine, but not mend for me. So, I did the following in my Access 2016.

    I created another textbox, (like a dummy textbox) with the name (which you choose) I've called my textbox "txtTarget"
    In the properties of that textbox under the tab Data, I put the following.
    Recordsource =0
    Locked = true
    enabled = true

    The properties for the new textbox must be the same as first textbox. (see tab formatting)

    Private Sub Form_Open(Cancel As Integer)
    
            Me.txtTarget.Visible = False
             
            If Me.txtSaldoWH1.Value >= Me.txtTarget.Value Then
                Me.txtSaldoWH1.BackColor = RGB(0, 255, 0)
            Else
                Me.txtSaldoWH1.BackColor = RGB(255, 0, 0)
            End If
    It works like a charm, because you set me on the right track to solve this problem.
    Many thanks,

  6. #6
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    .
    Glad you got it solved.

    I apologize for not noticing you were working with Access. Sometimes it makes a difference when it comes to the code you use.

Tags for this Thread

Posting Permissions

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