0

I am taking some data from Access table to Excel worksheet and they depends some parameters. My data list on access table includes for example:

1-x branch
2-y branch
3-z branch
4-t commercial branch
5-r commercial branch
6-w corporate branch
7-g corporate branch

On Excel VBA if I want to retrieve all of my data, I am creating a SQL query like:

sql = "select * from [table] where [Branches] like "%"

if I want to retrieve only commercial branches data:

sql = "select * from [table] where [Branches] like '%" & "commercial" & "%'"

and if I want to retrieve only corporate branch data:

sql = "select * from [table] where [Branches] like '%" & "corporate" & "%'"

and here is the question. What am I have to do if I want to retrieve data except corporate and commercial branches? What sort of sql query can we create?

Thanks a lot.


It's my bad, I could explain more clear. Before creating the query I am using my parameters like this:

Select Case main.Range("D6").Value
Case "ALL"
brnch = "%"
Case "CORPORATE"
brnch = "%" & "CORPORATE" & "%"
Case "COMMERCIAL"
brnch = "%" & "COMMERCIAL" & "%"
End Select

and my query is actually like that:

sql = "select * from [table] where [Branches] like '" & brnch & "'"

now what can I do to retrieve branches without commercials and corporates.

4
  • Gordon's query will work. However, querying the data as you're doing by looking for certain keywords in each branch's name... that gets to be ugly, slow, and potentially inaccurate. Is there any chance that you could add an integer column to that table that indicates the type of Branch that each entity is? 1 = Branch, 2 = CommercialBranch, 3 = CorporateBranch. Then filtering your query becomes trivial: WHERE [Table].[BranchCategoryId] IN(1, 2) AND WHERE [Table].[BranchCategoryId] NOT IN(2, 3) Commented Apr 19, 2019 at 14:52
  • it would be good but unfortunately I can't. It is quite an old database and some other Excel Workbooks run with this db. Commented Apr 19, 2019 at 15:01
  • So your Excel cell , main.Range("D6").Value, will have entries for NO commmercials and corporates? Commented Apr 19, 2019 at 18:08
  • I got an entry like "Others" in order to select no commercials and corporates. But I do not know how to create proper query. Commented Apr 24, 2019 at 9:01

1 Answer 1

2

You can use not like:

sql = "select * from [table] where Branches not like '%corporate%' and branches not like '%commercial%'";

If you want to run this code in MS Access, the wildcards are different and the string delimiter is a double quote:

sql = "select * from [table] where Branches not like ""*corporate*"" and branches not like ""*commercial*""";
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much but I made an additon to my problem below.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.