Connecting Tech Pros Worldwide Help | Site Map

DataReport with Two Tables

smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#1: Aug 24 '09
Dear All

I want to have data from two tables to be shown on DataReport as the tables are formed. For clarity of my question I have attached a Word file. Please look at it and help me solve the problem.
Attached Files
File Type: zip 2Tables.zip (4.1 KB, 57 views)
debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,500
#2: Aug 24 '09

re: DataReport with Two Tables


you want the output of both the tables on a single report ?

i think it has something to do with page break.
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#3: Aug 25 '09

re: DataReport with Two Tables


Thanks Debasisdas for taking interest. Actually I tried with various methods but could not secceed. I want output of both the tables in one report. I don't know about Page Break in DataReport. Can u plz tell me in detail about it & how to get required o/p.

Also is there any method to have two Details section in one report OR one report embedded inside other report, even then these things could be possible.

I can either use DataEnvironment with it or can get output programically. U can help me in either case. Programming one will be better because then data could be populated dynamically.

Thanks once again & hoping for an early reply.
Familiar Sight
 
Join Date: Oct 2007
Posts: 158
#4: Aug 27 '09

re: DataReport with Two Tables


hi smartchap
what i got form the word file is that you want to display data form both tables. upper half of data report will show data from first table and lower half will show data from second table.
if this is the case, please let me know

regards
manpreet singh dhillon hoshiarpur
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#5: Aug 28 '09

re: DataReport with Two Tables


Dear Mr. Dhillon

Yes actually I want the output in similar fashion as u understood. Besides these two tables o/p will have data from some labels, textboxes & combo boxes before as well as after the tables, i.e. some data before tables then tables then some data after tables.
Actually I have some data file with different ID for each record, for each ID have some data in two tables.
OK I am attaching a .zip file of .mdb file having three tabels. u can understand from this .mdb file.
If u help me for the problem as u understood, I think I could proceed further.
Thanks.
Attached Files
File Type: zip TwoTables.zip (11.0 KB, 20 views)
Familiar Sight
 
Join Date: Oct 2007
Posts: 158
#6: Aug 29 '09

re: DataReport with Two Tables


hi
u may use following logic to show data from two tables . i have attached a sample datareport with this code

Dim rst1 As New ADODB.Recordset
Dim rst2 As New ADODB.Recordset
Dim rst3 As New ADODB.Recordset
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
rst1.Fields.Append "a", adBSTR, , adFldIsNullable
rst1.Open
rst1.AddNew
rst1.Fields(0).Value = "Data from first table"
' in place of this block of code you will fetch data from first table
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
rst2.Fields.Append "b", adBSTR, , adFldIsNullable
rst2.Open
rst2.AddNew
rst2.Fields(0).Value = "data From second table"
' in place of this block of code you will fetch data from second table
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
'now add data from first table
rst3.Fields.Append "a", adBSTR, , adFldIsNullable
rst3.Open
rst1.MoveFirst
While Not rst1.EOF
rst3.AddNew
rst3.Fields(0).Value = rst1.Fields(0).Value
rst1.MoveNext
Wend

'following lines will separate data from both tables in the data report

rst3.AddNew
rst3.Fields(0).Value = "************************************"

'add data from second table
rst2.MoveFirst
While Not rst2.EOF
rst3.AddNew
rst3.Fields(0).Value = rst2.Fields(0).Value
rst2.MoveNext
Wend

'bind and show the report
With DataReport1
.DataMember = ""
Set .DataSource = rst3
.Sections("Section4").Controls("lblA").Caption = "Text before tables"
.Sections("Section5").Controls("lblb").Caption = "Text after tables"


.Sections("Section1").Controls("txtA").DataMember = ""
.Sections("Section1").Controls("txtA").DataField = "a"
.Refresh
.Show
.SetFocus
End With


regards
manpreet singh dhillon hoshiarpur
Attached Files
File Type: zip DataReport.zip (13.5 KB, 34 views)
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#7: Aug 30 '09

re: DataReport with Two Tables


Thanks Mr Dhillon
Please attach file again as it is corrupt file. Also please tell me how to fetch data from tables.
Thanks once again.
Familiar Sight
 
Join Date: Oct 2007
Posts: 158
#8: Aug 31 '09

re: DataReport with Two Tables


hi smartchap

actually attached file is not a .zip file but .rar file. because this site dont support .rar files so i changed the extention to .zip. to use this please change its extention to .rar.

use following code to fetch data from tables

dim cn as new adodb.connection
with cn
.Provider = "MsDataShape"
.Properties("Data Provider") = "Microsoft.Jet.OLEDB.4.0"
.Properties("Data Source") = "database path and name"
.Open
end with
Dim rst As New ADODB.Recordset
Dim cmd As New ADODB.Command
With cmd
.ActiveConnection = cn
.CommandType = adCmdText
.CommandText = "select * from table1"
End With
rst.CursorLocation = adUseServer
rst.Open cmd, , adOpenKeyset, adLockOptimistic, 8

with rst
if not (.eof and .bof) then
.movefirst
while not .eof
debug.print .fields(0).value
.movenext
wend
endif
end with

there are some other ways to open recordset. but i usually open recordset this way.

regards
manpreet singh dhillon hoshiarpur
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#9: Sep 4 '09

re: DataReport with Two Tables


Dear Mr. Dhillon
Thanks a lot for such a wonderful code and above all helping me with full code. I have modified the coe a lot as per my requirement.
I have drawn lines also around each textbox in detail section and now will try to hide border lines for last textbox for table1 since in Table 1 there are only 6 fields & in table2 there are 7 fields. So for displaying data for first table last textbox border will not be required.
This thing I will try & if required will ask u for help.
Thanks once again.
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#10: Sep 4 '09

re: DataReport with Two Tables


Dear Debasis
Though Mr. Dhillon has provided me the solution but I think u may help me designing the DataReport, as I required, using Page Break. If possible tell me more about it as I will be happy to know about it as it will add to my knowledge.

Thanks.
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#11: Sep 5 '09

re: DataReport with Two Tables


Dear Mr. Dhillon

Tried but could not succeed. I want to have data for both the tables separately, i.e. if I add a .AddNew line it will add a blank record. But as I am using rptLine lines around each textbox (in Detail Section) to get tabular look in output, a blank record which is added in between two tables is also having border lines, i.e. cells displayed. I want to hide / remove these lines for that blank record. Plz help.

Thanks.
Familiar Sight
 
Join Date: Oct 2007
Posts: 158
#12: Sep 11 '09

re: DataReport with Two Tables


hi smartchap
sorry for the delay
please let me know if the problam is not resolved.
if not then i will suggest you the alternate.

regards
manpreet singh dhillon hoshiarpur
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#13: Sep 12 '09

re: DataReport with Two Tables


Till date I could not resolve the problem. When I set the BorderStyle or BorderColor property of Line or Shape as 0 (Transparent / White) it reflects in whole report & not for some records / table. Please suggest me some method. With the help of ur code I have modified it a lot to suit my requirement and learned a lot. Now everyday I try to achieve solution for separate tables but could not succeed. Still I am trying it.
Thanks. Waiting for a fruitful suggestion.
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#14: Sep 15 '09

re: DataReport with Two Tables


Dear Mr. Dhillon
One more thing. When I run program separately it works fine (except problem of borders) but when I use this subroutine in another (big) program, it shows report for previous entered ID? Not the current ID. Any help.
Familiar Sight
 
Join Date: Oct 2007
Posts: 158
#15: Sep 26 '09

re: DataReport with Two Tables


hi smartchap
sorry again for the delayed replay

another solution for the problam:

make a union query for the two tables as illustrated

SHAPE {SELECT 'Table1' AS strTableID, strFld1, strfld2 FROM Table1 union all SELECT 'Table2' AS strTableID, strFld1, strfld2 FROM Table2} AS rstGrouped COMPUTE rstGrouped BY strTableID

to use shape command provider must be MsDataShape.

suppose your recordset is rstRpt

now bind the datareport following way


With rptTest
.DataMember = ""
Set .DataSource = rstRpt
.Sections("GH").Controls("txtGH_strTableID").DataM ember = ""
.Sections("GH").Controls("txtGH_strTableID").DataF ield = "strTableID"
'Bind Controls in Details Section
.Sections("Det").Controls("txtDet_strFld1").DataMe mber = "rstGrouped"
.Sections("Det").Controls("txtDet_strFld1").DataFi eld = "strFld1"
.Sections("Det").Controls("txtDet_strfld2").DataMe mber = "rstGrouped"
.Sections("Det").Controls("txtDet_strfld2").DataFi eld = "strfld2"
On Error GoTo ErrHndlr
.Refresh
.Show
.SetFocus
End With

i am also attaching the sample report with this mail
please rename the test.zip to test.rar

secret is in the field strTableID. this field will have same value for all the records of table 1 and different value for all the records of table2

and we grouped the recordset using strTableID

enjoy..........

regards
Manpreet Singh Dhillon Hoshiarpur
Attached Files
File Type: zip test.zip (1.4 KB, 17 views)
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#16: Sep 29 '09

re: DataReport with Two Tables


Thanks Mr. Dhillon
I will try it and will tell you about outcome.
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#17: Sep 29 '09

re: DataReport with Two Tables


Hi
As soon as F5 is pressed I get error 'Invalid Data Source' and can not debug the program because even if I use F8 soon I get the error without displaying the line of error.
Familiar Sight
 
Join Date: Oct 2007
Posts: 158
#18: Sep 29 '09

re: DataReport with Two Tables


hi smartchap
for this use following code to open connection
with cnn
.Provider = "MsDataShape"
.Properties("Data Provider") = "Microsoft.Jet.OLEDB.4.0"
.Properties("Data Source") = DataBasePath
.Open
end with
regards
manpreet singh dhillon hoshiarpur
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#19: Sep 29 '09

re: DataReport with Two Tables


Already used the similar (or should say same) code, with different variables, but error. Only after that I asked u why that error. Ok, tomorrow I will again see what is wrong with my project.
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#20: Sep 30 '09

re: DataReport with Two Tables


Dear Mr. Dhillon
Please see the attached files and help exact problem. As the error occurs even before the form is displayed, it means there is some error with datareport, but I could not find it.
Someone please help.
Attached Files
File Type: zip 2Tables1.zip (31.8 KB, 4 views)
Familiar Sight
 
Join Date: Oct 2007
Posts: 158
#21: Sep 30 '09

re: DataReport with Two Tables


hi smartchap
from the structure of the database if could not understand what data you want to retreive. to illustrate the logic i have combined tables and shown in report.

message 'invalid data source' appeared because you called rptTest on startup

i am attaching the modified project.
do not forget to change extention to .rar

regards
manpreet singh dhillon hoshiarpur
Attached Files
File Type: zip 2Tables1.zip (27.0 KB, 7 views)
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#22: Sep 30 '09

re: DataReport with Two Tables


Thanks Mr. Dhillon for quick and accurate help.
Actually I am very poor in SQL & DataReports. In last few days I have learnt a lot about DataReports and something about SQL. Before seeing reply from u, I changed SQL query a lot (with the helps got on net), but could not understand why report is opening before loading the form. Actually I didn't set it as startup, what I did was that I opened the report by u by double clicking, then added a form and pasted (& written) code in that. Due to this rptTest was set as startup which I could never imagine. Thanks for telling me the problem.
One more thing, will u please explain a little about sql query u wrote. Specially what is the use of SELECT 'Table 1' AS strTableID and similarly for Table 2.
Now I will try to modify the code as per my requirement and will let u know.

Thanks once again.
Hope zooming problem is solved as reqd by you.
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#23: Oct 1 '09

re: DataReport with Two Tables


Use of strTableID is clear. My first table contains 4 fields, second 6 fields & third 7 fields (excluding IDs). How to show them in report with / without line borders around each field. Borders are required for only 4 fields for table1 output, for table2 around only 6 fields and around all 7 fields for table3. Please help me how to achieve this.
Reply


Similar Visual Basic 4 / 5 / 6 bytes