How do I edit an Access report on the fly? - sql

This seems like it would be pretty simple, but I’ve been googling my brains out for the last two weeks and I can’t find the answer. I'm using Access 2010...
I have a form with a button that the user clicks to get a report, that button pops-up a form that ask for the date of the requested report and then the report open with the correct info. The problem is I need to manipulate the data before it is displayed. Some of it I’ve done directly through the SQL statement and some through the Control Source of the text boxes on the report. But some of the data is a little more complicated… I need to extract certain text from a “remarks” field (memo) and I need to concatenate a few fields together separated by a comma, some of which may be blank. I know I can use the “+” to do that as in “lastName & (“,” + firstName) which will eliminate the unwanted trailing comma… but what if “lastName” is blank… I will be left with an unwanted preceding comma???
How do I loop through the data as it is being “written” to the report so as to edit it “on-the-fly”? I don’t want the user to edit the data because I want to get the exact same results every time.

This an answer to the first question (but what if “lastName” is blank):
lastName & IIf(Len(Lastname) > 0, ",", "") & firstName

Related

MS Access: Query to list number of files in a series of folders

I have folders labeled by their keyfield, so 1, 2, ... 999, 1000. located in currentproject.path\RecordFiles\KeyFieldHere so like currentproject.path\RecordFiles\917.
I want to run a query that will count how many files are in each folder. I know this can be done with the DIR function through visual basic, but I can't seem to run it through a SQL query.
I've tried using this function in a SQL equation, so Expr1: [FlrFileCount("Y:\Education\Databases\RecordFiles\")] as one of the fields just to see if it can work, but it prompts me for a value and then returns nothing.
EDIT: I tried an approach using the FlrFileCount function in a continuous form, and it does work, BUT... I get an error after every single line. I have a field in a continuous form of =FlrFileCount([currentproject].[path] & "\recordfiles\" & [ID]), but when I run the form I get an error "Error 76, Error source: FlrFileCount, Error description: Path not found." Which is crazy because IT WORKS, it properly lists the number of files in the folder for each record.
I just need to get this functionality over into a SQL query so I can pull that data for mail merges.
I currently have something similar in a form. The form has an onload property to run a module (Link here) to create a list of all the files in the relevant folder to that record, and then I have another field that just counts the number of entries in the list. However a list can't be a value in a SQL query, so I don't think that code will help.
Thanks to Tim Williams, the answer was to put
=FlrFileCount(Currentproject.Path & "\recordfiles\" & [ID])
It seems the [currentproject].[path] part was where the error was. What's confusing is that in other places, MS Access adds the extra [] around currentproject and path, and I don't know why.
Thank you so much for your help! Now to the tricky part: Implementing a proper naming scheme by program ID across a sharepoint so that the relevant folder can be opened consistently even when program names change.

Add multiple blank lines to a Report from TextBox?

I have an Access Database linked to a Crystal Report (2011). The Report has many Sections, one Group, as well as several Sub Reports.
Each Sub Report is in its own Section on the main Report. Each Section/Sub Report is set to "Keep together on one page."
What my boss is looking for is a way to add space "on-demand" to the end of any section. In other words, he wants the report to be a word document that he can freely add lines to.
I know this is not exactly possible. I do realize that you can export a Report as a .doc File, but that takes extra steps and we are trying to automate this as much as possible.
What I am considering is a TextBox (TextBox1) in Access that allows you to type in a number and adding a that number of blank lines to a different TextBox (TextBox2) (E.g. if you type 3 in TextBox1 then the value for TextBox2 would be something like "". Then referencing TextBox2 in a formula in the Report using HTML interpretation.
Is there an easy way to do this?
Does anyone have any better suggestions?
If you haven't already, I'd try giving him/her a version of the report with "extra" blank space already included. Another option might be doing page breaks after the sub-reports. Make sure it looks good to you and then present it.
But, to do the variable blank spaces, I'd use a Crystal parameter (or multiple Crystal parameters). This way, everything is in one place. If the boss decides there should be one more line, he/she can just hit refresh and change the parameter rather than going back to the Access form. Once you have the parameter, add a formula to the form when you want the variable spaces, and set it to "can grow". The actual formula would be something like:
Local NumberVar i;
Local StringVar out := "";
for i := 1 to {?BlankLines} do
out := out + chr(13) + chr(10);
out;

Google Apps Script on Form Submit Time Formatting Glitch/Fix

Background:
How: I suspect that this is a glitch within Google Form (submission process)/Spreadsheet, but may be part of the Date conversion utility of the Spreadsheet interface (and is an intended feature).
When entering a format in a text box in Google Forms, there is some sort of communication error between the Form submit and Response Spreadsheet, or pre-processing of the Form's data before it is sent to the spreadsheet. The glitch only seems to happen for data in a text field of the format ##:## TEXT where TEXT contains no '.' characters. For example: 4:15 pm will reproduce the glitch, but 4:15 p.m and 4:15 p.m. will not.
Result: An apostrophe character is added to the beginning of the string when it is put into the Spreadsheet (i.e. '4:15 pm) which throws off several sub-systems I have in place that use that time data. Here are two screenshots (sorry for the bad sizing on the second):
I'm 99% certain that the glitch is caused by the ##: combination.
Temporary Fix?: The real question is... how might I go about removing that pesky apostrophe before I start manipulating the time data? I know how to getValue() of a cell/Range. Assume I have the value of a cell in the following manner:
var value = myRange.getValue();
// value = '4:15 pm
How can I go about processing that value into 4:15 pm? A simple java function could be
value = value.substring(1); // Assuming "value" is a String
But in Google App Scripts for Spreadsheets, I don't know how I would do that.
Post-Script: It is necessary to post-process this data so that I don't have to lecture university faculty in the language department about inputting time format correctly in their forms.
Thanks in advance to those who can help!
How can I go about processing that value into 4:15 pm? A simple java
function could be
value = value.substring(1); // Assuming "value" is a String But in
Google App Scripts for Spreadsheets, I don't know how I would do that.
Google Apps Scripts uses Javascript which has the exact same method.
value = value.substring(1);
should return all except the first character.
More about Javascript substring at: http://www.w3schools.com/jsref/jsref_substring.asp
If you remove the ' in the spreadsheet cell the spreadsheet interface will convert this entry to a date object.
This might (or not) be an issue for you so maybe you should handle this when you read back your data for another use...
It doesn't happen when text is different (for example with P.M) simply because in this case the ' is not necessary for the spreadsheet to keep it as a string since the spreadsheet can't convert it to a date object (time value).
Artificial intelligence has its bad sides ;-)
edit :
You cant do this in an onFormSubmit triggered function using the javascript substring() you mentioned. If you're not familiar with that, here is the way to go :
To run a script when a particular action is performed:
Open or a create a new Spreadsheet.
Click the Unsaved Spreadsheet dialog box and change the name.
Choose Tools > Script Editor and write the function you want to run.
Choose Resources > Current project's triggers. You see a panel with
the message No triggers set up. Click here to add one now.
Click the link.
Under Run, select the function you want executed by the trigger.
Under Events, select From Spreadsheet.
From the next drop-down list, select On open, On edit, or On form
submit.
Click Save.
see doc here and here

how to pass parameters to query in SQL (Excel)

I "linked" Excel to Sql and it worked fine - I wrote some SQL script and it worked great. All I want to do is to pass parameter to query. Like every time I make refresh I want to be able to pass parameter (filter condition) to Sql Query.
In "Connection Properties" Parameters button is disabled. So I can't make parameter query.
Can Anyone help me?
This post is old enough that this answer will probably be little use to the OP, but I spent forever trying to answer this same question, so I thought I would update it with my findings.
This answer assumes that you already have a working SQL query in place in your Excel document. There are plenty of tutorials to show you how to accomplish this on the web, and plenty that explain how to add a parameterized query to one, except that none seem to work for an existing, OLE DB query.
So, if you, like me, got handed a legacy Excel document with a working query, but the user wants to be able to filter the results based on one of the database fields, and if you, like me, are neither an Excel nor a SQL guru, this might be able to help you out.
Most web responses to this question seem to say that you should add a “?” in your query to get Excel to prompt you for a custom parameter, or place the prompt or the cell reference in [brackets] where the parameter should be. This may work for an ODBC query, but it does not seem to work for an OLE DB, returning “No value given for one or more required parameters” in the former instance, and “Invalid column name ‘xxxx’” or “Unknown object ‘xxxx’” in the latter two. Similarly, using the mythical “Parameters…” or “Edit Query…” buttons is also not an option as they seem to be permanently greyed out in this instance. (For reference, I am using Excel 2010, but with an Excel 97-2003 Workbook (*.xls))
What we can do, however, is add a parameter cell and a button with a simple routine to programmatically update our query text.
First, add a row above your external data table (or wherever) where you can put a parameter prompt next to an empty cell and a button (Developer->Insert->Button (Form Control) – You may need to enable the Developer tab, but you can find out how to do that elsewhere), like so:
Next, select a cell in the External Data (blue) area, then open Data->Refresh All (dropdown)->Connection Properties… to look at your query. The code in the next section assumes that you already have a parameter in your query (Connection Properties->Definition->Command Text) in the form “WHERE (DB_TABLE_NAME.Field_Name = ‘Default Query Parameter')” (including the parentheses). Clearly “DB_TABLE_NAME.Field_Name” and “Default Query Parameter” will need to be different in your code, based on the database table name, database value field (column) name, and some default value to search for when the document is opened (if you have auto-refresh set). Make note of the “DB_TABLE_NAME.Field_Name” value as you will need it in the next section, along with the “Connection name” of your query, which can be found at the top of the dialog.
Close the Connection Properties, and hit Alt+F11 to open the VBA editor. If you are not on it already, right click on the name of the sheet containing your button in the “Project” window, and select “View Code”. Paste the following code into the code window (copying is recommended, as the single/double quotes are dicey and necessary).
Sub RefreshQuery()
Dim queryPreText As String
Dim queryPostText As String
Dim valueToFilter As String
Dim paramPosition As Integer
valueToFilter = "DB_TABLE_NAME.Field_Name ="
With ActiveWorkbook.Connections("Connection name").OLEDBConnection
queryPreText = .CommandText
paramPosition = InStr(queryPreText, valueToFilter) + Len(valueToFilter) - 1
queryPreText = Left(queryPreText, paramPosition)
queryPostText = .CommandText
queryPostText = Right(queryPostText, Len(queryPostText) - paramPosition)
queryPostText = Right(queryPostText, Len(queryPostText) - InStr(queryPostText, ")") + 1)
.CommandText = queryPreText & " '" & Range("Cell reference").Value & "'" & queryPostText
End With
ActiveWorkbook.Connections("Connection name").Refresh
End Sub
Replace “DB_TABLE_NAME.Field_Name” and "Connection name" (in two locations) with your values (the double quotes and the space and equals sign need to be included).
Replace "Cell reference" with the cell where your parameter will go (the empty cell from the beginning) - mine was the second cell in the first row, so I put “B1” (again, the double quotes are necessary).
Save and close the VBA editor.
Enter your parameter in the appropriate cell.
Right click your button to assign the RefreshQuery sub as the macro, then click your button. The query should update and display the right data!
Notes:
Using the entire filter parameter name ("DB_TABLE_NAME.Field_Name =") is only necessary if you have joins or other occurrences of equals signs in your query, otherwise just an equals sign would be sufficient, and the Len() calculation would be superfluous.
If your parameter is contained in a field that is also being used to join tables, you will need to change the "paramPosition = InStr(queryPreText, valueToFilter) + Len(valueToFilter) - 1" line in the code to "paramPosition = InStr(Right(.CommandText, Len(.CommandText) - InStrRev(.CommandText, "WHERE")), valueToFilter) + Len(valueToFilter) - 1 + InStr(.CommandText, "WHERE")" so that it only looks for the valueToFilter after the "WHERE".
This answer was created with the aid of datapig’s “BaconBits” where I found the base code for the query update.
It depends on the database to which you're trying to connect, the method by which you created the connection, and the version of Excel that you're using. (Also, most probably, the version of the relevant ODBC driver on your computer.)
The following examples are using SQL Server 2008 and Excel 2007, both on my local machine.
When I used the Data Connection Wizard (on the Data tab of the ribbon, in the Get External Data section, under From Other Sources), I saw the same thing that you did: the Parameters button was disabled, and adding a parameter to the query, something like select field from table where field2 = ?, caused Excel to complain that the value for the parameter had not been specified, and the changes were not saved.
When I used Microsoft Query (same place as the Data Connection Wizard), I was able to create parameters, specify a display name for them, and enter values each time the query was run. Bringing up the Connection Properties for that connection, the Parameters... button is enabled, and the parameters can be modified and used as I think you want.
I was also able to do this with an Access database. It seems reasonable that Microsoft Query could be used to create parameterized queries hitting other types of databases, but I can't easily test that right now.

Formatting text from Mulitline text box in word with VBA

I'm putting together a template in Word, using a form for the user to fill in to then populate some of the document.
The bit I'm currently stuck on is at the end of the document, where the cc's are listed.
The form has a multiline text box into which the user puts in their cc's, one per line.
I then want to add to the end of the document the contents of the text box, but in the right format. Specifically, it should look like:
cc: First CC contact
Second CC contact
so on and so forth
I attempted to do this using 2 bookmarks, so my code currently is:
' If 'CC' box has content, add it
If doc_CC.TextLength > 0 Then
.Bookmarks("CC").Range.Text = vbCr + "cc:"
.Bookmarks("CCs").Range.Paragraphs.Indent
.Bookmarks("CCs").Range.Text = doc_CC + vbCr
End If
However, when this is run, on the page it looks like:
cc: first contact
second contact
and so on
Realise that the 2 bookmark method is a bit messy but it seemed like a good idea at the time - obviously this is not the case! Have done some searching for a way to do it with Split but am not making much progress down this path - suspect I'm googling for the wrong thing.
How do I do this so that the formatting is as desired? Any help is greatly appreciated.
Try inserting a tab character? + Chr(9) or even + vbTab may work.
Have found a work around which, while doesn't answer the actual question of how to do it, does produce a result to the same effect.
Have used a 2 column table without no lines instead with contents of a1 being "cc:" and contents of a2 being whatever was entered into the multiline text box. If there is nothing in the text box, then the table is deleted.
I'll keep on eye on this question though so if some one does have the proper answer I can mark it accordingly.
Another possibility would be to format the cc paragraph with a hanging indent (like is used for bullets or numbering). Use a newline character - Chr(11) - instead of vbcr to separate each entry. The text should all line up,then...