i have access 2007 form and i want to test if a particular control (toggle button) has the focus ,
something like :
if gotfocus(mytoggle) then
dosomething
endif
or maybe like :
if me.mytoggle.setfocus = true then
dosomething
endif
I have searched and cannot find this , can someone tell me what is correct top to do this ?
This for the current form:
If (mytoggle Is Me.ActiveControl) Then
This for the current Access.Application:
If (mytoggle Is Screen.ActiveControl) Then
Be careful, if no control has focus, *.ActiveControl may not exist.
Try this code - I've tried to account for .ActiveControl not existing.
Private Function isCurrentControl(thisControl As Control) As Boolean
On Error GoTo err_handler
If Not Me.ActiveControl Is Nothing Then
If (Me.ActiveControl Is thisControl) Then
isCurrentControl = True
Else
isCurrentControl = False
End If
Else
GoTo err_handler
End If
close_function:
On Error GoTo 0
Exit Function
err_handler:
isCurrentControl = False
Resume close_function
End Function
You just need to call the function and set the control as a parameter
''EXAMPLE: isCurrentControl(mytoggle)
Unfortunately, there are situations where the .ActiveControl is temporary non-existing ! When records are scrolled in a form, the procedure Form_Current() gets run. Already at the beginning, there is no focus anymore – the focus is reset to the previous field only after Form_Current() has terminated.
Related
I have a word-document and want to make a specific area uneditable (greyed out) if the value of a checkbox is true. My problem is that there are some errors, which I am unable to fix it by myself, hopefully you can help me out with it.
Sub checkBoxStatus()
'declare variables
Dim cb3Y As CheckBox
Dim cb3N As CheckBox
Set cb3Y = ActiveDocument.FormFields(1).CheckBox
cb3Y.Value = False 'just needed this for debugging, to see if I got the right checkbox
End Sub
I got an error message all the time when running this code fragment. "Runtime Error '5941' The Requested Member of Collection Does Not Exist". Unfortunately I don't know where I can edit the id of the right checkbox I need.
There is no CheckBox collection. Use something like:
Sub checkBoxStatus()
With ActiveDocument
If .FormFields(1).CheckBox.Value = True Then
' code for true here
Else
' code for false here
End If
End With
End Sub
how can I turn off/disable "The record source specified on this form does not exist" warning message? I created GUI that uses subforms, but the tables that are resource for these are generated in the process. So it means that GUI does not have any resource after opening the database and shows the error "The record source specified on this form does not exist". How can I turn it off/disable it? I tried to add DoCmd.SetWarnings False
but this has no effect on the error message.
You could leave the RecordSource empty or assign it some (empty) dummy record.
When you set the RecordSource to its actual value, the subform will automatically requery.
You need to look at Application.DisplayAlerts
Sub Example()
'do stuff
Application.DisplayAlerts = False
'Code that fires the warning message
Application.DisplayAlerts = True
'do stuff
End Sub
Instead of disabling alerts you can just assign some unrelated table to this form/subform.
Another solution - disable only this exact error if you have an error number (I guess it is 7874) like that:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 7874 Then
Response = acDataErrContinue
Else
Response = acDataErrDisplay
End If
End Sub
I'm having some trouble with a display object that I use to trigger a sub. The results of the display object are either true or false, and I use the _Change method. The code is quite simple.
Private Sub clamshellLblRequest_Change()
If Not tagDisplay Is Nothing Then
GoTo execute
Else
Set tagDisplay = LoadedDisplays
GoTo execute
End If
execute:
If clamshellLblRequest.Value = 1 Then
LogDiagnosticsMessage "Requesting clamshell label information"
Call labels.clamshell
End If
End Sub
When I first start the application, I get a "type mismatch" error (13) specific to this value. I have several other display objects that I use the same way with the same datatype but don't seem to have this problem. What else could be causing this?
Update:
I have a module I use standard timers with that include the following.
Public Sub tenthSec()
'Create a program delay, DateTime Timer resolution in MSWindows is 0.01. Needed for tag updates.
t = Timer
While Timer - t < 0.1
Wend
End Sub
When I execute call timers.tenthSec just before evaluating the value of the object, it doesn't seem to throw the type mismatch.
...
execute:
Call timers.tenthSec
If clamshellLblRequest.Value = 1 Then
LogDiagnosticsMessage "Requesting clamshell label information"
Call labels.clamshell
End If
End Sub
I wouldn't call this a solution, perhaps a band-aid. Any thoughts?
Agree with #Masoud about the wait. You could also use DoEvents inside of a loop, which allows other things to keep calculating, etc. Also, you shouldn't need the execute: and goto with the code you have, you should be able to just do something like this (note the change of Not Is Nothing to Is Nothing):
Private Sub clamshellLblRequest_Change()
If tagDisplay Is Nothing Then
Set tagDisplay = LoadedDisplays
End If
Application.Wait(Now + #0:00:01#)
' or
For i = 1 to 1000
DoEvents
Next i
If clamshellLblRequest.Value = 1 Then
LogDiagnosticsMessage "Requesting clamshell label information"
Call labels.clamshell
End If
End Sub
I have a main form with several tab controls with subforms in them. On the first tab or subform I have a "checkbox 1", which shall be:
checked if "textbox 1" is not empty
unchecked if "textbox 1" is empty
The code is directly put into the Class Object of "subform 1", that's why I thought I could use Me.
Here is my code, but I always get error messages :(
Private Sub Ctltextbox_1_AfterUpdate()
If Len(Ctltextbox_1.Value) = 0 Then
Me.checkbox_1.Value = 0
Else
Me.checkbox_1.Value = -1
End If
End Sub
That way I am getting the
Run-time error '2448': You can't assign a value to this object.
On the line that attempts to assign -1 to Me.checkbox_1.Value.
Try this. It is working for me. I moved it to the Ctltextbox_1_Change action. This will call the function whenever it is changed and will catch when you have nothing in the box as well. I also changed your values to True and False.
Please try this and let me know.
Private Sub Ctltextbox_1_Change()
If Len(frmSub.Ctltextbox_1.Value) = 0 Then
frmSub.CheckBox_1.Value = False
Else
frmSub.CheckBox_1.Value = True
End If
End Sub
you can also solve this in one line as mentioned below.
Private Sub Ctltextbox_1_Change()
frmSub.CheckBox_1.Value = IIf((Len(frmSub.Ctltextbox_1.Value) = 0), False, True)
End Sub
Ok, the simple answer is: you can not use the expression Len()= 0 for a not numeric field! So my working code for the subform is now:
Private Sub Textbox1_AfterUpdate()
If IsNull(Textbox1.Value) = False Then
Me.checkbox1.Value = True
Else
Me.checkbox1.Value = False
End If
End Sub
Thank you all for your help! :)
I tried creating a while loop to make sure it was set in case the user was typing but it seems like the macro magically stops when it tries to assign a value and the user is already typing something.
Basically how do you stop a macro from ending from this unexpected situation and how can I detect if the user is blocking?
from your comments you have a misconception of how VBA works.
first, a macro run from a module is taking control of the workbook, you can't detect user input in a while or for loop like that, what you want is to use an event listener like in this tutorial: a good site for vba basics
basiclly use the "Private Sub Worksheet_Change" option for a sheet/workbook.
also, if you're monitoring just one cell, check out this how to use worksheet change
Function IsEditing() As Boolean
If Application.Interactive = False Then
IsEditing = False
Exit Function
End If
On Error GoTo err:
Application.Interactive = False
Application.Interactive = True
IsEditing = False
Exit Function
err:
IsEditing = True
Exit Function
End Function