Results 1 to 7 of 7
  1. #1
    karmacable is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2011
    Posts
    24

    Operator / Syntax failure in coding

    I'm writing code where I look through more than two combo boxes (after a user has made their selections), click a command button to check to see if they match or not, and print the results in a caption.



    When I work with simply two combo boxes;

    Code:
     
    Dim Rig1st As String
    Dim Lenses1st As String
     
    If Me.cmbARig & Me.cmbBRig <> 0 Then
      If Me.cmbARig = Me.cmb1stCamBRig Then
        Rig1st = "2 x " & Me.cmbARig.Column(1)
        Me.lblRigs.Caption = Rig1st
      Else
        Rig1st = "1 x " & Me.cmbARig.Column(1) & vbNewLine & "1 x " & Me.cmb1stCamBRig.Column(1)
        Me.lbl1stRigs.Caption = Rig1st
      End If
    Else
      Me.lbl1stRigs.Caption = ""
    End If
    The above works great. As you can see in the second 'If' line, I'm simply checking to see if the combo boxes ID's (their number fields) match.

    However, if I try to apply the same logic / syntax to check through four combo boxes like this (and for this example I simply stopped at what happens if you choose the same in all four boxes);

    Code:
    If Me.cmbALens1 & Me.cmbLens2 & Me.cmbBLens1 & Me.cmbBLens2 <> 0 Then
      If Me.cmbALens1 = Me.cmbALens2 = Me.cmbBLens1 = Me.cmbBLens2 Then
        Lenses1st = "4 x " & Me.cmbALens.Column(1)
        Me.lblLenses.Caption = Lenses1st
      Else
        Me.lblLenses.Caption = "Formula Not Working"
      End If
    Else
      Me.lblLenses.Caption = "Formula Still Not Working"
    End If
    Then my caption box shows the "Formula Not Working". What am I doing wrong in my logic of trying to see if all boxes have the same value? And also, what would the syntax for exclusion be (e.g. cbo1 = cbo2 = cbo3 <> cbo4), because I will be coding messages for that as well?

    Thanks

  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,641
    Your first line probably won't work correctly, and would need to be:

    If Me.cmbALens1 <> 0 AND Me.cmbLens2 <> 0 AND Me.cmbBLens1 <> 0 AND Me.cmbBLens2 <> 0 Then

    Your existing test is likely concatenating the 4 values together and testing that against 0. In a similar manner, the second would look like


    If Me.cmbALens1 = Me.cmbALens2 AND Me.cmbBLens1 = Me.cmbBLens2 Then

    In other words, you have to test two values against each other.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    karmacable is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2011
    Posts
    24
    Your logic makes sense, I'll give it a try now and report back soon.

    BTW, sorry if this is a VBA 101 question, but when to use the '&' and when to use 'AND'?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    & is a concatenation operator, AND is a logical operator. You'd use & to join things together:

    "Paul" & " " & "Baldy"

    would yield

    Paul Baldy

    AND (along with OR) is commonly used in code to evaluate expressions:

    If ThisTestIsTrue AND ThatTestIsTrue Then
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    karmacable is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2011
    Posts
    24
    Awesome, it works!! Now the next question (as I now know I'll have much more code to write), is there a more efficient way to do the above coding? I have to make responses for each possible combination of selections from the 4 boxes. Would something like an 'array' help here (something I've heard of but have no idea how to work)?

    Thanks again for the first answer, I'll mark this thread as solved.

  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,641
    Are you just testing that something is selected, or what is actually selected? I'm not clear on what you're trying to do.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    karmacable is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2011
    Posts
    24
    I'm basically creating a summarized report based on what is actually selected. A user has a number of combo box selections and based on their choices (or lack of), I fill a label's caption to show what they've chosen. To illustrate (not my actual DB's setup, but the structure);


    Group 1___Group 2____Group 3___Group 4
    Sony______ Panasonic ___N/A _________N/A
    Sony______N/A__________N/A_________N/A
    N/A_______Sony_________N/A_________N/A
    Nikon______Sony_________Panasonic____N/A
    N/A_______Sony_________Nikon________N/A

    So for the above project, the end result would be;

    Group 1 has 2 x Sony & 1 Nikon
    Group 2 has 3 x Sony & 1 Panasonic
    Group 3 has 1 x Panasonic & 1 Nikon

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

Similar Threads

  1. f4 combo box failure
    By djuplina in forum Access
    Replies: 2
    Last Post: 09-19-2011, 01:03 PM
  2. Replies: 3
    Last Post: 08-19-2011, 09:06 AM
  3. Syntax error missing operator(3075)
    By parisienne in forum Programming
    Replies: 1
    Last Post: 04-07-2011, 02:29 PM
  4. Syntax Error...missing operator
    By jgelpi16 in forum Programming
    Replies: 14
    Last Post: 09-09-2010, 11:35 AM
  5. Replies: 1
    Last Post: 10-07-2009, 07:36 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