VBA Code to select current form

shwilsonjr

Registered User.
Local time
Today, 04:48
Joined
Nov 11, 2012
Messages
20
I have a command button on my main form that I want to use to set the property of the current form to read only. Not sure of the proper syntax to perform this feature.
 
To make a control Read Only, you can use;
Code:
Me.YourControlName.Locked = True
This will still enable the user to click or tab into the control, if you want to prevent this as well use;
Code:
Me.YourControlName.Locked = True
Me.YourControlName.Enabled = False
 
Can I use this in a If...Then sequence to lock the control after it has been clicked? If so can you show the syntax?
 
Yes it can. How will you be deciding whether to lock or not?

You will however need to move the focus to another control prior to Locking or Disabling a control.
 
Once the Report form is completed the user will click the cmdReportComplete command button to make the form read only. Once that occurs I would like to lock or disable the cmdReportComplete button.
 
So you want to be only able to click that button once for any given record? and then never ever again?
 
I have a from named frmOpeningForm which has three command buttons as follows:

cmdNew (opens new form)
cmdEditReview (allows users to edit and review the Daily Report that they are completing for their shift. Once completed they will click the cmdReportComlete button at which time I would like to disable that command button.)
cmdView (Which will be used by upper management to view the reports. I would like to set up a login form for this cmd button that will require a user name and password to allow them to see the cmdReportComplete button and be able to edit a report if necessary.)
I hope that I explained the functions that are needed.
 
It's easy enough to Disable your button, the thing you need to consider is when you want to re-enable it. The following will disable your button, simply make it the last event in your Buttons On Click event;

Code:
Me.SomeotherControl.SetFocus
Me.YourCommandButton.Locked = True
Me.YourCommandButton.Enabled = False

To re-enable it simply use;
Code:
Me.YourCommandButton.Locked = False
Me.YourCommandButton.Enabled = True
 
Should I use the VBA editor to enter this code for the cmdReportComplete button?
 
Yes, select Event Procedure from the Drop down list and click the ellipsis.

attachment.php
 

Attachments

  • Capture.PNG
    Capture.PNG
    32.2 KB · Views: 506
This the code I used for the cmdReortClosed command button and received a compile error “variable not defined”

Option Compare Database
Option Explicit

Sub macReportComplete()
If Form_frmDailyReports.cmdReportComplete = True Then
Form_frmDailyReports.CurrentRecord = NoEdit
End If
If DailyReports.cmdReportComplete = True Then
DailyReports.cmdReportComplete = Locked
End If
End Sub
 
Which line of your code was highlighted when you received the error message?
 
Form_frmDailyReports.CurrentRecord = NoEdit

"NoEdit" is highlighted in blue, When I click okay the following is highlighted yellow:
Sub macReportComplete()
 
Form_frmDailyReports.CurrentRecord = NoEdit

"NoEdit" is highlighted in blue, When I click okay the following is highlighted yellow:
Sub macReportComplete()

1. Do NOT use this syntax:
Form_frmDailyReports.cmdReportComplete

You should be using either
Me.cmdReportComplete
(if the code is on the same form as the control you are referring to)

or

Forms!frmDailyReports.cmdReportComplete
if the code is on a different form and needs to refer to a control on a different form.

Using

Form_FormNameHere

can cause problems for you.
 
This is current code that I have tried. I tried the with the Me., but it didn't work either. I got the variable not defined error with the "NoEdit" highlighted


Option Compare Database
Option Explicit

'------------------------------------------------------------
' EditReview
'
'------------------------------------------------------------


Sub macReportComplete()
If Forms!frmDailyReports.cmdReportComplete = True Then
Forms!frmDailyReports.CurrentRecord = NoEdit
End If
If DailyReports.cmdReportComplete = True Then
Forms!DailyReports.cmdReportComplete = Locked
End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom