Link to home
Start Free TrialLog in
Avatar of Aubs
Aubs

asked on

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
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

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 ...
Avatar of Aubs
Aubs

ASKER

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
>> 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?
Avatar of Aubs

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aubs

ASKER

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 :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aubs

ASKER

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).

Avatar of Aubs

ASKER

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
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 ...
>> 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!!
Avatar of Aubs

ASKER

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
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.