473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

vba Union SQL

70 64KB
I'm trying to get a union query to work in vba.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearch_Click()    Dim sqlSearch As String
  2.     If Not IsNull(Me.cboSearchLastName) Then
  3.         sqlSearch = "SELECT tblCustomer.LastName, tblFacilityMgr.CustomerFK, tblFacilityMgr.BuildingFK, tblRooms.BuildingFK" _
  4. & " FROM (tblBuilding INNER JOIN tblRooms ON tblBuilding.BuildingPK = tblRooms.BuildingFK) INNER JOIN" _
  5. & " (tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK) ON tblBuilding.BuildingPK = tblFacilityMgr.BuildingFK" _
  6. & " WHERE LastName ='" & Me.cboSearchLastName & "'"" _
  7. & " UNION SELECT tblCustomer.LastName, tblRoomsPOC.CustomerFK, tblRooms.RoomsPK, tblRooms.BuildingFK" _
  8. & " FROM (tblRooms INNER JOIN tblRoomsPOC ON tblRooms.RoomsPK = tblRoomsPOC.RoomsFK) INNER JOIN" _
  9. & " (tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK) ON tblRooms.RoomsPK = tblRoomsPOC.RoomsFK" _
  10. & " WHERE LastName ='" & Me.cboSearchLastName & "'"
  11.     End If
  12.     Me.RecordSource = sqlSearch
  13. End Sub
I get run-time error 3296, Join Expression Not Supported.

I did some research, it sounds like vba doesn't support "complicated queries"??? Is there a work-around for this?
Oct 4 '15 #1

✓ answered by Luuk

What is the '(' for in Line#4?
Expand|Select|Wrap|Line Numbers
  1. & " FROM (tblBuilding INNER JOIN tblRooms ......
Example inner join query:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   tblCustomer.LastName,
  3.   tblFacilityMgr.NameOfManager
  4. FROM
  5.   tblCustomer
  6.   INNER JOIN tblFacilityMgr 
  7.      ON tblCustomer.FacilityMgrId=tblFacilityMgr.tblId
  8. WHERE
  9.   tblCustomer.LastName = 'Obama'
  10.  

7 1234
jimatqsi
1,271 Expert 1GB
ittechguy,
"INNER JOIN tblCustomer INNER JOIN" is not going to work. It's not clear what you are trying to do but you need to either put tblCustomer at the beginning of the FROM clause and follow it with a comma or you need to specify how that table will be joined to the other tables. INNER JOIN implies certain join criteria have to be met and you have not supplied any.

Jim
Oct 4 '15 #2
Luuk
1,047 Expert 1GB
What is the '(' for in Line#4?
Expand|Select|Wrap|Line Numbers
  1. & " FROM (tblBuilding INNER JOIN tblRooms ......
Example inner join query:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   tblCustomer.LastName,
  3.   tblFacilityMgr.NameOfManager
  4. FROM
  5.   tblCustomer
  6.   INNER JOIN tblFacilityMgr 
  7.      ON tblCustomer.FacilityMgrId=tblFacilityMgr.tblId
  8. WHERE
  9.   tblCustomer.LastName = 'Obama'
  10.  
Oct 4 '15 #3
mbizup
80 64KB
If you haven't already done so, I'd suggest getting the query to work in the query designer first - using design view, SQL view or both.

Once you have it working, you can copy the SQL into the VBA editor and work out the VBA syntax starting with a known working query.
Oct 4 '15 #4
NeoPa
32,556 Expert Mod 16PB
Hi Miriam. Lovely to see you post. -Ade.

@ITTechGuy.
MBizup's advice is good. I'm not sure why you are posting VBA when your problem is with the SQL, but you can be assured that any SQL that works in Access will work equally well in Access when called from VBA. VBA simply creates the string (SQL) and executes it. Whether or not it works is down to Jet/ACE, which is a fundamentally independent engine.
Oct 4 '15 #5
mbizup
80 64KB
Thanks for the warm welcome, Ade :)
Oct 4 '15 #6
ittechguy
70 64KB
Thanks for your responses guys.

I tested this union query in the query builder and it worked (or so I thought it did). So I assumed this must be a vba issue. Last last night I ended up testing each half of the query separately. I found that the first half worked, but the second didn't. You were right Luuk.

I rebuilt the query and now it works!
Oct 5 '15 #7
NeoPa
32,556 Expert Mod 16PB
It's always harder to work out when you think you've tested it already. Trust me, you aren't the first to fall foul of that one! It's hard to avoid. You just learn over time to be even more careful and check each step even more precisely.

One technique I often use though, whenever I have queries that fail and are VBA created, is to take the actual SQL strings being used and paste them into the SQL view of a new QueryDef object (No need to save it ever). From here I switch to design view if allowed then try running it. When it fails from here it generally throws out a more helpful error message than returned via VBA (Err. etc).

If that isn't helpful enough, and that can often be the case when working with UNION or otherwise complex queries, then chop bits out until you get something that does.

Remember though, if the problem's in the SQL then that's the best thing to post here. Only post the VBA if you think you have a VBA problem. That way you get more focused help. Some experts are particularly helpful with one, some with the other. Many with both of course, but not all.
Oct 5 '15 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
6
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
2
by: Barry Schwarz | last post by:
Given a union of the form union { T1 m1; T2 m2;}obj; where T1 and T2 are different scalar (non-aggregate) types. The C99 standard states that obj.m1 = value; if (obj.m2 ... invokes...
10
by: Denis Pithon | last post by:
Hi, C lovers! I stuck on an union problem Here is snippet of my code .... /* two pointers of function with repsectively one and two argues */ typedef int (*dce_sn_f)(dce_t*);
2
by: Peter Dunker | last post by:
Hi, I will write ANSI C89. Is the following struct defenition correct ? I wrote it with VC6(Windows IDE) and at first no Problem. As I changed a compiler switch to 'no language extension', the...
73
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
18
by: ranjeet.gupta | last post by:
Dear ALL As we know that when we declare the union then we have the size of the union which is the size of the highest data type as in the below case the size should be 4 (For my case and...
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
3
by: SRK | last post by:
Hi, I wanted to use an anonymous union within an structure something like below - struct Test { union { std::string user; //char user; std::string role; //char role;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.