What is the difference between fields and formfields? - vba

in word pressing alt+F9 can display "FieldCodes"
How can I access this programmatically using vba and pair them with the formfields?
You can access the field code by ? Application.ActiveDocument.Fields(1).Code
And you can access the form fields by Application.ActiveDocument.FormFields(1)
But is there any guarantee's about the indices matching?
Can a formfield ever not be a Field? can a Field ever not be a formfield?
Will changing these away from FORMTEXT have any unintended side effects, or are these basically nice and friendly linking id's / display values allowing you to view them and swap between them with ease?
Edit: I've come up with the following to get the fields Code. I'm still unsure if it's a good idea to edit them or not, or what they represent.
Application.ActiveDocument.FormFields(1).Range.Fields(1).Code

Fields are general objects, they can be :
document's properties (built-in or custom),
mailing / mergemail,
calculation,
form fields,
...
A form fields is an field for inputs.
Take a look at the links in the tag info of word-field, there is a lot of interesting things!
And you can access them by their own collections (press F2 in VBE to use Object Browser).
For Fields the general collection is in Application.ActiveDocument.Fields,
but you can find them in a lot of objects (check with Object Browser!).
Take a look at that answer to have an idea of the other objects in which you can find it! ;)
To my knowledge, there is no possibility nest something inside a FormField, like you can do in a Field.
I'm not sure to understand that question :
"Will changing these away from FORMTEXT have any unintended side effects,
or are these basically nice and friendly linking id's / display values
allowing you to view them and swap between them with ease?"
If you want to create a Form that users can fill, you'll need to stick with FormFields.
If you want to display values at specific places in a document, Fields is the way to go.
I'd even suggest Custom Document Properties for a general use, and MergeMail for Mailings. ;)

Related

Can Access ensure a new form record displays all fields?

I have a database where I don't want some fields showing depending on data in other fields. I'm still new to VBA, having learnt how to do things I need via the internet (there's not much call for it in my job so like to try it out on side projects) for the things I need and have managed to create some code that hides certain fields that aren't needed, depending on what's been entered in another field and that works okay, if not perfectly (I'd like it to only work on the current record and not all of them at once but will worry about that later). My problem is, if I'm entering information onto a record and any of those fields become invisible exactly as I would want them to, then if I have more records to complete and load a new record, those hidden fields are also hidden on the blank record before any data has been entered and I want each new record to show all fields from the outset.
Another thing I've noticed is that if I close the database, next time I go into it the hidden fields have unhidden themselves again so I know I'm missing something important.
Here's a screenshot of a bit of the code where I want 2 other fields (What_reason and Date_sent_to_new_owning_School) to be visible depending on whether the answer in the current field after update is "Standard" or "Non-standard":
I'm sorry if this is really entry-level stuff but I AM entry level and trying to learn. This bit does work, albeit not perfectly as I'd like it to only work on the record I'm in at the time, and not go through and hide that field in all the other records at once (which it's doing).
I've searched everywhere but can't find the answer and although I've tried, I'm nowhere near good enough at VBA to try and use common sense to work it out. Is this something that can be done? I'm okay with computers generally and with Access too but I'm aware there's an awful lot I don't know and this is why I'm trying to do new things and learn stuff that I've not used before. I have tried all day to get this to work but am admitting defeat and am hoping somebody here will be able to help me. I'll probably need 'idiot level' advice if that's possible, I know my limitations. :)
Do you know how to use the Event tab in the Property Sheet? You can set all of your fields to [field].Visible = True on either: On Current, On Load, or On Open
Screenshot of the Property Sheet and for the field that determines the visibility of all of the other fields; you can use the Event: After Update so that way when you click/tab away from that field, it'll make those changes for you!
Property setting affects ALL instances of control. Control will be visible/not visible for all records depending on conditions of current record. Therefore, dynamically hiding controls on form set in Continuous or Datasheet will NOT give the desired result of
only work on the current record and not all of them at once
Db is not going to 'remember' dynamic setting - code needs to be executed when form opens and/or navigating records - so it is needed in OnCurrent event as well as control's AfterUpdate.
Conditional Formatting can dynamically enable/disable textbox/combobox by record although control is still visible.

why does selecting "refresh fields on keyword change" load a whole new document?

Notes 9.01
Why does a new document on the web change unique IDs every time you refresh it?!? this causes all kinds of issues. Obviously it is a different document, so maybe I should re-phrase that, but I think you know what I am saying.
I have a listbox field, with the setting to "refresh fields on keyword change" selected. This allows hide-whens to recalc, and other fields to recalc. I also have a computed text showing the current #DocumentUniqueID.
choices are: "Select one":"one":"two":"three"
When using this form on the web, in a new document, and I pick something in this field from the drop-down, it refreshes the form, and the choice I just picked is removed and "Select one" is what is showing again.
Once the document is saved, this stabilizes and you do not switch documents, and field values do not get cleared. I just want to understand the logic of this and find out how other people work around this.
Any feedback would be great. If I am doing something stupid, please tell me, I can take it.
Matt
I don't know what the "all kinds of issues" that you're dealing with are, but for as far back as I can remember, Notes documents have not had a stable #DocumentUniqueID value (or any at all, actually) prior to being saved for the first time. It's been my practice, and I think pretty widely accepted practice, not to write code that would have issues with that. If it's been necessary to depend on some unique value in the document prior to the first save, I've always used a computed-when-composed field with #Unique for its value.
My suggestion would be to not use "refresh fields on keyword change" for a form used on the web. The way I would handle it is to use some JavaScript to handle that.
Personally I would do this, in your situation:
Add jQuery to the form, you can easily put a CDN link in the page
header.
Write a JavaScript function called (for example)brecalculateFields(). This function would calculate field valuesband perform hide/show of fields/sections of the form.
Set a class for all fields where you want to trigger a recalc of the fields when the value is changed. I would call the class recalcForm.
Bind the function recalculateFields() to the changed event of all fields with the class recalcForm.
You may want to bind the function to a few other events as well, depending on what type of fields you have on the form.
$(".recalcForm").on("change", function(e) {
recalculateFields();
});
$(".recalcForm").on("blur", function(e) {
recalculateFields();
});
I have blogged about this in the past, hopefully you can use some of the info there:
http://blog.texasswede.com/using-jquery-to-emulate-notes-hide-when/
http://blog.texasswede.com/jquery-a-flexible-way-to-showhide-sections/

In Access VBA on a Form I want to use two listboxes to sort and reorder a set of field names

I'm trying to create a filter so the user can choose any of 40+ fields to display in a query result that has been dynamically created via VBA.
This is something I see standard in a lot of programs so I figure someone must have code that does it without me reinventing the wheel. Unfortunately I haven't been able to find it online.
Basically when it starts all the field names are listed in the left box and the right box is empty. The user can click a right arrow button to move the fields into the right box or a left arrow to remove them and put them back in the left box. Also the user can use up and down arrow buttons to change the order of a selected field in the right listbox.
Once I have the right listbox populated and ordered correctly I know how to execute the query properly.
If you are unable to find a full example for this then you'll need to work through it step-by-step yourself. Here is an MSDN link for the ListBox control. There are links to the various methods and properties of this control at the bottom of the page.
Essentially, clicking a button (your arrow(s)) triggers the Click event;
In this event you might need to loop through to find the Selected item;
If the listbox is multi-select then you can examine the ItemsSelected collection;
Then you would use the AddItem and RemoveItem methods to move them for right-to-left or left-to-right.
I cannot recall currently whether the Field List option is bound to the table, or whether it just populates the list once when the form is loaded. If it is bound then you would instead need to use the Value List option and use VBA to retrieve all the field-names, and use these to set the RowSource (you may be doing this already).
In either event (Field or Value List) I don't believe the lists can remain sorted automatically (unlike in VB.NET). You will probably need to split the RowSource up into a (dynamic) array, find a simple sort routine to sort the array, and reassign the sorted items as the RowSource.
An alternative would be to create two temporary tables and use ADO (or DAO) to insert and delete rows between them, then use SELECT statements each time to re-populate the lists. This has the advantage that the SELECT can include an ORDER BY clause to sort the lists each time. However, It perhaps involves a little bit more effort (to maintain, and delete, the temporary tables).
You might, however, want to continue your search as I suspect an example is out there somewhere. Good luck.

Pass hidden data through XUL autocomplete textbox?

One of the controls needed in my xulrunner application is an autocompleting text box which allows the user to type a search term, then looks up completions in an array of objects (each having a generated UUID, canonical name, a list of search terms gleaned from related data, etc.) and allows the user to select just one. Currently I'm using a textbox element of type="autocomplete" and a Javascript custom search component, and it is successfully prefix searching all the search terms and providing completions below the text field, in the customary fashion.
The catch is that I'm not interested in the possibly non-unique label but the object from which the label came, and I can't see any way of passing the object or even any out-of-band UUID back into document land without modifying the XBL or rolling my own control from scratch. Essentially I'm seeking to do what could have been done in HTML with the option[value] attribute. I can't use the built-in type-to-search effect of a standalone menulist because I need to prefix search multiple fields of the object. Any recommendations? Thanks in advance.
I ended up rolling my own as a listbox inside a panel next to a textbox. Even composed into an XBL binding, this was less effort than I'd spent working with the built-in autocomplete textbox and trying to force it to handle what it wasn't intended to handle.

How do I check programmatically if any document properties of a MS Word 2007 document has changed?

For example, I want the Title fields in the body and the page headers of the document to be updated automatically whenever the Title field in the document properties panel is changed. I know how to update the fields, but I want to know the name of the event that will tell me when the document properties have changed.
Your help will be appreciated. Thanks.
I also asked it on the MSDN Forums.
You can certainly check for these kinds of things in some of the events, such as DocumentBeforeClose or WindowSelectionChange, but this may be overkill. Instead, you could just use fields - they will update automatically. For example, go to Insert and then click on Quick Parts and then Field... Go to the Document Information section in the dropdown on the left and choose Title. Then, insert that and go back to the Home tab on the Ribbon and set its style to Title.
You also mention you also want page headers - are those properties you're setting in the Document Panel?
Word doesn't have any events like that. The best you might be able to do is use the selection change event, which will happen fairly often, but then you have to check all the propertiers of all the documents.
I think you're trying to do something that Word doesn't normally do. There are certain times when fields are updated and that's it. Teach you users how it works.