Vb and Forms

eunice007

New member
Local time
Yesterday, 18:31
Joined
Feb 3, 2008
Messages
8
Please help, when I write the code below on a form, there is a pop up that says 'Macro Name'. I want the check boz to change color when ticked.

But if i run the same code on VB6 environment it works.




Option Compare Database

Private Sub Check0_Click()
If Check1.Value = 0 Then

Form1.BackColor = vbBlue

ElseIf Check1.Value = 1 Then

Form1.BackColor = vbRed

End If

End Sub

Thank you
 
In Access you have to set the backcolor for each section of a form (detail, header and footer) not for the form as a whole. So, assuming you have all three sections displayed, your code would need to be:

Code:
Private Sub Check0_Click()
If Check1.Value = 0 Then

Me.Detail.BackColor = vbBlue
Me.FormHeader.BackColor = vbBlue
Me.FormFooter.BackColor = vbBlue

ElseIf Check1.Value = -1 Then

Me.Detail.BackColor = vbRed
Me.FormHeader.BackColor = vbRed
Me.FormFooter.BackColor = vbRed

End If

End Sub

If you don't have the header/footer expanded and showing, simply omit thses lines.

Notice also that I changed this line

ElseIf Check1.Value = 1 Then

to this

ElseIf Check1.Value = -1 Then

A checkbox that has been checked has a value of minus one (-1) not 1.
 
This procedure needs to be related to a Form CheckBox Control named Check0. The code within it makes reference to a CheckBox Control named Check1.

Notice the difference.....

Because of your Error, I'm going to go under the assumption that the actual CheckBox control you have on your Form is named Check0 therefore you need make sure that in code, that this is in fact the Control you want to read.

You can of course reference any control in code...perhaps you actually do have a CheckBox on your Form named Check1 but for what the code is actually doing, I would say not.

The name Form1 must also actually be the name of your Form. To remove any confusion about referencing a Form your code is actually running in, it's much easier to just use the Me keyword as in:

Me.Backolor = vbRed

Now...don't bother running any of this code because it simply will not work and I'll tell you why later....

Make sure your CheckBox Control on Form is named Check0. Change all reference to Check1 to Check0. Make Sure your Form Name is indeed Form1.

In most cases there are only two states (conditions) used in a CheckBox, either True (checked) or False (Unchecked). Knowing this you can simplify the code somewhat by removing one If/Then condition check.

If Check0.Value = False then
Me.BackColor = vbBlue
Else
Me.BackColor = vbRed
End If

Do you see how that works?

As I mentioned earlier, the code above will not work in MS-Access for the simple reason that MS-Access Forms do not contain a BackColor property and there is a reason for this. VB6 Forms do.

MS-Access Forms contain something VB6 Forms do not....Sections. To change the Background of a MS-Access Form you need to Reference the Form Sections. There are basically five Sections within a MS-Access Form and they are as follows:

Form Header, Page Header, Detail, Page Footer, and finally the Form Footer

This type of Form structure provides you with a lot of flexibility.

When you first open a new Form in Detail view the first section you see is the default Detail section. You can see the bar along the Top of the Form. If you click on the bar and check the Properties Window you can then see all the properties available to that section. You will see the BackColor property under the Format Tab of the properties window. If you flip through the Tabs in the Properties Window you will notice that these particular Form sections contain several different properties including Events.

Basically Visual Basic is pretty much the same all the way around but each application has slight differences to suit the needs of its environment. Even VBA (Visual Basic for Applications) has slight differences from one Application to another. For example, Excel VBA is slightly different than Access VBA. It's a matter of understanding these slight differences. Forms between VBA and VB6 are different and rightly so...the Forms in Access need to fill other specific requirements that VB6 didn't. Because of these specific requirements the way Forms are referenced are also different.

To change the Background color of a MS-Access Form you will need to decide which section you want to change. As I mentioned earlier, The Details section is the most commonly used and with that in mind, here is how you would do this in Code using your previous example:

Code:
Private Sub Check0_Click()
   If Check0.Value = False Then
      Me.Detail.BackColor = vbBlue
   Else
      Me.Detail.BackColor = vbRed
   End If
End Sub

Perhaps you want to change the Background color of a different open Form (Form2) from within the Form your Code is running in. As mentioned earlier, Forms in MS-Access are referenced differently than they are in VB6. In VB6 you can just use Form2.BackColor = xxx but with Access you must do it this way:

Code:
Private Sub Check0_Click()
   If Check0.Value = False Then
      Forms![B]Form2[/B].Detail.BackColor = vbBlue
   Else
      Forms![B]Form2[/B].Detail.BackColor = vbRed
   End If
End Sub

Do you see how the other Form (Form2) is referenced within the code. There are several ways in MS-Access to reference Forms in code and there are specific reasons for this. As an example:

Forms.Form2.Detail.BackColor = vbBlue
Forms("Form2").Detail.BackColor = vbBlue
Form_Form2.Detail.BackColor = vbBlue
Forms!Form2.Detail.BackColor = vbBlue


All these will carry out the task at hand.

.
 

Users who are viewing this thread

Back
Top Bottom