PDA

View Full Version : [SOLVED:] change backcolor field on opening forrm



TonC
07-28-2017, 11:51 AM
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

Logit
07-28-2017, 12:48 PM
.


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

TonC
07-28-2017, 04:35 PM
Hello Logit,:rofl:

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.

Logit
07-28-2017, 05:49 PM
Try this.

TonC
07-28-2017, 11:57 PM
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,

Logit
07-29-2017, 07:21 AM
.
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.