Access VBA: How to control element when it has focus - ms-access

I'm trying to disable a button when it is clicked:
Private Sub submitButton_Click()
Me.submitButton.Enabled = False
If dataPath = "" Or skuPath = "" Then
MsgBox "Please enter spreadsheet locations."
Else
Call importExcelData(dataPath, "data")
Call importExcelData(skuPath, "skus")
If validate = True Then
Call generateReports
End If
End If
End Sub
I'm getting a runtime error: "You can't disable a control while it has the focus".
Do you know a way around this?
Thanks!

You must first set the focus to a different control.
That said, if you really have no other control to send the focus to (which I'd find hard to believe), there is a "workaround." You can add a command button and set its TabStop property to False and its Transparent property to True and set the focus to that control.

Related

Get an Array of Controls in Access Form

Hello I'm very new to vba in Access
I'm trying to get an array of the controls in my form through a button.
My end goal is to press a button and have it loop through the array of controls and ask if it is equal to -1 (the controls are Yes/No check boxes) and if it is, to open a corresponding form.
Anyone know how to do this?
Thanks in advance for the help!
Use code like this in the OnClick event of your button:
Const FormName As String = "CorrespondingFormName"
Dim Control As Control
For Each Control In Me.Controls
If Control.ControlType = acCheckBox Then
If Control.Value = True Then
Exit For
End If
End If
Next
If Not Control Is Nothing Then
' A checked checkbox was found.
DoCmd.OpenForm FormName
End If

Unable to disable all fields of a ms-access userform on load

I have lots of controls on my userform and I want to disable all of them on load except for "login button" and "Shift Date" textbox, so i am using the code below:
Private Sub Form_Load()
Call GetValues
Me.WlcmLabel.Caption = "Hi " + GetUserName + " ! "
Dim ctrl As Control
For Each ctrl In Me.Controls
ctrl.Enabled = False
Next
Me.ShiftDate.Enabled = True
Me.LoginBtn.Enabled = True
Set ctrl = Nothing
End Sub
but this gives me an error on load saying "object does not support this property or method."
all the controls will get enabled as soon as the user clicks on login button.
What would be the mistake in my code ?
Please ask if any other information required.
Thank You !
Try locking the control instead. I have used the locked property below, but I use the tag property of each control to identify it as lockable. So the controls you don't want to lock will not get anything in the tag property, and therefore will not lock. For your application, you could flip the logic since you only want leave the two controls unlocked.
For Each ctlCurr In Me.Controls
If ctlCurr.Tag = "Lockable" Then
ctlCurr.Locked = True
End If
Next
You can't disable label control that's why that error is coming. You have to check the type of control.
For Each ctrl In Me.Controls
With ctrl
a = TypeName(ctrl)
Select Case .ControlType
Case acLabel
Case acEmptyCell
Case Else
ctrl.Enabled = False
End Select
End With
Next ctrl
Apply disable only when it's not label.

Bypass shift with pop up password box

I am trying to create an access database that is shut off from the users even the bypass shift option. I have done a code that does this but what I want to add is a pop up box when the shift key is pressed that allows a user to enter a password and the bypass shift option will work again. Does anybody know if this is possible?
Thanks!
Function ap_DisableShift()
On Error GoTo errDisableShift
Dim db As DAO.Database
Dim prop As DAO.Property
Const conPropNotFound = 3270
Set db = CurrentDb()
db.Properties("AllowByPassKey") = False
Exit Function
errDisableShift:
If Err = conPropNotFound Then
Set prop = db.CreateProperty("AllowByPassKey", _
dbBoolean, False)
db.Properties.Append prop
Resume Next
Else
MsgBox "Function 'ap_DisableShift' did not complete successfully."
Exit Function
End If
End Function
I don't think it is possible to catch the startup event while shift is disabled. What I always do is to add a hidden password text box on the startup form. The password text box can be activated by a 'hidden button'. The effect only applies to the next startup event (and on), so it is not as direct as you'd like it, but it is reliable.
Add a rectangle to the startup form that will act as a mouse catcher. Make sure that Back Style is set to Normal, but with the same color as the form's BackColorso it is invisible.
Add a hidden text box to startup form. Set Input Mask as Password
Add a Double_Click event to the rectangle, that will unhide the password text box
Add an AfterUpdate event to the password text box, that will enable the shift key (AllowBypassKey) if the password is correct. You can enter the access interface at the next startup.
To use the AllowBypassKey:
Public Sub CreateLockProperty()
CurrentDb.Properties.Append CurrentDb.CreateProperty("AllowBypassKey", dbBoolean, True)
End Sub
Public Sub SetToolLock(bLocked As Boolean)
CurrentDb.Properties("AllowBypassKey") = Not bLocked
End Sub
Public Function ToolLocked() As Boolean
ToolLocked = Not CurrentDb.Properties("AllowBypassKey")
End Function
First you have to create the property by running the first procedure. After that you can use the other two.

Message box that confirms if a user wants to leave a form

I had a piece of VBA running, and for some reason it has suddenly stopped working.
On my form unload event I have the code:
if isnull(me.field) then
ans=MsgBox("Warning you have not entered all the data. Do you want to exit the form", vbYesNo, "Error")
If ans=vbNo then
Cancel=True
end if
end if
This worked for a couple months, when the user exited the warning message would appear, and if they select no the form would not exit. Now when I click no I get an error:
Run time Error 3270. Property not Found
I changed the code to:
if isnull(me.field) then
ans=MsgBox("Warning you have not entered all the data. Do you want to exit the form", vbYesNo, "Error")
If ans=vbNo then
docmd.cancelevent
end if
end if
Now I get error:
Runtime Error '2001' You Canceled The Previous Operation
Which is what I want.
How do I get a messagebox to confirm that a user wants to exit a form?
Edit: I realize that the exit warning works when I exit the form by pressing x in the upper right, but when I exit using a button with the docmd.close I get the errors. Any way around that?
Simply use the button click event.
Where CommandButton14 is the name of your exit button.
EDIT for users comment.
Have your exit button call the UserForm_QueryClose event.
Private Sub CommandButton14_Click()
'UserForm_QueryClose 0, 0
Unload Me
End Sub
Ask your question in that event. If they say yes end the app or unload the form. If they say no, cancel.
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Dim Response As Integer
' Displays a message box with the yes and no options.
Response = MsgBox("Warning you have not entered all the data. Do you want to exit the form?", vbYesNo)
' If statement to check if the yes button was selected.
If Response = vbYes Then
'Cancel = False
End
Else
Cancel = True
End If
End Sub
You can take everything out of your unload event.
I feel like I am missing something because the path you chose seems a little over complicated.
Firstly, with my database I always make sure to set all of my forms Close Button property to "No" this way you always have control of when the user closes the form.
So from that point you just need this code attached to your close button:
Private Sub btnClose_Click()
Dim blnClose As Boolean
Dim strResponse As String
'Default to true so it always closes unless one or more future checks fail
blnClose = True
If IsNull(Me.Field) Then
strResponse = MsgBox("Warning you have not entered all the data. Do you want to exit the form", vbYesNo, "Error")
'User wants to cancel close toggle Boolean to false
If strResponse = vbNo Then
blnClose = False
End If
End If
'If nothing has toggled to false then close the form
If blnClose = True Then
DoCmd.Close , ""
End If
End Sub

Proper syntax for activating record selectors

I would like to know the proper syntax to write a string which will activate and de-activate the record selectors on subform 'frmEffortImpact' located on the main form 'frmProjectCharter01'. Unfortunately I continue to get an error message "properties or methods not supported by object". This is the code i am currently using:
Private Sub cmdAddImpacts_Click()
If Me.AllowAdditions = True Then
Forms![frmprojectcharter01]![frmEffortImpact].RecordSelectors = False
ElseIf Me.AllowAdditions = False Then
Forms![frmprojectcharter01]![frmEffortImpact].RecordSelectors = True
End If
With Forms![frmprojectcharter01]![frmEffortImpact] you are reaching the subform control (which has properties like SourceObject and LinkMasterFields). To get the normal Form properties of the form being used as a subform, you need to access the .Form property of the subform control.
Forms![frmprojectcharter01]![frmEffortImpact].Form.RecordSelectors = False
Then you can reach .RecordSelectors and others.