VBA to test the presence of "^" in a table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • IRISOS
    New Member
    • Apr 2014
    • 1

    VBA to test the presence of "^" in a table

    Dear All,

    I am new to VBA in a MS Access. Here is the problem: I have a table “Tbl_ABC” which contains a column “Code”. Sometimes, when we get the external data delivery, the product identification code contains special characters like “^”, which basically says that the information provided is outdated. Hence we can’t upload the data series, but instead we should get an alert by email that the data delivery needs to be reviewed.

    I have been able to implement the VBA sending out the alert, but I am not able to figure out how the code for testing the table “Tbl_ABC” should like look like, and the way the if .. else.. is going to interact.

    Any input how to get this done would be greatly appreciated. Thank you, christoph
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    Christoph,
    Welcome to Bytes. I'm sure you'll find lots of help here.

    There is a function called Instr. A call like this:
    Code:
    Instr("ABC^xyz","^")
    would return a value of 4, indicating the character position of the thing you were looking for. You can look for strings of any length. So
    Code:
    if (instr([fieldname],"^")>0 then do something
    is what you need.

    Does that help?

    Jim
    Last edited by jimatqsi; Apr 16 '14, 02:51 PM. Reason: weird result because of using a field name 'Code'

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      You could also use the LIKE operator.
      Code:
      WHERE field1 LIKE '*^*'

      Comment

      • jimatqsi
        Moderator Top Contributor
        • Oct 2006
        • 1293

        #4
        or is it ALIKE? I always have to double-check that. Might be simpler in this case to stay away from that pitfall.

        Jim

        Comment

        • Seth Schrock
          Recognized Expert Specialist
          • Dec 2010
          • 2965

          #5
          It is LIKE. I don't believe that Access has an ALIKE command.

          Comment

          • zmbd
            Recognized Expert Moderator Expert
            • Mar 2012
            • 5501

            #6
            Access 2007/2010 can use the ANSI-92 syntax, so that ALIKE can be used; however, beaware that you also go from "*" to "%" for the wild cards.

            To toggle between the Standard ACCESS-SQL and ANSI-92 SQL syntax:
            Close all database objects.
            Click the File-Ribbon Tab, (ACC2007:Office button), then Access Options.
            Click Object Designers.
            Clear/select the check box labeled "This database" (and if you wish, "Default for new databases") under SQL Server compatible syntax (ANSI 92) in the Query design section.
            Click OK.

            I just posted some links showing the differences between the ACCESS and ANSI - SQL syntax here:
            Recordset Returns No Records; SQL Returns Records - Post#5

            Comment

            • jimatqsi
              Moderator Top Contributor
              • Oct 2006
              • 1293

              #7
              Thanks, zmbd. It's surprising how hard it is to find that information online.

              Jim
              Last edited by zmbd; Apr 16 '14, 05:55 PM. Reason: [z{ (^_^) }]

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32663

                #8
                ALIKE is available in Access and has been for a number of versions (I just tested it on 2003).

                ANSI-92 is a separate but related issue. This specifies that the ANSI-92 special characters are used when LIKE is used.

                Using ALIKE will force the use of ANSI-92 special characters even when the database is using ANSI-89 as standard.

                Another difference is that LIKE is also supported in VBA whereas ALIKE is unfortunately not.

                It's a way of making you code more portable as it works in the same way for old and new alike (Pun not intended until I realised and then thought - hey, why not, and decided to intend it after all).

                Perhaps I should use it more myself as it has those benefits!
                Last edited by NeoPa; Apr 17 '14, 12:22 AM. Reason: Added VBA comment.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32663

                  #9
                  Christoph,

                  It's hard to tell, from what you've posted, what it is precisely that you're looking for. Is it to tell if any record has a carat (^) within the table? Is it to select each record that has?

                  These scenarios require different solutions. Knowing what to suggest is hard when the question doesn't say what it is you want.

                  Comment

                  Working...