Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 439
  • Last Modified:

Saving caption of Label in form on button press

Right then...

I have a form which is not based on any tables/queries etc, and is only for printing sections of reports.

The Report ID is taken from an underlying form, that's fine.

if a user selects checkboxes 1, 2 & 3 (i.e. they want to print the pages for Section1, Section2 & Section3) and clicks print, only those pages are printed. Great.

I have now added a new function that will save their default selection. I thought, instead of creating a new table to store the values, I would simply use VB to change the caption of a label to include a * if the corresponding checkbox (and report) should be selected by default...

Can I get it to do it? Can I hell!!!


Working with only one checkbox (untill it works correctly) I have:


Check01 is the checkbox for printing the "cover sheet"

Label01 is the label associated with Check01

Label01 has the caption "1 - Cover Sheet"

Behind a button which is labeled "Set as default" I have the following code:

CODE
Me.Label01.Caption = IIf(Me.Check01.Value = True, IIf(Right(Me.Label01.Caption, 1) = Chr(42), Me.Label01.Caption, Me.Label01.Caption & Chr(42)), IIf(Right(Me.Label01.Caption, 1) = Chr(42), Left(Me.Label01.Caption, Len(Me.Label01.Caption) - 1), Me.Label01.Caption))

DoCmd.Save acForm, "PrintSelection"


Notes (Things that do happen!!):

If the check box is ticked, and the button is pressed, the caption changes.
If I comment out the DoCmd.Save, save the form, close it and re open it, tick the check box, press the button and close the form, I am prompted to save. - The only alteration is the caption change to incluse a *


Anyone had the same problem, or is it just me?


Any help greatly appreciated - More points available if it's considered a difficult question

Aubs
0
Aubs
Asked:
Aubs
  • 7
  • 6
2 Solutions
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You really don't need to issue a DoCmd.Save UNLESS you make changes to the design of the form ... this command saves the Form Object, not any data or data changes ... and you can change a Label's .Caption on the fly (meaning you don't need to enter Form Design View to do this), thus there is no reason to issue a DoCmd.Save ... if you're being prompted to save, Access must think there is unsaved design changes ... try completely closing your form (after commenting out the DoCmd.Save), then open it, tick the box, etc etc WITHOUT entering design mode ... do you still get the prompt to save? If you do, I'd immediately make a backup of the db, then import everything to a new, blank database ...
0
 
AubsAuthor Commented:
Hi LSM,

Thanks for your reply...

When the form is being used by a user (only one person will use the DB) - ONLY IN FORM VIEW, NOT DESIGN VEW, and they decide:

"Right, I want reports 1, 3 & 5 to be my default selection, because I use them all the time"

then they put ticks in the CheckBoxes for 1, 3 & 5 and press the 'Set As Default' button.

The VB code behind the button then modifies the design of the form by changing the Captions of the corresponding Labels for the selected CheckBoxes.

This the DoCmd.Save is required otherwise the user is prompted "Do you want to save..."

Thanks again, and I hope someone can help!!!

Regards,


Aubs
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
>> The VB code behind the button then modifies the design of the form by changing the Captions of the corresponding Labels for the selected CheckBoxes.

Changing a label's .Caption property will NOT trigger a form save event UNLESS some other code is opening the form in Design view and making changes ... I regularly change .Captions for labels in runtime .mde files (which CANNOT open the form's in Design view). If your program is asking the user to save DESIGN changes, then something else is triggering this ... what's the code behind your SetAsDefault button's Click event?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AubsAuthor Commented:
Thanks again for the comment!!

This is the code:

Private Sub SetAsDefault_Click()
Me.Label01.Caption = IIf(Me.Check01.Value = True, IIf(Right(Me.Label01.Caption, 1) = Chr(42), Me.Label01.Caption, Me.Label01.Caption & Chr(42)), IIf(Right(Me.Label01.Caption, 1) = Chr(42), Left(Me.Label01.Caption, Len(Me.Label01.Caption) - 1), Me.Label01.Caption))
Me.Label02.Caption = IIf(Me.Check02.Value = True, IIf(Right(Me.Label02.Caption, 1) = Chr(42), Me.Label02.Caption, Me.Label02.Caption & Chr(42)), IIf(Right(Me.Label02.Caption, 1) = Chr(42), Left(Me.Label02.Caption, Len(Me.Label02.Caption) - 1), Me.Label02.Caption))
Me.Label03.Caption = IIf(Me.Check03.Value = True, IIf(Right(Me.Label03.Caption, 1) = Chr(42), Me.Label03.Caption, Me.Label03.Caption & Chr(42)), IIf(Right(Me.Label03.Caption, 1) = Chr(42), Left(Me.Label03.Caption, Len(Me.Label03.Caption) - 1), Me.Label03.Caption))
DoCmd.Save acForm, "PrintSelection"
End Sub


I'm only trying it with the first three CheckBoxes because I don't see the point in writing it all and then having to change it all to make it work!!

"Changing a label's .Caption property will NOT trigger a form save event "
I do not want the user to go into Design mode, so I included the DoCmd.save

Hopefully this will help!

I'll up the points to 100 :)

Aubs
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
I think you're trying to permenantly change the label caption?

If that's what you're trying to do, then your method won't work ... DoCmd.Save only saves changes made while in Design view ... since you're never entering Design view, you aren't making changes to the object. If you want to reset the captions permenantly, you'd have to open the form in design view, make the changes (note: you can do this via code), then save the form, THEN open it in Normal view ... normally, if you're going to change labels and other items depending on user choices, you would store these in a table and set them in the Form's Open or Load event.
0
 
AubsAuthor Commented:
That's exactly what I'm trying to do!!

Earlier you said forms cannot be opened in design view in .mda files, so is it still possible do do what I want without using tables?

If not, do you have the code to do this for .mdb files?

Many thanks again LSM, your invaluable help is much appreciated :)
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Well, generally speaking Defaults would be saved to a table ... is there some reason you don't want to save to a table?

You could use an .INI file ... build a file (call it something like "MyDefaults.ini") and save it in the same location as the frontend. Then, you can set defaults in the .ini file:

[frmYourFormName]
Label1="This is Label1 Caption"
Label2="This is Label2 Caption"

Note that you would need to change the frmYourFormName to match your forms, and change the Label1, Label2, etc to match the names of the labels on your form.
On your form's Open event, read the ini section:

Private Sub Form_Open(Cancel As Integer)

  Me.Label1.Caption = GetINI(CurrentProject.Path & "\MyDefaults.ini", Me.Name, "Label1")  
  Me.Label2.Caption = GetINI(CurrentProject.Path & "\MyDefaults.ini", Me.Name, "Label2")

End Sub

When you need to write to the INI file, do this:

WriteINI CurrentProject.Path & "\MyDefaults.ini", Me.Name, "Label1", YourNewCaption
WriteINI CurrentProject.Path & "\MyDefaults.ini", Me.Name, "Label2", YourNewCaption


Copy and paste the following in a new STandard Module:

**************** CODE START ******************

  Option Compare Database
  Option Explicit

  Declare Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
  Declare Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long

Function WriteINI(INIFileName As String, INISection As String, INISetting As String, ValueToWrite As String) As Boolean
'/Purpose:
'/Created: 9/6/2004 12:26 PM
'/Created By: Scott

  Dim retVal      As Long

On Error GoTo Err_WriteINI
  retVal = WritePrivateProfileString(INISection, INISetting, ValueToWrite, INIFileName)

Exit_WriteINI:
  On Error Resume Next
  Exit Function

Err_WriteINI:
  Select Case Err
    'case
    Case Else
     MsgBox Err & ":" & Error$, vbCritical, "Module2" & ": " & "WriteINI"
  End Select
 
  Resume Exit_WriteINI
End Function

Function GetINI(INIFileName As String, INISection As String, INISetting As String) As String
'/Purpose:
'/Created: 9/6/2004 12:26 PM
'/Created By: Scott

  Dim mvarData       As String  ' receives the value read from the INI file
  Dim lngLength      As Long  ' receives length of the returned string

On Error GoTo Err_GetINI
  mvarData = Space(255)  ' provide enough room for the function to put the value into the buffer
  ' Read from the INI file
  lngLength = GetPrivateProfileString(INISection, INISetting, "none", mvarData, 255, INIFileName)
  mvarData = Left(mvarData, lngLength)  ' extract the returned string from the buffer
  GetINI = mvarData


Exit_GetINI:
  On Error Resume Next
  Exit Function

Err_GetINI:
  Select Case Err
    'case
    Case Else
     MsgBox Err & ":" & Error$, vbCritical, "Module2" & ": " & "GetINI"
  End Select
 
  Resume Exit_GetINI
End Function
0
 
AubsAuthor Commented:
For that, I think you deserve to get the points!

But unfortunately, I have to leave the DB in it's original state, i.e. no extra tables and no extra files (.ini files etc).

0
 
AubsAuthor Commented:
Oops, should have finished writing that before I clicked send!!

I got the answer I was after from another site:

http://www.tek-tips.com/viewthread.cfm?qid=912261

I'll share the code I used for others who may be interested:

Public Function RestoreEcho()
On Err GoTo Err_RestoreEcho
   Dim Msg As String, Style As Integer, Title As String
   
   Application.Echo False
   
   Msg = "Application Echo is On!"
   Style = vbInformation + vbOKOnly
   Title = "Display Update Restored! . . ."
   MsgBox Msg, Style, Title

Exit_RestoreEcho:
    Exit Function
Err_RestoreEcho:
    MsgBox Err.Description
    Resume Exit_RestoreEcho
End Function
[/code]

[code]
Public Sub SetPrintDefaults(frm As Form)
On Err GoTo Err_PrintDefaults
    Dim n As Integer, Ary(1 To 14) As String
    Dim hld As String
    Dim frmName As String
    Application.Echo False
    frmName = "PrintSelection"
    Set frm = Forms(frmName)
    For n = 1 To 13
        If frm("Check" & n) Then
            hld = "True"
        Else
            hld = "False"
        End If
        Ary(n) = hld
    Next
    If Forms!PrintSelection!PreviewButton = True Then Ary(14) = "1"
    If Forms!PrintSelection!PrintButton = True Then Ary(14) = "2"
    DoCmd.OpenForm frmName, acDesign
    Set frm = Forms(frmName)
    For n = 1 To 14
        If n = 14 Then
            If Ary(n) = "1" Then
                Forms!PrintSelection!PrintPreviewLabel.Caption = "Print/Preview*"
            End If
            If Ary(n) = "2" Then
                Forms!PrintSelection!PrintPreviewLabel.Caption = "Print*/Preview"
            End If
            GoTo LastOne
        End If

        hld = frm("Label" & n).Caption
        If InStr(1, hld, "*", 1) > 0 Then hld = Left(hld, InStr(1, hld, "*", 1) - 1)
        If Ary(n) = "True" Then hld = hld & "*"
        frm("Label" & n).Caption = hld
    Next
LastOne:
    DoCmd.Close acForm, "PrintSelection", acSaveYes
    DoCmd.OpenForm "PrintSelection", acNormal
    Application.Echo True

Exit_PrintDefaults:
    Exit Sub
Err_PrintDefaults:
    MsgBox Err.Description
    Resume Exit_PrintDefaults
End Sub



Thanks again for your help, and I think for the effort you went to, you deserve the points!

Regards,

Aubs
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Then I see no way to guarantee persistence ... could you, perhaps, set these values in the registry? This would be extreme overkill, btw, for the few options you need to set. But if you can't use tables or ini, I see no other way to save this information ...
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
>> I have to leave the DB in it's original state, i.e. no extra tables and no extra files (.ini files etc).

Glad you got this resolved, but opening your form in design view would CERTAINLY violate the first portion of your statement ... and, doing so can also decompile your application, and decompiled=unstable ... good luck, and thanks for the points!!
0
 
AubsAuthor Commented:
I see what you mean, but since each mdb file is a front end to another DB and will be unique to each user, there shouldn't be a problem.

In any case, what I have done does not affect data in the slightest so corruption of data shouldn't occur.

Thanks again all the same :)

Aubs
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
True regarding data corruption ... but by doing this, your frontends can (and almost certainly will) corrupt ... and if this is ever moved over to an .mde file, your code will fail. Don't mean to be argumentative, just want to put this in the thread for future searches so that users will know the possible pitfalls of using the approach you found.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now