code help w/enable-disable features

momaw nadon

Registered User.
Local time
Yesterday, 22:04
Joined
Apr 19, 2013
Messages
10
Hi,
Novice to Access, working on Access 2010.
First problem: looking for code to allow me to do multiple things with one button. I can get part of what I need it to do working, as the button will enable fields, change display text (“edit” to “save” and back again), and disable/enable search. Though I’ve been unable to return the disabled form fields once the button it clicked again.
Reason for disabling search is to prevent mistakes and incomplete entries.
Here is the code I have so far:

Code:
[SIZE=3][FONT=Calibri]Private Sub Form_Load()[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   Me.sfrm.Enabled = False[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   Me.site.Enabled = False[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   Me.local.Enabled = False[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   Me.add.Enabled = False[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   Me.city.Enabled = False[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   Me.state.Enabled = False[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   Me.zip.Enabled = False[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]End Sub[/FONT][/SIZE]
 
[SIZE=3][FONT=Calibri]Private Sub cmdedit_Click()[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   Me.sfrm.Enabled = True[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   Me.add.Enabled = True[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   Me.city.Enabled = True[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   Me.state.Enabled = True[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   Me.zip.Enabled = True[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   If cbosearch.Enabled = True Then[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   cbosearch.Enabled = False[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   cmdedit.Caption = "Save"[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   Else[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   cbosearch.Enabled = True[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   cmdedit.Caption = "edit"[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   End If[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]End Sub[/FONT][/SIZE]
 
[SIZE=3][FONT=Calibri]Private Sub Form_Current()[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   cbosearch.Enabled = True[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]   cmdedit.Caption = "edit"[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]End Sub[/FONT][/SIZE]

Second problem: the above form has a subform. I have the fields disabled upon opening of both main and sub. I don’t have a problem with the main navigation features working with fields disabled, but do have the problem with nagivation being disabled on subform. I need the nagivation on subform to work when fields are disabled, so is there a way to correct.
Thank you for your help on my issues.
MN
 
Reason for disabling search is to prevent mistakes and incomplete entries.

May be it would be easier to check for incomplete fields etc. by putting some code in the Before Update of the form.
There are plenty of posts here about this subject, just do a search.

Catalina
 
I have a Function that I call whenever I need to lock down all of the controls and I use it in the form load event of my form. With this code, you can still navigate through your subform.


Code:
Private Function Lockdown() 'locks controls at load
 
    For Each tb In Me.Controls
        If TypeOf tb Is TextBox Then
            tb.Locked = True
            tb.BackColor = vbWhite
        End If
    Next
    For Each cb In Me.Controls
        If TypeOf cb Is ComboBox Then
            cb.Locked = True
            cb.BackColor = vbWhite
        End If
    Next
    For Each subF In Me.Controls
        If TypeOf subF Is SubForm Then
            subF.Locked = True
        End If
    Next
 
End Function

tb = textboxes
cb = combo boxes
subF = subform

the .backcolor will change the color of the textbox and combo box; I use this to let the user know that the controls can be edited when the color changes.

For example: I use white for locked and when the user clicks on my edit button, then the controls turn a light greenish blue color.


Then when I want to unlock the forms I just use the reverse of the above code and place it another Function called "UnlockFields"

Code:
Private Function UnlockFields() 'unlocks controls
    For Each tb In Me.Controls
        If TypeOf tb Is TextBox Then
            tb.Locked = False
            tb.BackColor = &HFAFAD2
        End If
    Next
 
    For Each cb In Me.Controls
        If TypeOf cb Is ComboBox Then
            cb.Locked = False
            cb.BackColor = &HFAFAD2
        End If
    Next
    For Each subF In Me.Controls
        If TypeOf subF Is SubForm Then
            subF.Locked = False
 
        End If
    Next
End Function

When you want to use the functions, then just call them:

example: Call Lockdown or Call UnlockFields

Oh, and don't forget to declare the controls:

Code:
Dim tb As Control
Dim cb As Control
Dim subF As Control

Hope this helps
 
Thank you for the help as I really like the simplicity of going your route, but keep getting a Compile error: Sub or Function not defined. Am I leaving out something when I call the functions, as to why the error?

Error occurs on the first line of this part of my code:
Code:
 Private Sub cmdsedit_Click()
     Call unlockfields
 
     If cbosearch.Enabled = True Then
       cbosearch.Enabled = False
       cmdedit.Caption = "Save"
     Else
       cbosearch.Enabled = True
       cmdedit.Caption = "Edit"
     End If
 
End Sub

Though I do have a question, as how would I include back color onto my subform?

Thanks again for all the help
MN
 
Can you post the function or sub you created. Also, are you calling the code from the form or from a module?


Oh, on the color change for the subform, I have not been able to get that to work yet
 
Using your code from above. Just with the field colors changed. I believe it's in the form, as the function is written in the form code.

Thank you again for your time.
MN
 
Try removing the "Call" from in front of the unlockfields and see if that changes anything.

Also, did you copy and paste the code into your form? if you did, then something is not setup correctly, because the unlockfields should be UnlockFields
 
Removing the "Call" didn't change anything still getting the sub or function not defined error.

I didn't copy/paste, but I double checked to see if the capital letters were correct, which they are.

Thanks again.
MN
 
Was able to fix my problem, it turns out that I had a misspelling in my code. Though I have found that my button doesn't return to load state of my form like I need it too.

So I need a dynamic or duel function button in that I click it to edit and then click it again to save changes and return to form load state. Any clue on how to make my button work like that.

Thanks
MN
 

Users who are viewing this thread

Back
Top Bottom