Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preserving Values from one session to next

Status
Not open for further replies.

DanScott

Programmer
Sep 13, 2002
14
US
I know this is possible, but I just can't get there. I have a form with a listbox as the control for several other fields, the list box is the "Month" ie JAN, FEB etc. I want to save the last month as the default setting for the next session. I have read about creating an INI or a table, but I cannot figure out how to do this. Any help would be greatly appreciated in getting me started. I am using Access98 on a 98 enviroment.

Dan
 
I could be wrong here but it looks like a dlookup on the listbox defoult value.

Herman
 
How many users are using your DB?

For persisting values on a per/user basis I normally save them to the registry:
Code:
    SaveSetting appname, section, key, setting

Then read them back when the form loads:
Code:
    strMonth = GetSetting(appname, section, key[, default])

If all users need the most recent value you should create a table with the 'CurrentMonth' field and just update its value when your form closes, and read it back when the form opens.
Code:
Private Sub Form_Load()
On Error GoTo ErrHandler
  
  Dim rst As Recordset
  Dim db As Database
  
  Set db = CurrentDb
  Set rst = db.OpenRecordset("Months", dbOpenTable)
  
  With rst
    If Not .EOF Then
      ListBox1.Value = .Fields("CurrentMonth")
    Else
      ListBox1 = ListBox1.Column(0, 0)
    End If
    .Close
  End With

ExitHere:
  On Error Resume Next
  Set rst = Nothing
  Set db = Nothing
  Exit Sub
ErrHandler:
  MsgBox Err & "-" & Err.Description
  Resume ExitHere
End Sub

'@-------------------------------------------------------@

Private Sub Form_Unload(Cancel As Integer)
On Error GoTo ErrHandler

  Dim rst As Recordset
  Dim db As Database
  
  Set db = CurrentDb
  Set rst = db.OpenRecordset("Months", dbOpenTable)
  
  If ListBox1.ListIndex = -1 Then GoTo ExitHere
  
  With rst
    If .EOF Then
      .AddNew    ' handle first unload
    Else
      .Edit
    End If
    .Fields("CurrentMonth") = ListBox1.ItemData(ListBox1.ListIndex)
    .Update
    .Close
  End With
  
ExitHere:
  On Error Resume Next
  Set rst = Nothing
  Set db = Nothing
  Exit Sub
ErrHandler:
  MsgBox Err & "-" & Err.Description
  Resume ExitHere
End Sub
VBSlammer
redinvader3walking.gif
 
Subs worked great, just one small problem, I use the sub_current form to update the other boxes based on the control box, and it seems to go to the first record of the control query no matter what the load is as the sub_current use some simple = statements ei:

newmonth = Monthname (field from select query)
bxdtestart = Startdate

etc for several boxes, if I comment out the first line, the load seems to be work in the sense it uses the last value, but its does not seem to sync up with the current procedure, but I think i'm missing a command to have the sub_current use this value first.

any help is greatly appreciated.
 
I'm not sure I follow the configuration you have, but one thing I do with this kind of scenario is to check whether the current record is a new record or not, and only unpdate the default values if it is:
Code:
Private Sub Form_Current()
  If Me.NewRecord Then
    ' update defaults
  End If
End Sub

You might also need to Requery the form after you set the default value of the primary control. VBSlammer
redinvader3walking.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top