none
Newbie Struggling with If OR Syntax RRS feed

  • General discussion

  • Hi all

    Trying to get my head round If statement syntax and failing dismally!

    I want to test the value of a combo box against two fields in a table and then report if the selected value exists.

    So far I have:

    Private Sub CmbContract_AfterUpdate()
    If Not IsNull(DLookup("ConID1", "TbLinkContract", "ConID1 = " & Val(CmbContract.Column(0)))) Then
    MsgBox "Contract has Link"
    End If
    End Sub

    This seems to work correctly, displaying the message if the combobox value is found in field ConID1 of my table.

    What I need to do is also check field ConID2 for the same combobox value.

    I've tried adding OR and the line

    If Not IsNull(DLookup("ConID2", "TbLinkContract", "ConID2 = " & Val(CmbContract.Column(0))))

    but I just get syntax errors.  I suspect my bracket control is at fault.

    Anyone help with the correct vba please?

    Ta

    Phil

    Thursday, December 3, 2015 8:25 PM

All replies

  • Hi Phil,

    You could try the following:

    If DCount("*", "tbLinkContract", "ConID1=" & Me.cmbContract & " OR ConID2=" & Me.cmbContract) > 0 Then

    MsgBox "Contract has Link"

    End If

    Note: The above assumes the combobox is bound to the first column.

    Hope that helps...

    Thursday, December 3, 2015 8:44 PM
  • Hi DB Guy

    Unfortunately my combo box is bound to column 2.

    How do I accommodate this in your example?

    Thanks

    Phil

    Thursday, December 3, 2015 9:04 PM
  • Hi Phil. Then, just add the Column(0) parts like you had in your original code. For example:

    Me.cmbContract.Column(0)

    Hope that helps...

    Thursday, December 3, 2015 9:30 PM
  • Thanks DB Guy that's got it.

    Phil

    Thursday, December 3, 2015 9:57 PM
  • Hi Phil. You're welcome. Good luck with your project.
    Thursday, December 3, 2015 10:15 PM