Clicking a button in Access Form using VBA Excel - vba

This is my code below. I am getting an error "Object doesn't support this property or method" on controls.click. Anyone there who can help?
'create new access object
Set appAccess = CreateObject("Access.Application")
'open the acces project
Call appAccess.OpenCurrentDatabase("D:\NSE Cash\db.accdb")
appAccess.Visible = True
With appAccess
Application.DisplayAlerts = False
.DoCmd.OpenForm "Form1"
.Forms("Form1").Controls("Run").Click
End With
Set appAccess = Nothing

First, the button Click event cannot be Private - remove Private from the procedure declaration. Then, this works for me:
.Forms("Form1").Run_Click

Related

My access to word merge code is not working

I have made a form in access and I'm trying to code a "merge to word" button but my code has a problem and I dont know how to fix it.
Here's the code:
Private Sub Command102_Click()
Dim LWordDoc As String
Dim oApp As Object
'Path to the word document
LWordDoc = "C:\school\information tech\document.docx"
If Dir(LWordDoc) = "" Then
MsgBox "Document not found."
Else
'Create an instance of MS Word
Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = True
'Open the Document
oApp.Documents.Open FileName:=Document.docx
End If
End Sub
If someone could help that would be great. Ill attach screenshots of the error I get when I click the button and the code as the debugger is pointing to the problem. (pointing to the start of the "oApp.Documents.Open FileName:=Document.docx" line)
Thanks heaps
Since you create a variable to hold the filename, try using that:
oApp.Documents.Open FileName:=LWordDoc

Run-time error '424' Object Required when doing userform.show on imported userform

I am attempting to import a userform, and show it on initial startup. The userform imports just fine, however, when attempting to show it, I keep getting a Run-time error '424' Object Required.
Here is my code:
Sub Workbook_Open()
Dim wkbTarget As Excel.Workbook
Dim szTargetWorkbook As String
Dim cmpComponents As VBIDE.VBComponents
Application.ScreenUpdating = False
szTargetWorkbook = ActiveWorkbook.Name
Set wkbTarget = Application.Workbooks(szTargetWorkbook)
Set cmpComponents = wkbTarget.VBProject.VBComponents
' IMPORT FORM
cmpComponents.Import "\\myserver.domain\Application\Forms\LOGIN.frm"
LOGIN.Show
End Sub
When I click End on the error, I can then show the userform just fine.
The run-time (thanks #Comintern!) won't like you referring to an object that doesn't exist yet. You could use:
Userforms.add("LOGIN").Show
to avoid that direct reference.

Trouble Running Access Macro from Excel VBA

I'm trying to run an Access Macro from VBA and keep getting error:
Run-time Error 2485; Access cannot find the object 'MyTest'
My code is below - it is odd because the line:
A.DoCmd.OpenModule "temp", "MyTest"
works (opens the module to the correct location).
The macro is a simple test one; all paths are correct so far as I can tell. Thanks!
Set A = Nothing
Set A = CreateObject("Access.Application")
A.Visible = False
A.OpenCurrentDatabase (DBFileName)
A.DoCmd.OpenModule "temp", "MyTest"
A.DoCmd.RunMacro "MyTest"
A.CloseCurrentDatabase
A.Quit
Set A = Nothing
I am not sure if this is what you are looking for, but the following Excel macro starts the macro "MyTest" from a module which is included in the Access file "filename":
Sub test_accesss()
Set A = Nothing
Set A = CreateObject("Access.Application")
A.Visible = False
A.OpenCurrentDatabase ("filename")
A.Run "MyTest"
A.CloseCurrentDatabase
A.Quit
Set A = Nothing
End Sub

Tabs, Ribbons, and Menus not responsive after VBA (Word 2007) generation of labels from IBM Notes

I am generating a set of labels using Word 2007, controlled via VBA from IBM Notes.
The document generates fine but after the generation the Tabs, ribbons and Menus are not responsive. I can click in the generated document but clicking on any of the top controls has no effect (icons do not press in, etc).
As a workaround I can click on another window, fiddle there, and then the ribbon for that particular document reacts. I am assuming that it has something to do with a focus problem.
After the code is finished, I am setting the Word Application.Visible to True, ScreenUpdating to True. I am calling a 'Close' to close all open files, but still, it's to no avail.
The code is being called by an IBM Notes Database in Lotusscript.
Have you encountered this before? It's very puzzling, and a no-go for my customers.
Andrew
Main Function:
Dim p As New LabelSourceFile
Call p.GenerateFileForSelectedDocuments()
Call p.ExtractWordTemplateFromConfig()
Dim w As New WordExport
w.setSelectedTemplateFullFile(p.FilePathToTemplate)
Print "file path to template: " + p.FilePathToTemplate
Call w.InitializeWordDocument()
Dim finaldoc As Variant
set finaldoc = w.MailMergeWithThisFile(p.getDataFileStreamFileName())
Call w.ReplaceCRWithCarriageReturns(finaldoc)
finaldoc.Activate 'gives focus back to document I've just generated
Set finaldoc = Nothing
'p.DeleteVorlage
'Kill p.FilePathToDataFile
Call w.ReleaseToUser()
This function initializes my document:
Public Function InitializeWordDocument As Integer
'Initialize a Word Document Object
' If m_strSelectedTemplateFullFile = "" an empty document is created
InitializeWordDocument = False
'On Error Goto ErrorHdl
m_objWordApplication.DisplayAlerts = wdAlertsnone
If Not m_objWordApplication Is Nothing Then
If m_strSelectedTemplateFullFile <> "" Then
Set m_objWordDoc = m_objWordApplication.Documents.Add( m_strSelectedTemplateFullFile )
InitializeWordDocument = True
Else
Set m_objWordDoc = m_objWordApplication.Documents.Add()
End If
Set m_objCurrentRange = m_objWordDoc.Range(0,0)
End If
m_objWordApplication.DisplayAlerts = wdAlertsAll
Exit Function
ErrorHdl:
InitializeWordDocument = False
Exit Function
End Function
This function is actually doing the merging:
'/*************************************************************************************
' * Function MailMergeWithThisFile
' * #param datafilename the data file
' * #return the final merged word document
' * #author Andrew Magerman/Magerman/NotesNet
' * #version Dec 18, 2013
' *************************************************************************************/
Function MailMergeWithThisFile (datafilename As String) As variant
On Error GoTo ErrorHandler
'If I don't block the popups, there is an annoying pop-up that appears.
m_objWordApplication.DisplayAlerts = wdAlertsnone
With me.m_objWordDoc.MailMerge
.MainDocumentType = wdFormLetters
Call .OpenDataSource(datafilename, wdOpenFormatText, false)
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute(False)
End With
'I need to set this here, immediately after the generation of the destination, merged document because the later
'actions of closing change the active document.
Set MailMergeWithThisFile = m_objWordApplication.ActiveDocument
Call m_objWordDoc.Close(False)
m_objWordApplication.DisplayAlerts = wdAlertsAll
Exit Function
ErrorHandler:
Call logError
Exit Function
End Function
By brainlessly commenting out lines until I got an answer, here is my workaround:
Always create a new instance (CreateObject) and forget about the GetObject.
If anyone has a good explanation for this, I'd be happy to hear it. For the moment I am just going to use the workaround.
'Initialize the Word Object
Set m_objWordApplication = Nothing
'I create always a new Word instance because that avoids me having focus issues.
' Set m_objWordApplication = GetObject("","word.application")
' If m_objWordApplication Is Nothing Then
Set m_objWordApplication = CreateObject("word.application")
' End If

Using VBS to open MS Project file as read only

I am trying to find a quick way to open MS Project files as read-only, preferrably from explorer. I was following the tutorial head to do the same thing from excel using this tutorial which uses the following code:
'-------------------- Open as Read-Only.vbs --------------------
Option Explicit
Dim app 'As Object
Call OpenAsReadOnly
Sub OpenAsReadOnly()
On Error Resume Next
Set app = GetObject(, "Excel.Application")
If IsEmpty(app) Then Set app = CreateObject("Excel.Application")
With app.Workbooks.Open(WScript.Arguments(0))
If Not .ReadOnly Then .ChangeFileAccess 3 'xlReadOnly
app.Visible = True
End With
End Sub
but when I try to modify it for Project like this:
'-------------------- Open as Read-Only.vbs --------------------
Option Explicit
Dim app 'As Object
Call OpenAsReadOnly
Sub OpenAsReadOnly()
On Error Resume Next
Set app = GetObject(, "MSProject.Application")
If IsEmpty(app) Then Set app = CreateObject("MSProject.Application")
With app.Project.Open(WScript.Arguments(0))
If Not .ReadOnly Then .ChangeFileAccess 3 'xlReadOnly
app.Visible = True
End With
End Sub
MS Project flashes open then closes. Any ideas?
The method to open MS Project files is app.FileOpenEx. The first parameter is the file name and the second is the ReadOnly option. There is no ChangeFileAccess method in MS Project.
Try this instead:
Dim app
Set app = GetObject(, "MSProject.Application")
If IsEmpty(app) Then Set app = CreateObject("MSProject.Application")
app.FileOpenEx(WScript.Arguments(0), True)