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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I want to Lock the Subform?

Status
Not open for further replies.

Fred1981

Technical User
Jun 1, 2010
12
SY
Hi All


I have a main form( Employee)and subform( EMP_Task) it is a one to many relationship

On the main form I have a print Preview button

I want to have a code in the Print Preview Button to lock the SubForm( Emp Task) when clicking the Buttton

but i still want the user to have the ability to add a new record but not to modify the old records after clicking print Preview Button

I can lock the Subform using this code

Private Sub Form_Current()
If Me.NewRecord Then
Me.AllowEdits = True
Else
Me.AllowEdits = False
End If
End Sub

As soon as you move to the new record the old record gets locked
(I can't edit it if the user makes a mistake)
I would like it through the Print Preview button

Regards
 
I honestly feel like I'm running in circles from your post. It sounds to me like you're not sure what you want. You say you want it locked, and you want them to be able to edit, but NOT be able to edit???? [ponder]

Can you give a step-by-step of how you want this to work (an example), and then point out where your process is not working as you desire?
 
Sorry I meant when they click the Print Button they get the subform Locked for the current recordds

However they can make an entry for a new record and edit but when they re click the Print preview button again they new records that got entered will be locked

so what I mean everytime you click the Print Preview button the available new entries will be locked
 
I think you might need to add a yes/no field to your table that stores whether the record is locked or not. Use the print preview button to set the locked field to true. Then use the On Current event of the subform to lock or unlock the record.

Duane
Hook'D on Access
MS Access MVP
 
can you explain the steps of doing that

and where to go for I am not an Access guru
 
Where do you have trouble? I listed about three steps. How far did you get? Did you add the field? What's the field name?

Can you share the code for your "Print Button"?

Do you have any code in the On Current event of your subform? Do you know how to get to the code window in your form?

Duane
Hook'D on Access
MS Access MVP
 
Private Sub rptTime_Click()
On Error GoTo Err_rptTime_Click


stDocName = "Employee2"
DoCmd.OpenReport stDocName, acPreview

Exit_rptTime_Click:
Exit Sub

Err_rptTime_Click:
MsgBox Err.Description
Resume Exit_rptTime_Click

End Sub



and on the Oncurrrent Subform I had

Private Sub Form_Current()
If Me.NewRecord Then
Me.AllowEdits = True
Else
Me.AllowEdits = False
End If
End Sub

This work fine for locking the record after you finish the entry but Iwant it on the button as explained before

there is more than one filed that I want locked when you click the Print Button
 
You did not mention anything about "Did you add the field? What's the field name?"

Let's assume you added a yes/no field [IsLocked] with a default value of 0.

Code:
Private Sub rptTime_Click()
    On Error GoTo Err_rptTime_Click
    stDocName = "Employee2"
    DoCmd.OpenReport stDocName, acPreview
    Me.IsLocked = True

Exit_rptTime_Click:
    Exit Sub
Err_rptTime_Click:
    MsgBox Err.Description
    Resume Exit_rptTime_Click
End Sub
and on the Oncurrrent Subform try
Code:
Private Sub Form_Current()
    If Not Me.IsLocked Then
        Me.AllowEdits = True
       Else
        Me.AllowEdits = False
     End If
End Sub

Duane
Hook'D on Access
MS Access MVP
 
These are the name of the fields
EMP_No
Task Code
Date_Task
OverTime
start date
end date
Task Desc
=(DateDiff("n",[start date],[end date]))\60 & Format((DateDiff("n",[start date],[end Date])) Mod 60,"\:00")

=(DateDiff("n",[start date],[end date])/60)*Forms!Employee!rate


I tried your code but it didn't work I get an error saying can't Find expression I tried it for one field OverTime
 
I can Lock the Record in the Subform using this

Private Sub Form_Current()
If Me.NewRecord Then
Me.AllowEdits = True
Else
Me.AllowEdits = False
End If
End Sub


However I wan ti tto run from the Print Button

 
So, in other words, you want the same code as "Form_Current" to run from your print button? If that's all you want, then just cut the code from Form_Current, and paste it to your button's code in the Form module..
 
Fred1981,
You didn't add the field [IsLocked] to your table. My solution suggests you add the field which can be used to determine if the record should be locked or not. You could also use a date field named [PrintDate] and insert the date of printing.

Duane
Hook'D on Access
MS Access MVP
 
How are ya Fred1981 . . .

Your problem revolves around users making mistakes/typos and the need to go back and edit the currently saved record. This will always be the last record entered (just above the new record line). The ability to remove locking from this record [blue]only[/blue], is whats needed. The following code will perform the task:
Code:
[blue]   If Me.NewRecord Or Me.Recordset.RecordCount = Me.CurrentRecord Then
      Me.AllowEdits = True
   Else
      Me.AllowEdits = False
   End If[/blue]
Where to put the code is the next problem. If you don't mind the last record always being editable, then the subforms [blue]On Current[/blue] event will handle it automatically.

If the above doesn't satisify you, then all your left with is manual control of unlocking. This puts the code in the [blue]On Dbl Click[/blue] event of the subform. With this you simply double-click the record selector of the last record to unlock! Be aware ... if you use this method you'll still need the locking code ...
Code:
[blue]   If Me.NewRecord Then
      Me.AllowEdits = True
   Else
      Me.AllowEdits = False
   End If[/blue]
... in the subforms [blue]On Current[/blue] event. Your just using the [blue]On Dbl Click[/blue] event to override.

Both methods above relieve any locking control via your print button. So be sure to rem out or remove this locking code from the button. This is simply to prevent any interaction.

[blue]Your Thoughts? . . .[/blue]


See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Thanks Mate it works

However can't we have it on the Print Button

I have uploaded it the Db3

and I also have the problem of the date
I want to have a list or a combo box that passes the value of the Month such as Dec or Mar.... into the Print Preview Button so I can print the whole month rather then printing all the records
 
Thanks Mate it works . . .

You can move the code to the print button if you like. However ... since he button is on the mainform you'll have to make sure you reference the subform in the code ...



See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Fred1981 . . .

Need to add ... if you move the code to the button the subforms last record has to have focus to be unlocked!

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
how do you refernce the subform in the code
 
Fred1981 . . .

? [surprise]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top