Consulting

Results 1 to 18 of 18

Thread: Solved: If Structure

  1. #1
    VBAX Newbie
    Joined
    Jun 2004
    Location
    NY, NY
    Posts
    5
    Location

    Solved: If Structure

    Hi, im trying to write a condition in an Access report. I have a checkbox field in my report that shows me the person owes me money. Now i would like to write an If statement saying that

    if the checkbox is checked then
    the lable of the checkbox is visible.

    Now i've tried

    If chkBox.Value = True Then
    lblChkBox.Visible = True
    End If


    Any Suggestions!! Thanx

    I forgot to mention, the field in the table is a Yes/No field. It is already populated with data.
    Last edited by tibby812; 06-07-2004 at 04:47 PM.

  2. #2
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Sorry if I'm wrong, tibby. I'm not good with this stuff.

    Try this syntax, changing the values, of course:

    [vba]
    'checked = 0
    'unchecked = -1
    If (MyField = -1 Or IsNull(MyField)) Then
    Me.MyLabel.Visible = False
    Me.MyField.Visible = False

    Else
    Me.MyLabel.Visible = True
    Me.MyField.Visible = True

    End If
    [/vba]
    ~Anne Troy

  3. #3
    VBAX Regular
    Joined
    May 2004
    Location
    Sweden
    Posts
    21
    Location
    Hi tibby

    Are you trying to hide the CheckBox? if so try:

    [VBA]
    If CheckBox1 = True Then

    CheckBox1.Visible = True

    End If

    If CheckBox1 = False Then

    CheckBox1.Visible = False

    End If
    [/VBA]

    If you just want to change the Caption try:

    [VBA]
    If CheckBox1 = True Then

    CheckBox1.Caption = "Visible"

    End If

    If CheckBox1 = False Then

    CheckBox1.Caption = "Hidden"

    End If
    [/VBA]

    /Roger

  4. #4
    VBAX Newbie
    Joined
    Jun 2004
    Location
    NY, NY
    Posts
    5
    Location
    Im not trying to change the caption or trying to hide it, i can do those things, but dreamboat is on the right track, i basically want to say if the field is yes, then display a lable. When i put

    If chkBox.Value = True Then
    lblChkBox.Visible = True
    End If

    it tells me that there is no value

    The exact error is run-time 2427 , You enetered an expression that has no value

  5. #5
    VBAX Newbie
    Joined
    Jun 2004
    Location
    NY, NY
    Posts
    5
    Location
    Thought this might help

    I have a table called Main Table

    i have 5 fields

    Name = txt
    ID = AutoNum
    Over21 = Yes/No
    HasComputer = Yes/No
    HasCellPhone = Yes/No

    Ok........now enter 5 people in the table and check the boxes.

    Now Create a report thats bound to MAIN TABLE

    In the detail, add all the fields.

    Now field Over21 , HasComputer, HasCellPhone are check boxes. These check boxes have labels.

    Now i have code saying

    [vba]Private Sub Report_Open(Cancel As Integer)
    chkOver21.Visible = False
    chkHasComputer.Visible = False
    chkHasCellPhone.Visible - False[/vba]

    Now this also hides the labels that are bound to these fields, but i want to say if the field chkOver21 is checked , display the corresponding label.

    if chkOver21.Value = Yes Then
    lblOver21.Visible = True
    end if

    Now you may ask why i don't try exactly what i wrote to you, but i did, and it gives me a "run-time error 2427, You enetered an expression that has no value"

  6. #6
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Did you try the syntax I gave?
    What happens then?

    PS: See the little VBA button just above the reply box.
    Use it to make your VBA code look like code. Click it, and paste the code between the tag sets.
    ~Anne Troy

  7. #7
    VBAX Newbie
    Joined
    Jun 2004
    Location
    NY, NY
    Posts
    5
    Location
    Drreamboat,

    I tried the code that you gave me and it gives me the same error. 2427 Runtime Error

  8. #8
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    I placed this in the AfterUpdate event of the checkbox and it works fine (on Off 2003)

    [vba]
    If cbox.Value = True Then
    lblCbox.Visible = True
    ElseIf cbox.Value = False Then
    lblCbox.Visible = False
    End If
    [/vba]
    "All that's necessary for evil to triumph is for good men to do nothing."

  9. #9
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    couldn't you shorten that James to:

    [vba]
    If cbox.Value = True Then
    lblCbox.Visible = True
    End If
    lblCbox.Visible = False [/vba]

    ???

  10. #10
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    Hmm - I think in your example, even when the IF statement executes, the

    [vba]
    lblCbox.Visible = False
    [/vba]

    line will execute every time. So lblCbox.Visible will always be set to False.

    It's late, so maybe I'm just not reading it right.
    "All that's necessary for evil to triumph is for good men to do nothing."

  11. #11
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    You could shorten it like
    [vba]
    If cbox.Value = True Then
    lblCbox.Visible = True
    Else
    lblCbox.Visible = False
    End If
    [/vba]
    "All that's necessary for evil to triumph is for good men to do nothing."

  12. #12
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    my line of thinking was, it's checkbox, right? I mean, we're looking at boolean values, so it's either True or False. You only need the one if statement and no Else.

    But it's late for me too, so, I could be wrong.

    edit: disregard, I'm WAY wrong - too tired.
    Last edited by Zack Barresse; 06-12-2004 at 02:06 PM.

  13. #13
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Ok, I've scanned this thread a bit quickly so I may have missed something.

    Putting code for this in the Report_Open Event isn't going to do a lot of good. You want to set the properties in the Detail_Format Event.

    As all your values are Boolean, you can also shorten it something like this ..

    [vba]chkOver21.Visible = chkOver21
    chkHasComputer.Visible = chkHasComputer
    chkHasCellPhone.Visible = chkHasCellPhone

    lblOver21.Visible = chkOver21
    lblHasComputer.Visible = chkHasComputer
    lblHasCellPhone.Visible = chkHasCellPhone[/vba]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  14. #14
    VBAX Tutor SJ McAbney's Avatar
    Joined
    May 2004
    Location
    Glasgow
    Posts
    243
    Location
    Quote Originally Posted by jamescol
    You could shorten it like
    [vba]
    If cbox.Value = True Then
    lblCbox.Visible = True
    Else
    lblCbox.Visible = False
    End If
    [/vba]
    Or:

    [vba]Me.lblCbox.Visible = Me.Cbox[/vba]


  15. #15
    VBAX Newbie
    Joined
    Jun 2004
    Location
    NY, NY
    Posts
    5
    Location
    To Tony Jollans, your advice inspired me and i wanted to thank you for your info.

    This is what i did,

    [VBA]
    Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

    chkOver21.Visible = chkOver21

    lblOver21.Visible = chkOver21.Visible

    End Sub

    [/VBA]

  16. #16
    VBAX Tutor SJ McAbney's Avatar
    Joined
    May 2004
    Location
    Glasgow
    Posts
    243
    Location
    Quote Originally Posted by tibby812
    To Tony Jollans, your advice inspired me and i wanted to thank you for your info.

    This is what i did,

    [VBA]
    Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

    chkOver21.Visible = chkOver21

    lblOver21.Visible = chkOver21.Visible

    End Sub

    [/VBA]
    One thing:

    Try never to imply what you are referred to - always be explicit. Include the reference to the current report's Class (Me.) - it helps the speed of the database once compiled. It simply means that when running the database doesn't have to find where the chkOver21 and lblOver21 objects are. By including the explicit reference to the report's class, it knows immediately reference these objects on that report.

    [vba]Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

    Me.chkOver21.Visible = Me.chkOver21

    Me.lblOver21.Visible = Me.chkOver21.Visible

    End Sub[/vba]
    Last edited by mark007; 06-08-2004 at 08:55 AM. Reason: Removed rogue colour tags

  17. #17
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Hey, tib?
    Is this resolved yet?
    It appears to be...
    ~Anne Troy

  18. #18
    VBAX Contributor DarkSprout's Avatar
    Joined
    Oct 2007
    Location
    Essex, England
    Posts
    144
    Location
    Instead of

    [vba]
    If Me.cbox.Value = True Then
    lblCbox.Visible = True
    Else
    lblCbox.Visible = False
    End If
    [/vba]


    Try Some Simpler Code:
    [vba]
    [lblCbox].Visible = [cbox]
    [/vba]
    =|)arkSprout=
    VBA | VBScript | C# Programmer

    "Give a person a fish and you feed them for a day; teach a person to use the Internet and they won't bother you for weeks."

Posting Permissions

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