This technique can be used to easily save user-specific data or preferences. It is similar to using profile documents, however, you don't need to use explicit variable names when writing or reading the data.
By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.
Instead, set up a User Defined Type (UDT) with the variables, and then just read/write the UDT as a binary file. The nice thing about this is that you don't need to modify the methods that would read/write the data as you add variables to your UDT.
This works great for saving a lot of global variables and saving user-defined object states (on/off). You can save a lot of data locally with very little code. Typically, some of the code would exist in different methods but have been chunked together here to keep things simple.
- Set up a LotusScript library to hold the UDT. I created a LotusScript Library called "Utilities" with a small UDT in the declarations section. This example creates "count", "name", and "ID" variables in the UDT.
- Add the following code to the "Declarations" section of the library:
(Declarations) Public Type myUDT count As Integer name As String id As Integer End Type
- Create a form.
- In the "Globals" section of the form, select the "Options" section and add this line of code so that the LotusScript Library can be used:
Use "Utilities" or whatever you called your LotusScript library that has the UDT.
- In the "Globals" section of the form, select "Declarations" and add the following code. This declares a new UDT called "glUDT" that can be used by the form.
Dim glUDT As myUDT
- In the "Globals" section of the form, select the "Initialize" section and add the following code. This will read the UDT variables when the form is opened and set all of the variables with the data read from the file automatically! The file used in this example is "c:testUDT.txt"
Dim filenum As Integer Dim filePath As String filenum = Freefile filePath = "c:testUDT.txt" 'check to make sure file exists (it won't run the first time) If Dir(filepath) <> "" Then Open filePath For Binary As filenum Get filenum , , glUDT Close filenum End If
- Add a button to the form with the following code in the LotusScript "Click" event. This code sets the variables in the UDT every time it is clicked (you can change the name if you want to). Note that the "count" variable is increased by one every time the button is clicked so that you can see it work. The code saves the UDT (with the variables) to a binary file, and then pops up a message with the current count.
'set the UDT variables glUDT.name = "The Current User" glUDT.id = 123 glUDT.count = glUDT.count + 1 'write the variable to a binary file Dim filenum As Integer filenum = Freefile Open "c:testUDT.txt" For Binary As filenum Put filenum , , glUDT Close filenum 'display the current count Msgbox glUDT.name & " has set this [ " & glUDT.count & " ] times."
- Open the form in the Lotus Notes client and click the button a few times. The count should increase by one with every click.
- Close the form, reopen it, and click the button again. The count should keep increasing from where you left off before you closed it.
This is a quick and easy way to save data locally and has several different uses.
Do you have comments on this tip? Let us know.
This tip was submitted to the SearchDomino.com tip library by member Joe Steblay. Please let others know how useful it is via the rating scale below. Do you have a useful Lotus Notes, Domino, Workplace or WebSphere tip or code snippet to share? Submit it to our monthly tip contest and you could win a prize.