How to suppress "SQL Server Login" window on Excel? - sql

I am using Microsoft Task Scheduler to run Excel models everyday. The models retrieve data from the database and do stuff. The connection strings contain all information(username, password etc.) needed to connect to the database. However, I get this prompt sometimes:
SQL Login Prompt
When I click "OK", everything goes fine, so I need the Excel to just click OK because the username and password are already there. I want to suppress this window by clicking OK. Otherwise the models stop running and wait for me to click OK manually.
I checked for the solutions on the web but did not encounter a working one. I tried to change the connection string in various ways but it did not work.
Does anyone have any idea on how to solve this issue?
Thanks
Edit: Here is the code I am running on VBScript for Task Scheduler.
'Input Excel File's Full Path
ExcelFilePath = "MyExcelFile"
'Input Module/Macro name within the Excel File
MacroPath = "Macro1"
'Create an instance of Excel
Set ExcelApp = CreateObject("Excel.Application")
'Do you want this Excel instance to be visible?
ExcelApp.Visible = True 'or "False"
'Prevent any App Launch Alerts (ie Update External Links)
ExcelApp.DisplayAlerts = False
'Open Excel File
Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)
'Execute Macro Code
ExcelApp.Run MacroPath
WScript.Sleep 10000
'Input Module/Macro name within the Excel File
MacroPath = "Macro2"
'Execute Macro Code
ExcelApp.Run MacroPath
WScript.Sleep 10000
'Save Excel File (if applicable)
wb.Save
'Reset Display Alerts Before Closing
ExcelApp.DisplayAlerts = True
'Close Excel File
wb.Close
'End instance of Excel
ExcelApp.Quit
The excel VBA code for Macro1 is just
Sub UpdateModel()
ActiveWorkbook.RefreshAll
End Sub
command for pivot tables which are getting data from the database.

Related

excel vba suppress "downloading file" message

I realize this question has been asked before, but the supplied answers did not solve the problem. I'm running VBA code from within Excel which does the following:
1.) Open a local worksheet (i.e. C:\desktop\AOR\Names.xlsx)
2.) Read down column B getting the names of files that need to be verified
3.) Open the filename (as taken from column B) from a Network drive.
4.) Verify some cells from the opened file
5.) Close the file.
6.) Iterate.
All this is working with no problems; my situation is that when the file is opened from the Network drive (i.e. step 3) I get the "Downloading File" message. The only option on this is "Cancel" or let it go to completion. Pressing cancel (or using SendKeys to do so) causes the file open to cancel, thus stopping the rest of the process. I've had users accidentally press this cancel enough times that I want to suppress the dialog box completely but cannot find anything that works.
Here is some code with the options I've tried all to no avail:
WhatName = Workbooks(AgencyNamesList).Worksheets("Agencies").Range("B" & i).Value ' get name of file from locally opened workbook.
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Application.EnableEvents = False ' Prevent code in workbook from running
' Application.SendKeys "{ESCAPE}", False ' Cancels subsequent file opens
' Application.SendKeys "{ESCAPE}", True ' Cancels the file open
' Application.SendKeys "~", True ' Cancels the file open
QuarterDueDate = Workbooks.Open(SourceFolder & WhatName).Worksheets("YTD").Range("I6").Value ' open book get value of I6
This page shows a picture of the dialog box along with someone else trying to find a solution (since 2012) to this problem.
OPSYS: Windows 7 Enterprise SP1
EXCEL: MS Office Professional Plus 2013
I'm sure hoping someone has found a way to prevent this pop-up box from popping!
NOTE: When this code is run against the same files stored locally, the dialog box does NOT display.
Thanks!

EXCEL 2nd Enable Editing Button appears after already Enabling

I have an excel workbook that I download using simple HTML portal that I coded. When I download the workbook it automatically renames it as "myWorkbook (27)" or whatever number of times it has been put in the downloads folder.
I have some code that changes the Save As location of the workbook to the original location that it was downloaded from. See:
Private Sub Workbook_Open()
Application.DisplayAlerts = False
'Check to see if another workbook with the same name is open -- close if so.
If IsOpenWorkbook("myWorkbook.xlsm", True) Then
Workbooks("myWorkbook.xlsm").Close saveChanges = False
End If
ThisWorkbook.SaveAs Filename:="\\999.9.9.99\folder\anotherFolder\myWorkbook.xlsm"
Application.DisplayAlerts = True
End Sub
This has worked fine for almost a month but recently has been performing erratically. When I first download the sheet, it has the regular "Enable Editing" prompt which I accept. After accepting, the name of the workbook changes suggesting the code as run, but then has another "Enable Editing" prompt which, if I accept, will crash Excel.
Any insights appreciated.

MS Excel macro runs different from VB Script?

I have a VB Script file that I open using a task in Windows Task Scheduler each morning. The script opens MS Excel and then runs a macro stored in a workbook. The problem is that the macro runs differently if I use the VB Script file to kick off the macro versus when i run it myself from the workbook. Specifically, I find that "Refresh All" doesn't actually refresh all data connections when I run the macro starting from the VB Script file. It works fine if I run the macro from the workbook itself. I'm thinking that I am missing something in this VB Script to ensure MS Excel opens correctly and makes "refresh all" work properly. Anything plainly wrong with this VB Script?
Dim ObjExcel, ObjWB
Set ObjExcel = CreateObject("excel.application")
ObjExcel.Application.Visible = True
Set ObjWB = ObjExcel.Workbooks.Open("K:\Analytic Reporting\11.Projects\TonyAdHoc\Autorefresher\DashboardAutorefresher.xlsm")
objExcel.Application.Run "DashboardAutorefresher.xlsm!Main"
ObjWB.Close True
ObjExcel.Quit
Set ObjExcel = Nothing
It seems that you are trying to do a refresh to pull data from a database, but you are not seeing any new data from this script when you manually open the Excel file after the script runs.
You will need to save the Excel file after making modifications.
ObjExcel.Save
For anyone still caring about this, what I've found is that for connections to a database table, if the property "Enable Background Refresh" is selected, then the vba command in RefreshAll in the marco doesn't work. So I add a little loop to disable that property for all the connections just to make sure before I call RefreshAll. That fixed it for me.
' -- make sure enable background refresh is not checked else RefreshAll doesn't work
Dim cn As WorkbookConnection
For Each cn In ActiveWorkbook.Connections
cn.OLEDBConnection.BackgroundQuery = False
Next cn
' -- then ok to refresh all
ActiveWorkbook.RefreshAll

Shared Excel Document

I've got a Excel documents with macros with input and update forms for information, the document is used as a monthly database where you can run reports from as well.
The issue is that when using the document sometimes the input and update options are used at the same time causing information loss. Both the input and output save at the end of the macro to minimise the losses, but I was wondering if there is anyway of checking at runtime if there is a macro being use by another user and if so delay the next macro run until the other user is finished?
There is one way I can think of. Logically it should work. However I have not tested it.
Create a temp sheet and hide it
When anyone runs a macro, check if cell A1 of that sheet is empty or not
If it is empty then run the macro
Before running the macro, write to that cell and once the macro is run, clear the contents of the other cell
Sandwich your macro code as mentioned below
Code
Sub Sample()
Dim ws As Worksheet
ThisWorkbook.Save
Doevents
Set ws = ThisWorkbook.Sheets("HiddenSheetName")
If Len(Trim(ws.Range("A1").Value)) = 0 Then
ws.Range("A1").Value = "Macro Starts"
ThisWorkbook.Save
Doevents
'
'~~> Rest of your code goes here
'
ws.Range("A1").ClearContents
ThisWorkbook.Save
Doevents
Else
MsgBox "Please try after some time. There is a macro running... Blah Blah"
End If
End Sub
CAUTION: Once the code runs, you cannot undo the changes since the code save the file programatically. The changes are permanent. In a file which is not shared, you can undo by closing the file without saving and re-opening it.

Trouble running Excel Add In macro from MS Access 2010 procedure

I'm using VBA code in MS Access to export a query as an Excel workbook. I have a separate procedure that should open the workbook from MS Access and run a macro to format the workbook. The macro is stored in a separate Excel Add-In (.xlam) workbook.
My problem: I'm getting a 1004 can't run macro error, because "the macro can't be found or macros have been disabled." I tried enabling all macros in the Excel Trust Center options and I opened the Add-In workbook separately and re-ran the procedure. I'm getting the same error.
I've searched online for an answer, but I haven't found anyone who has successfully ran a macro stored in a separate Add-In workbook from a MS Access procedure. Anyone care to try and help?
Here's my MS Access procedure that's giving me problems:
Private Sub RunExcelTrackerMacro(strFileName As String)
Dim xl As Object
' Create excel app.
Set xl = CreateObject("Excel.Application")
' Open workbook to be formatted via a macro.
xl.Workbooks.Open (strFileName)
xl.Visible = False
' Run the macro, stored in a separate add-in (.xlam) workbook.
' This procedure fails to run the public Call_FormatTracker macro, which
' is stored in an Add-In in a module called "MacroCalls."
xl.Run "MacroCalls.Call_FormatTracker"
' Save and close the workbook.
xl.ActiveWorkbook.Close (True)
' Close excel app.
xl.Quit
Set xl = Nothing
End Sub
What about changing the xl.Run line to something like xl.Run "Sales.xlam!Orders"
Not sure what your xlam is called, but just change to suit.
I had a similar problem running a macro in an add on. Although the addin was available when I opened Excel directly it was not when Excel was called from Access.
After you create the Excel object add the line below.
Call ReloadXLAddins(xl)
Function ReloadXLAddins(oExcel) ' As Boolean
' Dim CurrAddin As Excel.AddIn
For Each CurrAddin In oExcel.AddIns
If CurrAddin.Installed Then
CurrAddin.Installed = False
CurrAddin.Installed = True
End If
Next CurrAddin
End Function