Access VBA Set Focus in Column - ms-access

good afternoon,
I have an access with a subform (sub_frm_robo2) that has a specific column (CD_SENHA).
In the Form Load event I put the code: (Me.sub_frm_robo2.Form! SENHA.InputMask = "Password")
and I am trying to create a condition that at the moment this Column
(CD_SENHA) receives focus (Me.sub_frm_robo2.Form!SENHA.SetFocus),
the data mask be removed (Me.sub_frm_robo2.Form!SENHA.InputMask = "")
and when the focus changes to the next column return the data mask to the initial format (Me.sub_frm_robo2.Form! PASSWORD = "Password")
Below some images to better exemplify
Before Focus
With Focus
After Focus
I think the code would look something like this
Do While Me.sub_frm_robo2.SetFocus = True
If Me.sub_frm_robo2.Form!SENHA.SetFocus Then
Me.sub_frm_robo2.Form!SENHA.InputMask = ""
End If
Next
Can you help me?

Well, I did not have any help here but I managed to resolve after a few attempts and errors, just to code a code in the event of each column of the subformaluario to make it work. Good luck to everyone
Private Sub CNPJ_AF_GotFocus()
Me.Form!SENHA.InputMask = "Password"
End Sub
Private Sub SENHA_GotFocus()
Me.Form!SENHA.InputMask = ""
End Sub

Related

MS Access VBA, efficient way to enable a button only after all required textboxes contains valid data

My code is working, but I just want to know if there is a more efficient way to achieve the same effect.
I have this layout in a form:
In my effort to foolproof the record creation process, I would like to have the "Save and Clear fields" button enabled only after all but the 'Comment' textbox/combobox contains some valid data.
The text/combo boxes are called txtBatteryID, cmbModelNumber, cmbChemistryType, txtSpecVoltage, txtSpecCapacity.
My code is as follow
Private Sub EnableSaveBtnCheck()
'this checks if the required fields contains valid data, if so, enables the save button.
If Me.btnSaveAndCLear.Enabled = False Then
If IsNull(txtBatteryID) = False And IsNull(cmbModelNumber) = False And IsNull(cmbChemistryType) = False And IsNull(txtSpecVoltage) = False And IsNull(txtSpecCapacity) = False Then
Me.btnSaveAndCLear.Enabled = True
End If
End If
End Sub
As you can see, I did the most straightforward way of using AND to combine all must-have conditions in an IF statement. This sub is called in After_Update() event of each text/combo box. Like this:
Private Sub cmbChemistryType_AfterUpdate()
Call EnableSaveBtnCheck
End Sub
My question, in the end, is: Is there a more efficient way to setup the condition "all text/combo box need to have something valid in them"? And is there a more elaborate way to check if the condition is met (something like a event on the form itself)?
Add the values of those 5 fields. If any of them is Null, the sum will be Null. So you only need call IsNull() once.
If IsNull(txtBatteryID + cmbModelNumber + cmbChemistryType + txtSpecVoltage + txtSpecCapacity) = False Then

How do you update a selected dropdown value using VBScript

I am trying to problematically change the dropdown selection on an InternetExplorer.Application then save that selection. The code I have so far is
dim myValue
myValue="3"
for j = 0 to obj.Options.length - 1
if(obj.Options(j).Value = myValue) then
obj.Options(j).selected = true
exit for
end if
next
This works on the current pages dropdown list, however when I click save, the value "3" isn't saved and it reverts back to its original value when I reload the page.
Another thing to mention is that when I manually click the dropdown and select a value then save, it does update to the new value when I reload the page. I have tried the obj.click function on it but I do not believe a programmatic mouse click works like a actual mouse click with the action listener.
My guess would be something to do with the databinding between the new value selection and the action listener for the page. I am fairly new to vbscript and have tried all sorts of different things.
Any help would be very much appreciated. Thank You!
Supposing you have the obj object set properly, e.g. something like
set obj = ie.document.getElementById("my_dropdown") then you should ensure that only one option is selected:
for j = 0 to obj.Options.length - 1
if (obj.Options(j).Value = myValue) then
obj.Options(j).selected = true ''' do not exit for
else
obj.Options(j).selected = false
end if
next
or
For Each opt In obj.Options
If opt.Value = myValue Then
opt.Selected = True
Else
opt.Selected = False
End If
Next
Caution: above code snippet could result to (undesired?) case that no option remains selected!

VBA code that changes background color of form conditionally by answer

I want to use VBA (or some other solution) to conditionally change the background color of a form based off what number users enter in a numeric field. Basically, after they enter their answer to the Starter question, if they entered 1 then I want the form background to change to a specific shade of blue, and if they entered 2 then I want the form background to change to a specific shade of green. I saw a code that looks like it would be very similar to my need in another question on here, but I couldn't figure out how to make the code work, and was having trouble figuring out exactly how/where to put each module.
Some information:
The field I want it to be based off of is numeric, called Starter, and through data validation users are limited to entering 1, 2, 9, or leaving it blank. I only want the color to change if it's entered as 1 or 2.
I'm using Access 2010
the form has neither header nor footer
the code I was attempting to use and made some alterations to is the following:
Private Sub Form_AfterUpdate()
blue_yes = "15325906"
green_no = "13888226"
Dim colorThis As String
booWhatever = Me.Starter ''Use of the variable can prevent problems
If booWhatever = 1 Then
colorThis = "blue_yes"
End If
If booWhatever = 2 Then
colorThis = "green_no"
End If
subFrm.Form.Section(acDetail).BackColor = colorThis
subFrm.Form.Repaint
End Sub
I've also managed, off a very different piece of code, to sort of do what I want, but the way it's working it seems to change the status of all forms, not just the one I'm currently working with, which is the goal. So for example if I enter 2 to starter, it changes the background color of every single record's form.
Private Sub Starter_AfterUpdate()
If Me.Starter = "1" Then Me.Detail.BackColor = vbBlue
If Me.Starter = "2" Then Me.Detail.BackColor = vbGreen
End Sub
EDIT:
Welp, embarrassingly I found the solution. It's not a very neat one, but it works.
Private Sub Form_Current()
Dim Presence As String
Presence = Nz(Me.Starter.Value, 9)
Select Case Presence
Case "1"
Me.Detail.BackColor = 15325906
Case "2"
Me.Detail.BackColor = 13888226
Case Else
Me.Detail.BackColor = vbWhite
End Select
End Sub
Private Sub Starter_AfterUpdate()
Dim Presence As String
Presence = Nz(Me.Starter.Value, 9)
Select Case Presence
Case "1"
Me.Detail.BackColor = 15325906
Case "2"
Me.Detail.BackColor = 13888226
Case Else
Me.Detail.BackColor = vbWhite
End Select
End Sub
I know it is a really old question (probably you have already solved it in a better way) but I will give it a try anyways.
Try the following:
Private Sub Text0_Change()
Select Case Me.Text0.Text
Case ""
Case "1"
Me.Detail.BackColor = 15325906
Case "2"
Me.Detail.BackColor = 13888226
Case Else
Me.Detail.BackColor = vbWhite
End Select
End Sub
EDIT:
I tried that and it works I think now as it supposed to do.
When you change the text on the text box triggers this event every time, runs the Sub, checks it's own text and changes the color of the form as described.
The change is that I changed the property of the field it checks. From Value to Text. We want when the event triggers to check the current text because the Value property updates when you "finish" with the textbox (after you press enter or the focus on the control is lost) and we want the change to happen the same moment we press the key changing the value and not later.
The second change and the reason we got strange patterns before is that I have added one more Case when the text is "" to do nothing on that change (empty case). Without that case when we used delete or backspace to remove the text and left the textbox empty ("") then the case else was True and it changed the background color.
I hope this is the correct answer now. Please let me know!

ms access vba code I can disable a field based on content of another field, but my code to re-enable is not working

I have a simple form where I have a field InstrumentTypes_ID and a field DGLenders_ID.
When InstrumentTypes_ID equals "Mortgage" or "Modification", I want DGLenders_ID to stay enabled, but any other value should make DGLenders_ID become disabled.
I have the following code in an AfterUpdate for the InstrumentTypes_ID textBox, which to me seems would be sufficient to get this effect (However, when testing, I can type in "Warranty Deed" (or anything that's not "Mortgage" or "Modification"), which disables the DGLenders_ID field, but if I go back and update it with "Mortgage" or "Modification", it won't re-enable that field):
Private Sub InstrumentTypes_ID_AfterUpdate()
Me.DGLenders_ID.Enabled = True
If Me.InstrumentTypes_ID = "Mortgage" Then
Me.DGLenders_ID.Enabled = True
ElseIf Me.InstrumentTypes_ID = "Modification" Then
Me.DGLenders_ID.Enabled = True
Else
Me.DGLenders_ID.Enabled = False
End If
End Sub
You should try to put your code in a subroutine and call it on the current event of the form and in the lost focus event of the field.

MS Access vba programmatically accessing and setting label properties

Is it possible to access labels programmatically with VBA. I'd like to create a number of labels using a for loop such as that shown below which would set all labels named "Label1" to "Label20" to visible
for a_counter = 1 to 20
Me.Label(a_counter).Visible = True
next a_counter
Is something such as the above possible?
You can refer to each of those label controls, "Label1" thru "Label20", by name from the form's Controls collection.
For a_counter = 1 To 20
Me.Controls("Label" & a_counter).Visible = True
Next a_counter
labels are a specific kind of controls in access forms. You should be able to write down some code like this:
function listLabels()
dim m_ctl as control
for each m_ctl in screen.activeForm.controls
if m_ctl.type = .... 'please check the control types available!
debug.print m_ctl.name
end if
next m_ctl
end function
Be careful. I am not even sure of the control's properties (.type, .name) but you will easily find them in the help. Look for 'control' object.