Running Access Macro whilst open using Batch file - ms-access

I have a batch file which contains a path to the access database. When clicked it opens up the desired database however I want the batch file to run the macro.
I can do this by creating a AutoExec macro to run when you open up the database but I also want the macro to run whilst the database is running.
Here is what I have on my batch file at the moment:
echo off
start "C:\Program Files (x86)\Microsoft Office\Office\MSACCESS.EXE" "S:\DB NEW\Report.accdb"
How do I do this?

You can use the /x "macroname" command line indicator to run a macro (not a VBA sub or function, a macro):
echo off
start "C:\Program Files (x86)\Microsoft Office\Office\MSACCESS.EXE" "S:\DB NEW\Report.accdb" /x MyMacroName
Read more: Command-line switches for office.
Alternatively, you can read commands passed using the /cmd switch from VBA. You could use an auto-starting form that reads a command and executes a VBA function on load, or a predeclared class that reads and executes the command on initialize.
For more advanced functionality, I recommend switching to either PowerShell or VBScript. That way you can hook into running Access Application objects and get them to execute code.

Related

How to create a transaction log on WinSCP on SSIS package

I have SSIS package, which uses winscp to execute some commands in a text file to upload files to a FTP site. I use DTExec.exe to execute this SSIS package. Right now, I need to show winscp FTP log including display file name uploaded on FTP site. Does someone know how to capture the log? I use '>' and it does not work.
WinSCP can generate its own log using /log command-line switch (or /xmllog):
winscp.com /script=script.txt /log=winscp.log
If you want to just capture WinSCP console output:
Make sure you are using winscp.com, not winscp.exe.
Run winscp.com via cmd.exe, as that is what understands the >.
cmd.exe /C winscp.com /script=script.txt > winscp.log
In both cases, you will typically need to use full paths to all files (winscp.com, script.txt, winscp.log).

Executing a batch file to create a test file with xp_cmdshell - differentt results

I'm trying to execute a batch file in SQL Server 2008 Express using xp_cmdshell. If I use it to execute a batch file that contains the following command:
echo > C:\development\test\itworks.txt
a file called "itworks.txt" is created and inside it text says "ECHO is on".
But if I run a batch file that contains the command:
CD. >test1.txt
it doesn't work (no error, just nothing created)
and neither does:
type NUL > test2.txt
although both those batch files do create the file if double clicked/run from command prompt. I thought it might be a permissions error (I hadn't tried the echo command at that point), so changed file permissions so that NTAuthority (which is what the SQLServer service runs as) had full control over the folder but it still didn't work. Nothing in event logs. I'm a novice at DOS commands so I don't really understand the different commands. Does anyone have any idea what might be going on?
If you test the commands directly at a command prompt, they work. The cd statement produces a text file containing the name of the current directory; the type statement produces a zero-byte file, but it does indeed produce a file.
Most likely, xp_cmdshell is executing in a folder where the account it's running under has no write privileges, and you're not specifying another location for the file to be written. (The echo statement that works specifies a folder location for the text file, while your other two don't.)
Change your batch file to:
cd > C:\development\test\test1.txt
or
type NUL > C:\development\test\test1.txt
If the echo statement works there when run via xp_cmdshell, you know it's writable by the NTAuthority account.

How to fix exit code issue while using WinSCP for SFTP from within SSIS package?

I am using SSIS 2005 to do some SFTPtasks using WinSCP. I googled about the error but couldn't make it work. I got this error:
[Execute Process Task] Error: In Executing "C:\Program Files\WinSCP\WinSCP.exe" "-script=C:\Documents and Settings\nian_z\Desktop\temp\SSISMovingSOA\removeSOA8.txt" at "C:\Program Files\WinSCP", The process exit code was "1" while the expected was "0".
Here is my WinSCP script that I use:
option batch abort
option confirm off
#open sftp://user:password#server:22
#cd /m/vo/Cont/fileftp
get OrderOutbound*
close
exit
For above script, I even tried leaving only one statement at a time, but still got error.
Here is the setup of the execute process task.
This is not an answer but an attempt to help you find a solution to your problem.
Try this:
Try the following steps to find out if your script is actually working outside of SSIS or not.
Click Windows Start and click Run...
Type cmd to open Command Prompt.
On the command prompt window, type the following command at the prompt to switch to the WinSCP installation directory. I have the WinSCP installed in the following directory. Change the path according to your environment settings.
cd "C:\Program Files (x86)\WinSCP"
If your WinSCP script file removeSOA8.txt is located in the path C:\Documents and Settings\userid\Desktop\temp\SSISMovingSOA\ with spaces, then type the following command by enclosing the script path in double quotes to run the script and also use the /log option to capture all the status messages.
WinSCP.exe "/script=C:\Documents and Settings\userid\Desktop\temp\SSISMovingSOA\removeSOA8.txt" /log=C:\temp\WinSCP_log.txt
After the script executes, your will find that a log file named WinSCP_log.txt will be created in the path C:\temp. Read through the file to identify if there are any error messages.
Attempt to run FTP in SSIS:
I tried downloading a file from FTP using WinSCP with the following script:
option batch abort
option confirm off
open ftp://myuserid:mypassword#ftp.myftpsite.com:21 -passive=on
cd /root/somefolder/
option transfer binary
get SomeFileOnFTP.txt c:\temp\
close
exit
Here are the settings how I have configured the Execute Process Task within the SSIS package.
The process ran successfully in BIDS.
Hope that gives you an idea.
If you are running the script from SQL Server job agent then mostly you will get this exit error.
Scenario: In my case I faced when i m trying to download a file from sFTP server.
Solution :
Step 1 - Create a batch file
#echo off
CD "C:\Program Files (x86)\WinSCP"
winscp.com /ini=nul /script=C:\path\download_script_pm.txt
Exit
Note: When calling using SQL Server Job agent make sure to NULL out the “ini” configuration else it will throw up this error
Step2 – create a txt file (winscp script)
option echo on
option batch on
option confirm off
open sftp://user:password#sftp.xxxx.com/ -hostkey="ssh-edXXXX-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx"
option confirm off
get -filemask=">=1D" /sftp_Path/File_Name_.csv
C:\local_path_for_downloaded_file\"
exit
Note: make sure to use hostkey. To obtain host key you need to do the following :
1. Open winscp app tool and login to the server
Click Session -> Generate Session / URL code option
Check SSH Host key and copy the finger print value and use in the winscp hostkey value
Earlier I was getting the same error but now is working fine for me.
SSIS Execute Process Task :
Executable - C:\SSIS\SSIS_2008_Projects\HRIS_RepomanFusion_Load\WinSCP\WinSCP.com
Arguments - "/script=wscpBatchTest.txt" /log=C:\SSIS\SSIS_2008_Projects\FTP_Load\WinSCP\WinSCP_log.txt
Working Directory - C:\SSIS\SSIS_2008_Projects\HRIS_RepomanFusion_Load\WinSCP
Here is the WinSCP script that I use: wscpBatchTest.txt
option batch
option confirm off
open sftp://username:password#ftp.test.com
option transfer binary
cd /home/SAFAA
get employeedetails.csv C:\SSIS\SSIS_2008_Projects\SAFAA\InputDirectory\
close
exit

SSIS package not running when called as step in SQL Job

I have a .dtsx file (an SSIS package) that downloads files from an FTP server and imports data. It runs fine whenever I run it manually. However, when I schedule calling the package as a step in a SQL server agent job, it fails. The step it fails at is the one where I call a .bat file. The error in the job history viewer says this:
Error: 2009-05-26 12:52:25.64
Code: 0xC0029151 Source: Execute
batch file Execute Process Task
Description: In Executing
"D:\xxx\import.bat" "" at "", The
process exit code was "1" while the
expected was "0". End Error DTExec:
The package execution returned
DTSER_FAILURE (1).
I think it's a permissions issue, but I'm not sure how to resolve this. The job owner is an admin user, so I've verified they have permissions to the directory where the .bat file is located. I've tried going into Services and changing the "Log On As" option for SQL Server Agent, and neither option works (Local System Account and This Account). Does anyone have ideas as to what other permissions need to be adjusted in order to get this to work?
I tried executing just the batch file as a SQL Job step, and it gave more specifics. It showed that it failed when I was trying to call an executable, which was in the same directory as my .bat file, but not in the windows/system32 directory, which is where it was executing from.
I moved the executable to the system32 directory, but then I had no clue where my files were being downloaded to. Then I found that there's a property for the Execute Process Task (the one that executes the .bat) called WorkingDirectory. I set this to be the directory where the bat is located, moved the executable back into the same one as the .bat file, and it's now working as expected.
For me it was a permissions issue. Go to Environment --> Directories, then change Local directory to something the SQLAgentUser can access. I used C:\temp. Click the dropdown for Save, and choose "Set defaults".
Are you executing the SSIS job in the batch file, or is the batch file a step in the SSIS control flow?
I'm assuming the latter for this answer. What task are you using to execute the batch file (e.g. simple execute program task or a script task). If the latter, it looks like your batch file is actually failing on some step, not the SSIS script. I'd check the permissions of what your batch file is trying to access
In fact, it might be a better idea to rewrite the batch file as a script task in SSIS, because you'll get much better error reporting (it'll tell you which step in the script fails).
You could try executing the batch file using the runas command in a command window. If you try and execute it under the local system or network system account, it should give you a better error. If it does error, you can check the error level by going "echo %ERRORLEVEL%".
If it wasn't the latter, and you're executing the SSIS package via a batch file, why?
Are you possibly accessing a mapped drive in your .bat file? If so, you can't rely on the mapped drive from within the service, so you'd have to use UNC path.
I had the same error and I resolved it by logging on to the user account that runs the job, opened Coreftp site in question there, test the site access, made the change there (in my case, I had to reenter the new password) and now it works.
So yes, it is an issue of file access. This one is file access to the coreftp site in question.

automatically running an access macro

I have an access macro, that I want to run automatically from a batch file which will be scheduled with windows scheduled tasks. At the moment however the batch file will not continue until I close the access window after it is finished. Is there a way to get access to do this silently?
The line I am using is
"c:\Programme\Microsoft Office\OFFICE11\MSACCESS.EXE" "c:\Dokumente und Einstellungen\hom\Anwendungsdaten\BayWotch4\baywotch.db5" /excl /X Makro1
Why not add a "Quit" action to the macro, or add a RunCode that runs a function with "DoCmd.Quit" in it?