How to write Case Sensitive Query for MS Access?
Asked Answered
F

5

8

I want to know the Select Query for MS Access with case sensitive.

I have two values for VitualMonitorName as below

VCode VirtualMonitorName
Row 1 (1, 'VM1');
Row 2 (2, 'Vm1');

Here both values are different.

If I write

"SELECT VCode FROM VirtualMaster WHERE VirtualMonitorName like '" + Vm1 + "'";

It replies VCode = 1 Only.

Figment answered 6/4, 2012 at 16:59 Comment(0)
A
14

You can use the StrComp() function with vbBinaryCompare for a case-sensitive comparison. Here is an example from the Immediate window to show how StrComp() works. See the Access help topic for more details.

? StrComp("a", "A", vbBinaryCompare)
 1 

? StrComp("a", "A",vbTextCompare)
0

StrComp() returns 0 if the first two arguments evaluate as equal, 1 or -1 if they are unequal, and Null if either argument is Null.

To use the function in a query, supply the vbBinaryCompare constant's value (0) rather than its name.

SELECT VCode
FROM VirtualMaster
WHERE StrComp(VirtualMonitorName, "Vm1", 0) = 0;

This approach is also available to queries from other applications if they use the newer Access Database Engine ("ACE") drivers. For example, the following C# code

string myConnectionString =
        @"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" +
        @"Dbq=C:\Users\Public\Database1.accdb;";
using (OdbcConnection con = new OdbcConnection(myConnectionString))
{
    con.Open();
    using (var cmd = new OdbcCommand())
    {
        cmd.Connection = con;
        cmd.CommandText = 
                "SELECT COUNT(*) AS n FROM [VirtualMaster] " +
                "WHERE StrComp([VirtualMonitorName],?,?) = 0";
        cmd.Parameters.AddWithValue("?", "Vm1");
        cmd.Parameters.Add("?", OdbcType.Int);

        var vbCompareOptions = new Dictionary<string, int>() 
        {
            {"vbBinaryCompare", 0},
            {"vbTextCompare", 1}
        };
        string currentOption = "";

        currentOption = "vbBinaryCompare";
        cmd.Parameters[1].Value = vbCompareOptions[currentOption];
        Console.WriteLine(
                "{0} found {1} record(s)", 
                currentOption, 
                Convert.ToInt32(cmd.ExecuteScalar()));

        currentOption = "vbTextCompare";
        cmd.Parameters[1].Value = vbCompareOptions[currentOption];
        Console.WriteLine(
                "{0} found {1} record(s)",
                currentOption,
                Convert.ToInt32(cmd.ExecuteScalar()));
    }
}

produces

vbBinaryCompare found 1 record(s)
vbTextCompare found 2 record(s)
Abc answered 6/4, 2012 at 18:6 Comment(1)
Note: if you are using this in a query (as opposed to a vb script), you have to use the numeric value as the vb constants will throw an errorApery
H
2

Check this out:

https://support.microsoft.com/kb/244693?wa=wsignin1.0

This article describes four methods of achieving a case-sensitive JOIN using the Microsoft Jet database engine. Each of these methods has advantages and disadvantages that should be weighed before choosing an implementation. The methods are:

  • StrComp
  • Case-Sensitive IISAM Driver
  • Hexadecimal Expansion
  • Binary Field
Hermaphroditus answered 14/12, 2014 at 7:30 Comment(0)
M
1

Using only built-in functions, add an additional custom column in the query design view:

location: InStr(1,[VCode],"VM1",0)

the zero parameter requests binary compare (case sensitive) when finding location of "VM1" within [VCode]

set the criteria in that column to >0 so only records with non-zero location in the vcode matching Like "*vm*" contain the exact VM1 string -

The WHERE clause looks like:

WHERE (((VirtualMaster.VCode) Like "\*vm*") AND ((InStr(1,[VCode],"VM1",0))>0));
Marisamariscal answered 1/12, 2017 at 16:39 Comment(0)
M
0

Using at a simpler level of coding.

As a condition in a DCOUNT operation, checking on a Field (Column) that has to have the correct Case, and ignoring Blank States/Territories.

      '  lngcounter will count the all States 
      ' or Territories Field ( Column) with this 
      ' exact case value of 'Ohio'. ([ID] is an Autonumber ID field)

      lngCounter = DCount("[id]", Trim(Me!tboDwellingTablename), "[State/territory],'Ohio',0) = 0")
Mccourt answered 24/8, 2017 at 6:46 Comment(1)
hello it might help if your code used the questions example code. It may make it easier for the asker to apply your solution.Ancon
D
0

This only does one letter:

MS-ACCESS SQL:

SELECT Asc(Left([Title],1)) AS t FROM Master WHERE (((Asc(Left([Title],1)))=105));

Title is the field you want to search

Master is the Table where Title field is located

105 Ascii code for character..

In this case only Title's that start with i not I

If you want to search for lower case "a" you would change the 105 to 97

Daile answered 6/1, 2019 at 0:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.