SQL issue - Select multiple fields from multiple unrelated tables

Lestatos

Registered User.
Local time
Today, 07:10
Joined
Oct 22, 2013
Messages
16
Good evening dear programmers .
A small issue I was wondering of for a few day . Is it possible in SQL query to SELECT multiple fields from multiple tables ?
Example for the question is
Code:
dim  my_var  as String 
 my_var  = "SELECT Emp_FName , Emp_LName , Emp_Adress " _ 
& " FROM Table1 " _ 
& " AND Emp_Date_Of_Payment , Emp_Sum_Of_Payment " _ 
& "FROM Table2 " _ 
& " WHERE Emp_ID = 3 "
Is this code actually valid in SQL gramatics , and is it usable if passed to a Recordset variable ( rs = CurrentDB.OpenRecordset(my_var) ) ???
Just FYI - The two tables are not related and I want to keep them that way (If possible relate their records just via SQL/Vba )
Best Regards.
 
Is this code actually valid in SQL gramatics

If only you had some sort of machine that could interperet SQL and produce results if it was valid. Oh to dream.

Until then I guess you will have to post all SQL here so we can let you know. And in this case, no, it is not valid SQL. You have a few things--multiple FROM declarations, 'AND' in the FROM clause.


If you can post sample data from both tables along with field and table names, then what data the query should return based on that sample data we can help you achieve what you want (assuming its possible)
 
Good evening , plog .
You can see the attached pictures of the tables I want to query .

And here is the code what I want to run ( a little explanation is below :) ) :
Code:
Public Function CreateDynamicReport(strSQL As String, sname As String)
Dim db As DAO.Database ' database object
Dim rs As DAO.Recordset ' recordset object
Dim fld As DAO.Field ' recordset field
Dim txtNew As Access.TextBox ' textbox control
Dim lblNew As Access.Label ' label control
Dim rpt As Report ' hold report object
Dim lngTop As Long ' holds top value of control position
Dim lngLeft As Long ' holds left value of controls position
Dim title As String 'holds title of report
Dim imgPath As String
Dim lblCurrentValue As String
imgPath = "C:\Users\svetlio\Documents\Icons for Projects\Backgrounds_Darkgrey.jpg"
     'set the title
     title = "Äàííè çà èçáðàíèÿ ðàáîòíèê"
 
     ' initialise position variables
     lngLeft = 0
     lngTop = 0
 
     'Create the report
     Set rpt = CreateReport
 
     ' set properties of the Report
     With rpt
         .Width = 8500
         .RecordSource = strSQL
         .Caption = title
         .Modal = True
         .PopUp = True
         .LayoutForPrint = True
         .Picture = imgPath
         .PictureTiling = True
         
     End With
 
     ' Open SQL query as a recordset
     Set db = CurrentDb
     Set rs = db.OpenRecordset(strSQL)
 
     ' Create Label Title
     Set lblNew = CreateReportControl(rpt.Name, acLabel, _
     acPageHeader, , DLookup("Labels_Msg", "tbl_Labels", "Label_ID = 2") & sname, 0, 0)
     lblNew.FontBold = True
     lblNew.FontSize = 12
     lblNew.SizeToFit
     lblNew.ForeColor = vbBlack
 
     ' Create corresponding label and text box controls for each field.
     For Each fld In rs.Fields
 
         ' Create new text box control and size to fit data.
         Set txtNew = CreateReportControl(rpt.Name, acTextBox, _
         acDetail, , fld.Name, lngLeft + 1500, lngTop)
         txtNew.SizeToFit
         txtNew.FontBold = True
         txtNew.FontSize = 12
         txtNew.SizeToFit
         txtNew.ForeColor = vbRed
         txtNew.BackStyle = Transparent
         
         ' Create new label control and size to fit data.
    If fld.Name = "Emp_First_Name" Then
         
         
         Set lblNew = CreateReportControl(rpt.Name, acLabel, acDetail, _
         DLookup("Labels_Msg", "tbl_Labels", "Label_ID = 3"), DLookup("Labels_Msg", "tbl_Labels", "Label_ID = 3"), lngLeft, lngTop, 1400, txtNew.Height)
         lblNew.SizeToFit
         lblNew.ForeColor = vbBlack
    End If
    If fld.Name = "Emp_Last_Name" Then
    
         Set lblNew = CreateReportControl(rpt.Name, acLabel, acDetail, _
         DLookup("Labels_Msg", "tbl_Labels", "Label_ID = 4"), DLookup("Labels_Msg", "tbl_Labels", "Label_ID = 4"), lngLeft, lngTop, 1400, txtNew.Height)
         lblNew.SizeToFit
         lblNew.ForeColor = vbBlack
    End If
    
         ' Increment top value for next control
         lngTop = lngTop + txtNew.Height + 5
     Next
     
 
     ' Create datestamp in Footer
     Set lblNew = CreateReportControl(rpt.Name, acLabel, _
     acPageFooter, , Date, 0, 0)
     lblNew.ForeColor = vbBlack
 
     ' Create page numbering on footer
     Set txtNew = CreateReportControl(rpt.Name, acTextBox, _
     acPageFooter, , "='Page ' & [Page] & ' of ' & [Pages]", rpt.Width - 1000, 0)
     txtNew.SizeToFit
 
     ' Open new report.
     DoCmd.OpenReport rpt.Name, acViewPreview, , , acDialog
 
     'reset all objects
     rs.Close
     Set rs = Nothing
     Set rpt = Nothing
     Set db = Nothing
 
End Function

This code is indeed copied from another person . It should be not commercially used ( I must say it :) )
Well that code I have studied in details and now I am trying to change it and suit it to my needs .
Indeed as we see it now it can only work with one Table and respond to its fields . I want to make this dynamic report able to take info from all the tables I want (probably separate arguments ? ) and read their fields info concernig one Emp_ID or Company_ID ....
I hope my idea is a bit clear for you , cause I think my explanation skills are not that good :banghead:
Best regards .
 

Attachments

  • Emp_Gen_Info.png
    Emp_Gen_Info.png
    11.7 KB · Views: 156
  • Emp_Paym_Add.png
    Emp_Paym_Add.png
    10.5 KB · Views: 164
You only gave me table structures, you didn't give me sample starting data nor ending query data, so I can't really help you.

However, from those images you posted it seems your data is related: both tables contain Emp_ID
 
Total shot in the dark here, but would something like this be what you are looking for:

Code:
"SELECT Table1.Emp_FName, Table1.Emp_LName, Table1.Emp_Adress, Table2.Emp_Date_Of_Payment, Table2.Emp_Sum_Of_Payment" _ 
& " FROM Table1 Inner Join Table2 On Table1.Emp_ID = Table2.Emp_ID" _ 
& " WHERE Table1.Emp_ID = 3 "
 
Yes , both tables have data that is related to a single Employee , but I didn't use Access Relationships Tool , I want to do my complete database via VBA or SQL .
May I ask a question - what exactly should I post ( attach ) so it would be useful for you to help . ( Like - Table , with data on it , Form , Query , etc .. ) ?
Best regards .
 
You should post starting data from your 2 tables from the relevant fields (include table and field names) and then what data your query should produce based on that sample data. Use this format:

TableNameHere
Field1Name, Field2Name, Field3Name...
David, 1/2/2013, 17
Sally, 3/13/2008, 99
Jim, 7/7/2009, 12
 

Users who are viewing this thread

Back
Top Bottom