|
-
May 7th, 2009, 03:26 PM
#1
Thread Starter
Addicted Member
Access Automation
Here is my code which is supposed to open an access report for previewing rather than printing. For some reason when the code runs it kinda looks like it doing something for a few seconds butr nothing is displayed.
Can anyone advise why this is. This is the first time ive tried any automation of application so im not sure whats going wrong.
Code:
Dim FileName As String = LoginForm.Filename
Dim oAccess As Access.Application
' Start a new instance of Access for Automation:
oAccess = New Access.ApplicationClass()
Dim sDBPassword As String
Dim oDBEngine As DAO.DBEngine
Dim oDB As DAO.Database
sDBPassword = "A5B7C5D8E4" 'database password
oDBEngine = oAccess.DBEngine
oDB = oDBEngine.OpenDatabase(Name:=FileName, _
Options:=False, _
ReadOnly:=False, _
Connect:=";PWD=" & sDBPassword)
oAccess.OpenCurrentDatabase(filepath:=FileName, _
Exclusive:=False)
Dim ReportName As String = "Autoglass Totals Report"
oAccess.DoCmd.OpenReport(ReportName:=ReportName, View:=Access.AcView.acViewPreview)
oDB.Close()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oDB)
oDB = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(oDBEngine)
oDBEngine = Nothing
Last edited by mouse88; May 7th, 2009 at 03:37 PM.
-
May 7th, 2009, 05:49 PM
#2
Re: Access Automation
When automating any Office application you need to set the Application.Visible property to True if you want to see anything. When using Automation you need to ensure there is a way to respond (or ignore) any dialogs/messageboxes that the Application might display.
The code that opens the database using DAO doesn't seem to belong there, unless you haven't posted all your code. Typically you use one method or the other (Automation or DAO).
This is VB6 code but its pretty much the same for .NET.
Code:
Dim FileName As String
Dim ReportName As String
Dim oAccess As Access.Application
ReportName = "Sales Totals by Amount"
FileName = "c:\testing\nwind2000.mdb"
Set oAccess = New Access.Application
oAccess.Visible = True
Call oAccess.OpenCurrentDatabase(filepath:=FileName, Exclusive:=False, bstrPassword:="dude")
Call oAccess.DoCmd.OpenReport(ReportName:=ReportName, View:=Access.AcView.acViewPreview)
Set oAccess = Nothing
-
May 9th, 2009, 08:13 AM
#3
Thread Starter
Addicted Member
Re: Access Automation
Ok cool problem solved. Is there a way of opening the database as read only so the user cannot modify anything in the database?
-
May 10th, 2009, 12:51 PM
#4
Thread Starter
Addicted Member
Re: Access Automation
Can anyone help with this? Have tried googling it but havent had any luck.
-
May 10th, 2009, 02:33 PM
#5
Re: Access Automation
You could add some security like run a macro that removes toolbars and menus but they can easily bypass that if they hold the Shift key down when opening the db. Better would be to use Access Workgroup Security. See Access Security in my Office FAQ for more on it.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
May 10th, 2009, 02:52 PM
#6
Thread Starter
Addicted Member
-
Jun 5th, 2009, 08:11 AM
#7
New Member
Re: Access Automation
Hi there, I have one question... I also want to automate an access that generates an .xls report. I have an file .mdb that is used to generate this report. I REALLY can't change this file. I'm using an vbs script that manipulates the access in order to generate my reports. Here's my vbs code:
Code:
'Criação de objeto Access.
Set oAccess = CreateObject("ACCESS.Application")
'Seta a visibilidade do programa em execução.
oAccess.Visible = True
'Seta o aviso de segurança para o nível mais baixo.
oAccess.automationsecurity=1
'Abre o arquivo de batimento (.mdb)
oAccess.OpenCurrentDatabase("D:\Intelig\Sistemas\Interconnect\Relatorios\Interface_Batimento_v11.mdb")
'Abre o Formulário
oAccess.DoCmd.OpenForm "FrmRelat", , , stLinkCriteria
Dim a
a = "C:\BATIMENTO\REL_BAT_125_DIV_DET_20090520_111111.xls"
Call sCriaExcell(125, a , 1)
My problem Is: I need to call the function sCriaExcell (This function is on my mdb file)
here's the code for sCriaExcell:
Code:
Public Function sCriaExcell(pBat_Code As Integer, _
pArqSaida As String, _
pTipoRelat As Integer)
On Error GoTo Error_Handle
Dim iCount As Integer
Dim i As Integer
If StrComp(Dir(pArqSaida), "") <> 0 Then
Kill pArqSaida
End If
DoCmd.Hourglass True
' Create the Excel App Object
Set xlApp = CreateObject("Excel.Application")
' Create the Excel Workbook Object.
Set xlBook = xlApp.Workbooks.Add
' Excluir as Sheets
iCount = xlApp.Worksheets.Count
wSheet = 1
xlApp.Worksheets(wSheet).Activate
xlApp.Worksheets(wSheet).Name = "Relatório"
xlApp.Worksheets(wSheet).Visible = True
xlApp.ActiveWindow.DisplayGridlines = False
Call sGerar_Rel_Analise(pBat_Code, pTipoRelat)
xlApp.Worksheets(wSheet).Cells.Select
xlApp.Worksheets(wSheet).Cells.EntireColumn.AutoFit
xlBook.SaveAs pArqSaida
xlBook.Close savechanges:=True
xlApp.Quit
MsgBox "Arquivo " & pArqSaida & " foi Gerado"
Exit_Proc:
On Error Resume Next
DoCmd.Hourglass False
Set xlBook = Nothing
Set xlApp = Nothing
On Error GoTo 0
Err.Number = 0
Exit Function
Error_Handle:
MsgBox "Cod.: " & Err.Number & vbCr & _
"Descr.: " & Err.DESCRIPTION & vbCr & _
"Contexto.:" & Err.HelpContext
Resume Exit_Proc
End Function
so, how can I call sCriaExcell from my vbs script? Everytime I try I get "mismatch exception" or "can't use () when calling a sub" things like that..
thanks for the help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|