Parameter value in Access report dialog - c#

so i'm trying to automate some reporting in Access 2013. When I run a report I get a dialog asking for a parameter (Enter Plant:), something like this.
What I want is to run this code without asking the query for a plant name. The code works but if I run it, it pops a dialog asking for a Plant name, and if I type the Plant Name it runs and saves the pdf file just like I want to. The report in Access works by giving a different Plant name and it outputs a different report depending on the given Plant. My idea is to put this code on a loop and in each iteration pass a different plant name and save a different new file. But it always pop a dialog asks for a plant name to be added manually.
Microsoft.Office.Interop.Access.Application oAccess = null;
// Start a new instance of Access for Automation:
oAccess = new Microsoft.Office.Interop.Access.Application();
// Open a database in exclusive mode:
oAccess.OpenCurrentDatabase(
"route DB", //filepath
true //Exclusive
);
//This doesnt work
// oAccess.DoCmd.SetParameter("[Enter Plant:]", "Arlington");
oAccess.DoCmd.OpenReport(
"06 - Security Report - Plants",
AcView.acViewReport,
"qry Security Report - Plant",
//This doesnt work either, still asks me for a plant name
"[Enter Plant:] ='Arlington'",
AcWindowMode.acWindowNormal
);
//If I give the plant name to the dialog it works correctly en saves a pdf file wit the report
oAccess.DoCmd.OutputTo(
AcOutputObjectType.acOutputReport,
System.Reflection.Missing.Value,
"PDF Format (*.pdf)",
"route to save file",
false,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
AcExportQuality.acExportQualityPrint
);
oAccess.Quit();
I can access the query but unfortunately i cant modify it, also its pretty long thats why I will not be able to show it(looks like it was created by a the Access Wizard, so its preeety long) though here its an example where the asked parameter is being used:
AND ((Signers.Location)=[Enter Plant:])
This parameters is in the query like 40+ times.
Any ideas? Thanks in advance!

I have faced this many times at work, and the solution I used most often is to have a TextBox in a form somewhere that can be referenced directly.
AND ((Signers.Location)=[Forms]![frmMyForm]![txtMyPlantTextBox])
NOTE: You would need to have the form open and data entered into the TextBox before you run the query.

Related

C# Crystal Reports Asks Connection Parameter or Crashes Randomly

I have a C# WinForm application which opens a Report based on Crystal Reports.
It has a DataSet connection, filled by the application.
DataSetRicerca_produzione ds = new DataSetRicerca_produzione();
Ricerca_produzioneTableAdapters.Ricerca_ProduzioneTableAdapter ta = new Ricerca_produzioneTableAdapters.Ricerca_ProduzioneTableAdapter();
ta.Connection.ConnectionString = CnnStrBld.ConnectionString;
ta.Fill(ds.Ricerca_Produzione, Inizio, Fine, OrdAcc, Cicli, var1, var2, var3, var4, var5, var6, var7, var8, var9, var10, IdLav.ToString(), Idris.ToString(), IdRep.ToString(), IdOp.ToString(), IdArt.ToString(), IdMst.ToString(), IdCliente.ToString(), IdCodRagg.ToString(), IdLotto.ToString(), IdCausale.ToString(), EffMin, EffMax, Sgn, Cnf);
...
ReportDocument rd = new ReportDocument();
rd.Load(RPT PATH FILE);
rd.SetDataSource(ds);
foreach (CrystalDecisions.Shared.IConnectionInfo c in rd.DataSourceConnections)
{
c.SetLogon(pCnnStrBld.UserID, pCnnStrBld.Password);
c.SetConnection(pCnnStrBld.DataSource, pCnnStrBld.InitialCatalog, pCnnStrBld.IntegratedSecurity);
}
...
//crViewer is a CrystalDecisions.Windows.Forms.CrystalReportViewer
crViewer.ReportSource = rd;
It works properly until, it seems, the application reaches at least 2 GB of ram or the process has more than 2000 USER Objects.
The more those 2 values (Ram and USER Objects) increase, the more often this behavior can happen and, when it starts doing this, lowering the Ram occupation or the USER Objects of the application doesn't solve the issue.
The application, build in Release|x64, has no problem but, if you try to open the report in the conditions above (Ram & USER Object), it starts to have a strange behavior.
It can randomly do one of those things:
Open the report
Ask for SQL connection parameters
CrysalReports asks login
Crash without any specific error (this are the only 2 found in Event Viewer)
EventViewer Error1
EventViewer Error2
I also noticed that in %Temp% Folder it can happen:
When the report works: Are created 4 files
CRTemp Files
When the reports dowsn't work (parameter requested or crashed): Are created 3 files instead of 4 (the ~cpe is missing)
I expect the Report to work every time.
I tried installing on the PC even the CRRuntime (x64 bit) or the CR for VS with no different result.
Has someone faced this problem? How can I solve this?
Ask me more details if needed
Thanks in advance

Way to interrupt method to get additional input from user?

I'm working in ASP.net Core and have a situation where I'm importing a large list of swimming results from a previous meet. The list contains swimmer name and location so I'm doing a lookup to find that swimmerID in the database which I have previously imported. HOWEVER, I've found that many of the previous sheets have names misspelled or names that were not included in my original import.
I'm looking for a way to change the below code:
//Get SwimmerId and check for an existing entry in the db
var swimmerId = await _swimmerRepo.GetSwimmerByNameAndLocation(lookupObj.FirstName,
lookupObj.LastName,
lookupObj.LocationCode);
if (swimmerId == 0) throw new Exception($"Swimmer {lookupObj.LocationCode}: {lookupObj.FirstName} {lookupObj.LastName} not found.");
What I really want to do here instead of throwing an exception is to popup a modal or whatever to the user that allows them to choose from a swimmer that has a similar name/location (possible spelling error) or enter a new swimmer and then push that selected/new swimmerId back and continue processing the import.
I have no idea how I can do that. Any direction is appreciated.
A few notes:
Again, asp.net core with EFCore.
SQL server database
Import is currently in a controller
Import is from a csv file

How to gather information from an Input Box from within an Excel Plugin using C#

I have an Excel Add-In on the Ribbon using C# which grabs data from an API, but the password used for the API may change from time-to-time.
Without going into the myriad of security issues that may exist by gathering the PW for this purpose directly within the add-in, I'm not able to even determine how to display an Input Box, much less get user input entered into it and then pass it back to the program.
I have researched the _Application.InputBox Method but can't seem to get that to work. I always get a response such as The name 'InputBox' does not exist in the type '_Application'.
The code at this point is very simple, just:
using Excel = Microsoft.Office.Interop.Excel;
try
{
myToken = RequestToken("name#domain.com", "MyPass");
}
catch
{
Excel._Application.InputBox inputPassword = new Excel.Application.InputBox(); // this is where i get the error
myToken = RequestToken("name#domain.com", inputPassword.value);
}
Again, the purpose here is to try the "default" password. If it fails, ask the user for the updated password (the username won't change) and then we will re-try. Ideally, we'd like to mask the password input, but we can cross that bridge later.
The key here is to not have to re-deploy the entire app with a hard-coded password every time it changes on the server-side.
In a perfect world, I would probably store the updated password somewhere else (.ini file, registry, etc) instead of asking every single time when the "default" password failed. But again, this is a very early first step.
Please keep in mind this is a custom add-in to be used only by a single user, and we are still in testing, so I will probably move to a more robust security method down the road, but for now, this is the best option I have.
Any help will be greatly appreciated.

Enable built-in Microsoft Word button controls with Microsoft.Office.Interop.Word

I am currently in the need to compare two Microsoft Word documents with Microsoft.Office.Interop.Word. I've found the Application.CompareDocument method, which does exactly what I want. The following C# source code (snippet) compares a document saved in the filesystem with the current active document and opens the result in a new document:
using Word = Microsoft.Office.Interop.Word;
// [...]
Word.Document originalDocument = this.application.Documents.Open(filePath, ReadOnly: true, Visible: false);
Word.Document diffDocument = this.application.CompareDocuments(
originalDocument,
this.application.ActiveDocument);
((Word._Document)originalDocument).Close(SaveChanges: false);
// TODO Activate two built-in Microsoft Word buttons.
// [...]
But I also need to activate two built-in buttons in the view of the newly created Word document. After searching on MSDN for a while, I am unable to find a way to achieve what I want. I've added two screenshots to this question, which display the built-in buttons I want to activate (sadly, I am using the German version of Microsoft Word 2010, so I don't know what the exact translations are).
"Quelldokumente anzeigen" (could be translated as "Display source documents"). I need to activate the button "Beide anzeigen" (could be translated as "Display both").
"Überarbeitungsbereich" (could be translated as "Revision pane"). I need to activate the button "Überarbeitungsbereich vertikal..." (could be translated as "Vertical revision pane...").
To conclude, I want to know how I can modifiy the state (either directly or indirectly via an method call) of these two buttons.
EDIT (2013-08-03)
The revisions pane can be set via the following method:
diffDocument.ActiveWindow.View.SplitSpecial = Word.WdSpecialPane.wdPaneRevisionsVert;
I am still searching for a solution to display both the source document and revised document panes.
EDIT (2013-08-05)
The show source documents button can be modified to show both source documents via the following method:
diffDocument.ActiveWindow.ShowSourceDocuments = Word.WdShowSourceDocuments.wdShowSourceDocumentsBoth;
Possible solutions of your problems:
Ad 1. but first, you need to open your documents with ReadOnly: false while,according to both C# and VBA test it will not work when you set parameters to ReadOnly: true:
((Word._Document)diffDocument).Windows.CompareSideBySideWith(originalDocument);
Ad 2. this time you need to refer to window object of Word Application. Here is code for active window:
appWRD.ActiveWindow.View.SplitSpecial = Word.WdSpecialPane.wdPaneRevisionsVert;
where: appWRD is Word.Application in my code.
Ad 1 again. (one above was a result of misunderstanding).
According to some test this code should give you what you need:
appWRD.ActiveWindow.ShowSourceDocuments = Word.WdShowSourceDocuments.wdShowSourceDocumentsBoth;

Microsoft.Office.Interop.Word.Document won't prompt to save changes to single document without _Application.Quit()

Before I begin, I have googled this to death and there are many posts about how to prevent the save prompt. I am having an issue showing the save prompt.
I am building the template editing portion of a document generation system in C#. The system will edit 'dot' and 'dotx' files. Before outlining the problem, the environment I am using for development is running Visual Studio 2010 and Word 2010. It will eventually run on other versions, but i would like to get these versions functional first.
To set the scene I have a form open that has a list of columns returned from a stored procedure(Data Source) to add to the document as bookmarks. I have all of the bookmark and drag/drop operations functional. When I close the application, I catch the 'ApplicationEvents4_DocumentBeforeCloseEventHandler' event to close the form.
When I close the form, i check haw many documents there are open. If only one document is open, I close the application which prompts the user to save changes. If however there are multiple documents open (Most people have a number of different word documents open concurrently), I locate the correct document and close it with the flag set to prompt the user to save changes.
This is where the problem occurs. At this point, the save changes dialog never shows up and everything freezes up in Visual Studio. If I stop the debugging on Visual Studio 2010 the document flashes in the task bar indefinitely, and if you focus on it, it disappears and saves changes without a prompt.
This is the code to handle the form closing event:
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (app != null)
{
if (app.Documents.Count < 2)
{
this.TopMost = false;
((Word._Application)app).Quit();
app = null;
}
else
{
foreach (Word.Document document in app.Documents)
{
if (document.FullName.Equals(wordDoc.FullName))
{
object saveChanges = Word.WdSaveOptions.wdPromptToSaveChanges;
((Word._Document)wordDoc).Close(ref saveChanges);
break;
}
}
}
}
}
The problem is this line should show a save changes dialog:
((Word._Document)wordDoc).Close(ref saveChanges);
I have tried debugging this without much luck. Putting a breakpoint on this line and on the
break;
line allows the program to stop at the 'Close' line but when you 'step' forward or 'continue' word becomes unresponsive, so does the form and the breakpoint on the very next line never gets hit.
Any help would be greatly appreciated as something this simple is so annoying to get stuck on.
To avoid the Prompt or to get the Prompt to you have to set the Saved property to true or false respectively:
var doco = WordApp.Documents.Add();
doco.Saved = true;
doco.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges, Type.Missing, Type.Missing);
Something fishy is going on if Word hangs on the line of code when you try to close the document. I recommend disposing of all the resources properly. Here is a great article on using VSTO Contrib that helps provide this functionality:
http://jake.ginnivan.net/vsto-com-interop
Update:
Enable your VSTO log file by adding the following on your system environment variables:
NAME: VSTO_LOGALERTS
VALUE: 1
There might be an exception error that is why your add-in is not loading.
You can check this source for more info on VSTO logging and alerts, but in essence you change two environment variable values depending on what you need to do:
Displaying VSTO Alert Prompts
To display each error in a message box, set the
VSTO_SUPPRESSDISPLAYALERTS variable to 0 (zero). You can suppress the
messages by setting the variable to 1 (one).
Logging VSTO Alerts to a Log file
To write the errors to a log file, set the VSTO_LOGALERTS variable to
1 (one).
Visual Studio Tools for Office creates the log file in the folder that contains the application manifest. The default name is .manifest.log. To stop logging errors, set the variable to 0 (zero).

Categories

Resources