Introduction
Today I will explain how to do an Options dialog box with parameters for your application in MS Access.An options window is a simple dialog form.
It may be linked to a table, but I prefer to do it using VBA, to have a more professional look and a very good maintenance.
This post is organized in six steps: the table, the form, the records, the code in a module, finishing the form, and the code for the form.
At the end of this post you may download a small Access file with the form options, the table and the code to manage it.
First step: The table
For the table, I usually create a table with three fields. The name of the table is Options:- parName (text, 50, Primary key): the name of the parameter
- parValue (text, 50): the value of the parameter
- Details (text, 255): a brief explanation text field to have information about the field, like a comment.
Later in this example we will see that there are different types of values, but the field is always a text field. Our code will translate our data.
Second step: The form
Once I've created the table, we may create the form.
It's a simple form where we will put the some controls for the parameters.
The form must be created as a dialog box, that is, in the properties box you have to configure:
- Record selector: No
- Navigation buttons: No
- Scroll bars: Neither
And in the Data section, at the Row source, nothing.
After that, we have to put two command buttons for "Save and close", and for "Close" without saving. I usually put them in the right bottom side of the footer. Let's name them cmdSaveClose and cmdClose. We will see later the code for those command buttons.
Third step: the records
Ok, the table and the form are made, the next step is to fill some data in the table.
Let's fill those simple records, each field separated by semicolon:
Name;Value;Details
AppName;MyApplication;The name of my application
AppVersion;1.0;The version of my application
AppBoolean;1;Example of a boolean expression (0: false, 1: true)
AppNumber;12.3;Example of a number expression
Fourth step: the code in a module
It may seem little bit strange to put the code in a module, but you will see it's better.
In this step I will use a function and a sub to manage the data.
As you will see, I always declare all may variables, this is due to I use to put the command
Option Explicit
At the top of all my modules. Of course you may not use that, but like this we may be sure of all variables are well declared.
So, we need this code:
This function reads the value of a parameter given by ParName in the Options table.
Public Function getParam(ParName As String) As String
Dim sqlAction As String
Dim record As Recordset
Dim retValue As String
sqlAction = "SELECT parValue FROM Options WHERE parName = '" & ParName & "'"
Set record = CurrentDb.OpenRecordset(sqlAction)
retValue = IIf(Not IsNull(record.Fields(0)), record.Fields(0), "")
getParam = retValue
End Function
And this one writes the value of the parameter in the table:
Public Sub setParam(ParName As String, ParValue As String)
Dim sqlAction As String
sqlAction = "UPDATE Options " & _
"SET parValue = " & ParValue & " " & _
"WHERE parName = '" & ParName & "'"
CurrentDb.Execute sqlAction
End Sub
Those two functions, in some cases, are the most used in your app when it runs.
There are two notes about them:
- They are in a Module, because they are not only made for the Options form.
- They are declared as Public because they will be used by all the application.
Fifth step: finishing the form
Well, once we have created the table, and we have entered some data, and we have entered the basic code to manage this data on the table, we will have to finish our form.
Earlier in this post I've been talking about four parameters. Now, we will have to "draw" three text fields in our form and a checkbox.
The name for those controls is your choice, but I usually like to name them like the parameters on the table. Or almost.
Following the example, we may put:
- A text field named txtAppName, for the name of the application
- A text field named txtAppVersion
- A check box named chkAppBoolean
- A text field named txtNumber
Sixth step: code for the form
Well, this is the final step.
In this section we will put the code for the buttons, and the code that loads and saves de values of the parameters for the application.
First, a procedure to close the form:
Private Sub CloseForm()
DoCmd.Close acForm, Me.Name, acSaveYes
End Sub
First, a procedure to close the form:
Private Sub CloseForm()
DoCmd.Close acForm, Me.Name, acSaveYes
End Sub
After, a procedure to save the data:
Private Sub SaveData()
setParam "AppName", "'" & Trim(Me.txtAppName) & "'"
setParam "AppVersion", "'" & Me.txtAppVersion & "'"
setParam "AppBoolean", IIf(me.chkAppBoolean,"1","0")
setParam "AppNumber", "'" & Trim(Str(Me.txtAppNumber)) & "'"
End Sub
The following procedures must be written by clicking on each button, and selecting Event procedure on the OnClick event in the Events tab of the Properties box.
Now, an event procedure to close the form when the command button is pressed:
Private Sub cmdClose_Click()
CloseForm
End Sub
Next, and event procedure to save the data. To write it, just click on the button (in design mode), and in the Properties, click on Events, and after On Click, select Event Procedure.
Private Sub cmdSaveClose_Click()
SaveData
CloseForm
End Sub
This procedure must be written by selecting the OnOpen event at the properties of the form.
An event procedure to load the form:
Private Sub Form_Open()
Me.txtAppName = Trim(getParam("AppName"))
Me.txtAppVersion = Trim(getParam("AppVersion"))
Me.txtAppBoolean = (getParam("AppBoolean")=1)
Me.txtAppNumber = Val(getParam("AppNumber"))
End Sub
Is it simple?
I hope you like it.
In fact, it's true, perhaps I posted it too fast and I forgot to correct some mistakes.
Those errors are now corrected on the code posted avobe.
I have to aknowdlege to +Brian Welch , I've learned a good lesson.
Me.txtAppName = Trim(getParam("AppName"))
Me.txtAppVersion = Trim(getParam("AppVersion"))
Me.txtAppBoolean = (getParam("AppBoolean")=1)
Me.txtAppNumber = Val(getParam("AppNumber"))
End Sub
Is it simple?
I hope you like it.
Ending note
I'm updating this post on feb. 15, after reading that a user has some problems with the example.In fact, it's true, perhaps I posted it too fast and I forgot to correct some mistakes.
Those errors are now corrected on the code posted avobe.
I have to aknowdlege to +Brian Welch , I've learned a good lesson.
Comentaris
Publica un comentari a l'entrada