-
[RESOLVED]printing access reports using vb.net, you be da man robdog
It's like the title said: how can I print a certain page from a report from Access using visual basic?
I hope it's called report, in dutch it's called like that anyways.
Thanks for any feedback.
-
Re: printing access reports using vb.net
Welcome to the Forums.
So are you automating Access using COM?
-
Re: printing access reports using vb.net
im using vb.net, nothing like COM i guess
-
Re: printing access reports using vb.net
Well if you have a reference set to use Access xx.0 Object Library then thats using COM, so just to be sure we are on the same page.
Usually you just reference Access and printout a report but are you shouwing the user the Access report first as in a prrint preview?
-
Re: printing access reports using vb.net
I have no idea what you're talking about, this is what I have for now: a report, a database, a database connection, a vb project which needs to print reports, and preview them first.
I have no idea whatsoever how to print, and I need all the help I can get with that...
-
Re: printing access reports using vb.net
Ok, thats what I needed to know that you were displaying access and/or the report(s).
Have you added a reference to Access via the COM tab and instanciated an Access Application object variable?
This is the VBA format:
vb Code:
expression.DoCmd.OpenReport "Report1", acViewPreview
-
Re: printing access reports using vb.net
So is that in vb .net or in vba? I'm confused.
Thank god that you're helping me, I'm a real dummy in this program when it comes to using it for external programs like access...
-
Re: printing access reports using vb.net
Its universal, so to speak, but depending on how you are integrating your db will dictate how the rest of the code will go.
Can you answer the previous questions for me and then I can help with more.
-
Re: printing access reports using vb.net
Quote:
Have you added a reference to Access via the COM tab and instanciated an Access Application object variable?
Do you have msn because I have no idea what you're talking about. If you don't mind, my msn is [email protected]
-
Re: printing access reports using vb.net
Project menu item > Add Reference ... > COM tab > select Microsoft Acces xx.0 Object Library > click OK.
Sorry, busy writting an article and after that I need to do some painting :(
-
Re: printing access reports using vb.net
Ok, I did ok after the COM tab, now what do I have to do? because I don't see anything anywhere.
np btw, do your things first; I got a month.
-
Re: printing access reports using vb.net
I'm out for today, you do your thing, I'll be back tomorrow or tuesday.
hf doing whatever you do :)
-
Re: printing access reports using vb.net
Gee, Thanks. lol. I always have fun slaving away working on home repairs. :D
I have a FAQ on Access for .NET. The code example is 2003 but if you concentrate on the button1click event, its almost all in there and the same for 2005.
It will get your db connected and ready to display the report with...
moApp.DoCmd.OpenReport("Report1", Microsoft.Office.Interop.Access.AcView)
http://vbforums.com/showthread.php?t=402075
-
Re: printing access reports using vb.net
2 errors: "name 'moApp' is not declared." and "'AcView' is a type in 'Access' and cannot be used as an expression."
as what do i have to declare moApp? and what do i have to do to get rid of the 2nd error?
-
Re: printing access reports using vb.net
Did you even look at the code example I posted in the link?Everything is in there as I posted. ;)
-
Re: printing access reports using vb.net
I looked at it
That was pretty much it.
No, I tried to understand, but it was about interaction between excel and access so what does that have to do with viewing and printing data from access? Or is the code very very very simular to it?
edit: I knew you loved doing work around the house, just for that, you get from me 2 thumbs up!! :thumb: :thumb:
-
Re: printing access reports using vb.net
No, the button1 click event shows how to initialize and connect to your db and then use the docmd method.
Here, I'll post a copy from my thread.
vb Code:
Public moApp As Access.Application
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
moApp = DirectCast(GetObject(, "Access.Application"), Access.Application)
Catch ex As Exception
If TypeName(moApp) = "Nothing" Then
moApp = DirectCast(CreateObject("Access.Application"), Access.Application)
Else
MessageBox.Show(ex.Message, "VB/Office Guruâ„¢ Access Demo", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Try
moApp.Visible = True
moApp.OpenCurrentDatabase("D:\RobDog888.mdb")
moApp.RunCommand(Access.AcCommand.acCmdAppMaximize)
moApp.DoCmd.Maximize()
moApp.DoCmd.SetWarnings(False)
'Then add the other line I posted earlier.
moApp.DoCmd.OpenReport "Report1", acViewPreview
'...
'...
-
Re: printing access reports using vb.net
what do i have to declare everything like?
I get errors of "not declared ... on these which I can't figure out what to make of:
MoApp, Access.Application, Access, AcViewPreview
I did find the mbKillMe to be a boolean :)
I'll go check the other code for declarations I think .. I'll edit if I got em all
edit: I can't declare as access.anything.At.All
-
Re: printing access reports using vb.net
How about I send you my current program + the database? I'll add in the mail what exactly the program more needs, and then you can help me, but pointing me in the direction is (should) be enough, if you do it all the way for me I don't learn anything :'(.
-
Re: printing access reports using vb.net
I edited my post and trimmed it down. It was a copy from my othe post as I was saying to get the logic out of it as that is what you need. So now as it is should work for you but I dont have time to write programs for members, just when I take breaks or ? from working. ;)
-
Re: printing access reports using vb.net
When you take breaks is fine for me.
Are you allowed to take "breaks"? :p
edit: I'm going to do this next year http://www.digitalartsandentertainment.be/
So I'll be needing C#, but I don't know anything about it yet (well, declaring but that's the same in almost all OOP languages) so you happen to know some free online "C# for dummies" ?
I've looked around in this place, but it's not really the basics, and that's what I need.
-
Re: printing access reports using vb.net
So, I type what code after the COM thing? because I still get errors like this:
"not declared ... on MoApp, Access.Application, Access, AcViewPreview
-
Re: printing access reports using vb.net
Quote:
Originally Posted by frederik
When you take breaks is fine for me.
Are you allowed to take "breaks"? :p
edit: I'm going to do this next year
http://www.digitalartsandentertainment.be/
So I'll be needing C#, but I don't know anything about it yet (well, declaring but that's the same in almost all OOP languages) so you happen to know some free online "C# for dummies" ?
I've looked around in this place, but it's not really the basics, and that's what I need.
that place looks like it teaches C++, not C#
-
Re: printing access reports using vb.net
Do you have the reference added to Access xx.0 Object Library? Do you have the Office Primary Interop Assemblies installed? The code I posted earlier is valid if you have the two requirements I just typed
-
Re: printing access reports using vb.net
Office primary interop assemblies?
How, where, what?
-
Re: printing access reports using vb.net
-
Re: printing access reports using vb.net
intresting.
edit: I'll repost tomorrow or in the weekend if I have problems, me sleepy now, 23:40 here right now.
Gl, Hf.;)
-
Re: printing access reports using vb.net
Ok, I'm having problems: how do I install the pias stuff when I got both office and .net installed?
Or do I just have to do the COM reference? because it looks quite the same to me.
-
Re: printing access reports using vb.net
ok, it looks like I got the pias thing, so I'm guessing I did something wrong in the COM reference, going to do it again, going to try your code again, and I'll post later to let you know what happens.
-
Re: printing access reports using vb.net
Ok, I tried the COM thing again, but it doesn't do anything: I do as you say, I go to project> add reference> COM> Microsoft Access 11.0 Object Library
Then I click Select, do Ok, but nothing happens/changes after that.
What did I do wrong?
btw: I checked the pias stuff in my assembly map, and it's there (microsoft.office.interop.access)
-
Re: printing access reports using vb.net
Add the imports....
Imports Microsoft.Office.Interop
-
Re: printing access reports using vb.net
"imports statements must procede any declarations"
I'll still have to edit this code for my own report, but what's wrong with the imports... ?
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Imports Microsoft.Office.Interop
Dim mbKillMe As Boolean
Dim moapp As Access.Appliaction
Try
moApp = DirectCast(GetObject(, "Access.Application"), Access.Application)
mbKillMe = False
Catch ex As Exception
If TypeName(moApp) = "Nothing" Then
moApp = DirectCast(CreateObject("Access.Application"), Access.Application)
mbKillMe = True
Else
MessageBox.Show(ex.Message, "VB/Office Guruâ„¢ Access Demo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Try
If mbKillMe = False Then
If moApp.CurrentDb.Name <> "D:\RobDog888.mdb" Then
moApp.OpenCurrentDatabase("D:\RobDog888.mdb")
End If
Else
moApp.Visible = True
moApp.OpenCurrentDatabase("D:\RobDog888.mdb")
End If
moApp.RunCommand(Access.AcCommand.acCmdAppMaximize)
moApp.DoCmd.Maximize()
moApp.DoCmd.SetWarnings(False)
expression.DoCmd.OpenReport("Report1", acViewPreview)
End Sub
-
Re: printing access reports using vb.net
Goes at the top of your file but right below the "Option Explicit On" or "Option Strict On" statement(s).
-
Re: printing access reports using vb.net
what statements?
sorry if I'm such a crappy vb'er, but I've never done what you're saying before. Also never seen option strict/explicit yet.
-
Re: printing access reports using vb.net
You should be using them. They help to require variable definition and validate casting between data types.
vb Code:
Option Explicit On
Imports Microsoft.Office.Interop
Public Class Form1
Private Sub Button1_Click(Blah, blah...
-
Re: printing access reports using vb.net
what do i have to declare the expression like?
Code:
expression.DoCmd.OpenReport("Report1", acViewPreview)
this one.
-
Re: printing access reports using vb.net
-
Re: printing access reports using vb.net
Yes, I found it. (even before you posted it WOW)
I debugged it, (changed the database pad and report name ofc), but after clicking the button I get an error on the moApp: "An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in printen.exe
Additional information: The expression contains an invalid * to the attribute Visible."
In dutch the word is "verwijziging", but I don't know how to translate it ...
Like ... it points/directs to something else.
-
Re: printing access reports using vb.net
Take the "moApp.Visible = True" line out by commenting it. Some versions of Access may not support it I think.
-
Re: printing access reports using vb.net
YES, ITS ALIVE, ITS ALIVE
Now, how can I select a certain page from the report? Because it's 676 pages long, and I don't think the guy giving me work(what's that in english again...) wants to print 676 pages every time he wants a report.
edit: This is the actual thing: need to be able to search a certain record out of a datagrid, and all that data: meaning the report made of that record.
But I think it's quite impossible to do that (unless you prove me wrong) so is it possible to show the report in a visual basic window, choosing a report there and printing that one?