Question : Access SQL Join Statement in VBA - Help

Hi

I'm having trouble obtaining a recordset in VBA.  I'm coding in MS Outlook and have established a connection with our Access database.  I can generate a recordset with no joins without any issues, but when I try to include a join the recordset doesn't open.

I have two tables:

tbl001_CompanyHeader - This contains a list of all companies we speak to.  CompanyID is the primary key and it also has a CompanyName field

tbl001_Clients - This is a list of companies that are classified as clients.  It has a CompanyID field, but no CompanyName field.

I just need a simple join to create a recordset that has the CompanyID's and CompanyName's of the companies in the tbl001_Clients table.

I've attached the code, and the results of the debug.print (i.e. strSQL) is as follows:

SELECT tbl001_Clients.CompanyID, tbl001_CompanyHeader.CompanyName FROM tbl001_Clients INNER JOIN tbl001_CompanyHeader ON tbl001_Clients.CompanyID = tbl001_CompanyHeader.CompanyID;

Any idea what's wrong here?

Thanks



Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
Set objMyConn = OpenAccessDB("Z:\[DBNAME].mdb")
Set rstClient = CreateObject("ADODB.Recordset")
strSQL = "SELECT tbl001_Clients.CompanyID, tbl001_CompanyHeader.CompanyName " & "FROM tbl001_Clients " & _
"INNER JOIN tbl001_CompanyHeader " & "ON tbl001_Clients.CompanyID = tbl001_CompanyHeader.CompanyID;"

Debug.Print strSQL

If objMyConn.State = adStateOpen Then
MsgBox ("You are in")
End If

rstClient.Open strSQL, objMyConn, , adLockReadOnly

If rstClient.State = adStateOpen Then
With rstClient
.MoveFirst
If (.State = adStateOpen) And (Not (.EOF)) Then
Do Until .EOF
strName = .Fields("CompanyName")
Debug.Print strName
.MoveNext
Loop
End If
End With
End If

Answer : Access SQL Join Statement in VBA - Help

Hi,
It MUST open:
a- if there is a problem with the strSQL string, than an error should occur,
b- if strSQL string is correct, the recordset will open (your case), but may not produce any result..

Now, please tell what (if any) error is produced when using "JOINED" query ?

Thank you,
Zlatko.
Random Solutions  
 
programming4us programming4us