Results 1 to 8 of 8
  1. #1
    ui7598h is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    10

    IF Then Else VBA statement

    Hello, I have a form where user enters a case number into a text box and on the after update property i need one of two labels to become visible based on if the case number is on the table tblQualityAudit. the case number is numeric, and the field property is set to double on the table as well as the text box property. below is the code that I have.


    Private Sub txtLookup_AfterUpdate()
    Dim dblCase As Double
    dblCase = txtLookup
    If strCase = DLookup("case", "tblQualityAudit") Then
    Me.lblReject.Visible = True
    Else
    Me.lblGood.Visible = True
    End If
    End Sub




    when testing, the lblGood appears for everything. even if the case number is in the tblQualityAudit. not sure if there is an issue with the DLookup or maybe I am missing something. any help would be appreciated.

    Also if you can give insight on how to make the label go back to not visible when the user goes to enter a new value that would be awesome too


    Thanks in advance
    Steve

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Well for starters, you set dblCase and then test strCase.

    http://www.baldyweb.com/OptionExplicit.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ui7598h is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    10
    Private Sub txtLookup_AfterUpdate()
    Dim dblCase As Double
    dblCase = txtLookup
    If dblCase = DLookup("case", "tblQualityAudit") Then
    Me.lblReject.Visible = True
    Else
    Me.lblGood.Visible = True
    End If
    End Sub

    I originally declared variable as string, and pasted the code. i thought after starting the post that it may be because it is a numeric value it needed to be declared as Double and i just changed what I had pasted. I missed changing the strCase to dblCase. I fixed and pasted above.


  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Is this in a Datasheet or Continuous form? Programmatically setting control properties will be reflected by ALL instances of the control - after all, there is actually only one control. Textbox and combobox have Conditional Formatting to accomplish dynamically setting font and background colors and enabled properties. Visibility is not available with Conditional Formatting. Can use an UNBOUND textbox with Conditional Formatting to serve as the 'label' - change colors or Enabled property.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Two things to note:

    First, you see the labels on each record because they are unbound controls, and stay visible/hidden until you change them. In your after update procedure, when make one label visible, you have to hide the other one - so each part of the condition will set one label to true and the other label to false.

    Secondly,you have to have the same code in the On Current event procedure, so that the labels are displayed correctly when you move from one existing record to another.
    That will work only if you have a field in your table where you keep the value you selected in the lookup combo.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Sounds like your DLookup() needs a criteria. As it is, it's only looking at the first record.

    DLookup Usage Samples
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    ui7598h is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    10
    pbaldy,

    Thank you. it was in need of criteria. changed code to

    Private Sub txtLookup_AfterUpdate()Dim dblCase As Double
    dblCase = txtLookup
    If dblCase = DLookup("case", "tblQualityAudit", "case = '" & dblCase & "'") Then
    Me.lblReject.Visible = True
    Else
    Me.lblGood.Visible = True
    End If
    End Sub

    it now works flawlessly.

    Thanks to all who replied

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 11
    Last Post: 04-29-2015, 01:38 PM
  2. Need help with IIF statement
    By mommyof4kids in forum Forms
    Replies: 11
    Last Post: 06-21-2013, 08:10 AM
  3. if statement in sql statement, query builder
    By 54.69.6d.20 in forum Access
    Replies: 4
    Last Post: 09-11-2012, 07:38 AM
  4. Replies: 7
    Last Post: 08-17-2011, 01:49 PM
  5. Help with an Iif statement please
    By 10 Gauge in forum Programming
    Replies: 4
    Last Post: 04-05-2011, 06:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums