Hi Karl,
I forgot to mention that you will need to set a reference to the "Microsoft
DAO 3.6 Object Library" in order to get the code in Form_Close to compile. If
you are using Access 97, then the correct version is the Microsoft DAO 3.51
Object Library.
Tom
http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________
:
Hi Karl,
Small world. Poulsbo is a very beautiful place! I've been there a few times.
I get a bug that says "tables" not a defined variable.
You cannot refer to the value in a table like that. The form you used is
similar to how one refers to the combo or list box in an open form, but it
doesn't work for tables.
For this example, I created the following table with two fields:
tblUserPreferences
pkDefaultDesc Text (set as Primary Key)
DefaultValue Text
I chose Text for the DefaultValue, so that one could store text values
easily. You can store numeric values that are saved as text as well. In table
preview, I entered the following:
pkDefaultDesc DefaultValue
PrintAlignment 1
I added an unbound frame control with two option buttons to a form. I named
the frame "fraPrintOptions" (without the quotes). The option button with
label "Center" has an option value of 1, and the option button with label
"Left" has an option value of 2.
I added the following two procedures to the code module associated with this
form. I used the Form_Open event procedure instead of Form_Current as you
indicated:
Option Compare Database
Option Explicit
Private Sub Form_Open(Cancel As Integer)
On Error GoTo ProcError
' Read the last value chosen by the user
Me.fraPrintOptions.Value = CInt(Nz(DLookup("DefaultValue", _
"tblUserPreferences", "pkDefaultDesc = 'PrintAlignment'"), 1))
ExitProc:
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in Form_Open event procedure..."
Resume ExitProc
End Sub
Private Sub Form_Close()
On Error GoTo ProcError
' Save the last value chosen by the user
Dim db As DAO.Database
Dim strSQL As String
Set db = CurrentDb()
strSQL = "UPDATE tblUserPreferences SET DefaultValue = '" _
& CStr(Me.fraPrintOptions) & "' " _
& "WHERE pkDefaultDesc = 'PrintAlignment';"
db.Execute strSQL, dbFailOnError
ExitProc:
On Error Resume Next 'Cleanup
db.Close: Set db = Nothing
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in procedure Form_Close..."
Resume ExitProc
End Sub
Notes:
1.) If you are missing Option Explicit from new code modules, I recommend
that you make a change to your VBA editor settings so that you will get this
most important line in all new modules. Here is a gem tip that discusses this
more fully:
Always Use Option Explicit
http://www.access.qbuilt.com/html/gem_tips.html#VBEOptions
2.) The tblUserPreferences table can be used to add any number of settings
that you wish to save for the user. Just use unique pkDefaultDesc values. For
example, you could use "NameOfFormPrintAlignment" instead of just
"PrintAlignment" if you want to store default values for a number of
different forms, where NameOfForm is equal to the name of the form. You'd
need to make a similar adjustment in the DLookup function to refer to the new
value.
3.) The method I used to retrieve the value uses a domain aggregrate
function named DLookup. This is a fairly easy method to implement, but it may
not be the most efficient, especially if your database is a split multi-user
database. An alternate method of reading the value from the table can be
written using either DAO or ADO code.
4.) The Nz function is used to convert a null to a pre-defined value (1 in
my example above). The CInt function is used to convert the looked up string
into an integer. (DLookup returns a string). The CStr function is used to
convert the number into a string.
Tom
http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________
:
Hi Tom, Thanks for responding. I'm not quite getting it just yet. I created
a two field table for this option group to select center printing or
left-aligned printing. The table's first column is for center or left and
the corresponding values are 1 or 2.
I used the control for the option group as this table, then for the
"current form" event, I have:
'Sets print option to the last one selected by user.
Private Sub Form_Current()
Me.frmPrintOptions.Value = Tables.tblPrintOptions.Column(1)
End Sub
I get a bug that says "tables" not a defined variable.
Can you tell me what I'm doing wrong?
Thanks again,
Karl..Across the water from you in Poulsbo