Link to home
Start Free TrialLog in
Avatar of J G
J G

asked on

view mode vs edit mode

I have a data base with a split form.  I want to make it so the form defaults to  "view mode" then with a double click of a record on the bottom list portion of the form (or another trigger of somesort) the record will open in an "edit mode"
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

You already have that - in the status bar bottom, right.

/gustav
Avatar of J G
J G

ASKER

Not Exactly.....

When I switch between Layout view/Form view my form controls/search box doesn't work.  Can I make it so all of my search tools work but not be in edit mode?

Also could I assign a command button and/or keyboard shortcut to do this?
If you set allow edits to no, you effectively lock the search fields also.  There are several ways around it.  One simple way is to add an "Edit" button on the form.  The Edit button should set a form level variable to "allow".  The current event should set the variable to "locked".  Then the form's BeforeUpdate event should check the variable and disallow updates or make the user confirm if you want to ensure that the user isn't just leaning on the keyboard.  Then you have to set the flag back to "locked" in the form's AfterUpdate event.

BeforeUpdate event:

If bEdit = True then
Else
    If Msgbox("Are you sure you want to modify the record?", vbYesNo) = vbYes Then
    Else
        Cancel = True
        Me.Undo
        bEdit = False
        Exit Sub
    End If
End If
Oh, I had design mode in mind.

/gustav
Avatar of J G

ASKER

I want this to apply to all fields and controls on the form except a search field and control buttons.  So.. instead of creating a form level variable, could I just apply this to specific controls?  

I.E. I want to be able to search without having to hit edit.
You can't use bound controls for searching if that is what you are doing.  Modifying a bound control changes the underlying record.  Make all your search fields unbound then the search will not inadvertently change a record.
Avatar of J G

ASKER

I'm a little confused how to implement this.  I have a form with all bound fields, and one unbound search field.

Could you provide an example of defining the form level variable:

"The Edit button should set a form level variable to "allow".  

Also, what would be the "current event"

The current event should set the variable to "locked". "
Here's the code from a form's class module that sets and unsets the bAllowUpdate flag in the necessary spots.  I named the button - cmdEdit
Option Compare Database
Option Explicit

Public bAllowUpdate As Boolean

Private Sub cmdEdit_Click()
    bAllowUpdate = True
End Sub

Private Sub Form_AfterUpdate()
    bAllowUpdate = False
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
    If bAllowUpdate = True Then
    Else
        MsgBox "Updates are not allowed.  Please press 'Edit' button.", vbOKOnly
        Cancel = True
        Me.cmdEdit.SetFocus
        Exit Sub
    End If
End Sub

Private Sub Form_Current()
    bAllowUpdate = False
End Sub

Open in new window


If you want to control deletes, use the BeforeDeleteConfirm event.
If you want to control adds, use the BeforeInsert event.
Avatar of J G

ASKER

I don't want there to be a "Are you sure you want to modify the record" question.

 Just a
1) edit button that unlocks the fields
2) the fields are editable, you can tab thru them and change values without writing to the table (with the last record information initially still stored in them)
3) save button that will write the changes to the table and relock the fields with a "item updated" message box.

Attached is a sample db.  I don't want you to do the work.  I will figure it out.  Could you tell me if this is possible, and give me a road map of how to implement.  Maybe code samples using my field names, and how to configure.
C--Users-josht-Desktop-Produce-Pri.accdb
SOLUTION
Avatar of PatHartman
PatHartman
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 J G

ASKER

I can only get it to work if I put the all the code in a class module associated with the form
ASKER CERTIFIED 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
I can only get it to work if I put the all the code in a class module associated with the form
If you are putting it in a form's class module, you would have to duplicate it for every form where you needed the code or you would have to make sure that the form containing the code was always open.  Put the code in a standard module - NOT a class module.

Gus, when you set AllowEdits to No, that locks all unbound search fields also.  The code I posted is generic and you use the Tag property of the control to override it so you can have some controls ALWAYS locked such as autonumbers and application generated values such as ChangedBy and ChangedDT and other controls NEVER locked such as search fields and buttons.
Read again. It is not AllowEdits but AllowEdit. It is so clever.

/gustav
I think we determined a couple of days ago that I need new glasses.  Perhaps I should also not post before I have coffee in the morning.  Looks pretty much like my original suggestion.
The smart thing is that it blocks the user from any editing of existing records while allows new records to be added.
Of course, this is not flexible as the tag method is.

/gustav
I thought about that but I prefer consistency so unless the users specifically request otherwise, even new records open locked.

I should also have mentioned that the locking code I posted is used in conjunction with the user's security so each form checks the user's authorization when it opens and uses that as the basis for locking / unlocking where appropriate.  I didn't post any of that since it was well beyond the initial question.