How to make global changes to Access forms? - ms-access

I've inherited a database which has a tabbed form, which in turn has tabbed subforms. Many of the subforms have a label that has to be corrected. If this were a Word document or Excel workbook, I'd use Find-and-Replace-All. You can't do that across controls on forms, can you?
Is there a way to do so using VBA? I think it would look something like:
Set aForm = Me
For each aPage in aForm.Pages
For each aControl in aPage.Controls
if aControl.type = "Label" then
if aControl.Value = "OldText" then aControl.Value = "NewText"
end if
Next aControl
Next aPage
EDIT -------------
I tried this code:
For Each aForm In Application.CurrentProject.AllForms
For Each aPage In aForm.Pages '<---- Error here
For Each aControl In aPage.Controls
If aControl.Type = "Label" Then
If aControl.Value = "OldText" Then aControl.Value = "NewText"
End If
Next aControl
Next aPage
Next aForm
But I get an error where I put that arrow. It says "Object doesn't support this property or method", which means it doesn't like "aForm.Pages". What's the right way to cycle through tabbed pages?

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

Access VBA Set Focus in Column

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

How to perform font changes to a report using an option box

I am having issues trying to use an option box to change a font on a series of controls. Basically, I have a report (named Q_tblProject52Week) embedded in a form. I have embedded an option box within the form (called "cornice33) which aims to change the font on the two controls (testo107 and testo108) embedded in the report.
At the moment I am trying the following with no success:
If Cornice33 = 1 Then
testo107.FontName = "calibri"
testo108.FontName = "times"
ElseIf Cornice33 = 2 Then
testo107.FontName = "times"
testo108.FontName = "calibri"
End If
I am getting a missing object message (it is not recognising the controls testo107 and testo108). Also important to note, the report is embedded in a folder control.
You need to tell access that you refer to a control on the report which is a child of your form.
If Cornice33 = 1 Then
Me.Q_tblProject52Week.testo107.FontName = "calibri"
Me.Q_tblProject52Week.testo108.FontName = "times"
ElseIf Cornice33 = 2 Then
Me.Q_tblProject52Week.testo107.FontName = "times"
Me.Q_tblProject52Week.testo108.FontName = "calibri"
End If
hope this helps

Send message to user if they leave all fields blank when adding data to table; and don't add the data

So, I have a table, 2 forms, a sub form, 2 buttons, and a bunch of text boxes. With these I made a button that pops up an "add field" where you type information into text boxes then click add, and it adds it to the table which back on the other subform shows that data; I have that working. I am not sure how to make it not add the data if you leave ALL fields blank though (note; I only want it to be ALL fields, it's okay if they fill out one field and the rest are null). If all fields are null, it sends out a msgbox to the user saying please fill in data into the text boxes.
Another thing I am looking for for a future part of this database is required fields. Where lets say there are 10 text boxes, and before you can add the data into the table through the form you MUST fill out 6 out of 10 of the text boxes (marked with a * which ones are needed) and if you don't, give them an error saying please fill out [text boxes that weren't filled out] and try again. If all of the 6 required fields were filled out, then it can save it to the table even if the other 4 are null or not.
This is the code I have inside of the add button:
Private Sub CustomerAddBtn_Click()
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("Select * from CustomersT")
rec.AddNew
rec("CustomerName") = Me.CustomerAddSupplierNameTxt
rec("Address") = Me.CustomerAddAddressTxt
rec("City") = Me.CustomerAddCityTxt
rec("ProvinceState") = Me.CustomerAddProvinceStateTxt
rec("PostalZip") = Me.CustomerAddPostalZipTxt
rec("Phone") = Me.CustomerAddPhoneTxt
rec("Fax") = Me.CustomerAddFaxTxt
rec("CustomerSince") = Me.CustomerAddCustomerSinceTxt
rec("Email") = Me.CustomerAddEmailTxt
rec("Notes") = Me.CustomerAddNotesTxT
rec.Update
Set rec = Nothing
Set db = Nothing
'Send message to user saying it was saved, so they know
Dim intReply As Integer
intReply = MsgBox("Customer has been successfully saved to the database!", vbOKOnly, "Success!")
End Sub
Thanks in advance.
You can check if a Text Box has a value in it by using IsNull(Me.TextBoxName). So, if you want to test if all of the text boxes are empty then you can do something like this
If IsNull(Me.CustomerAddSupplierNameTxt) _
And IsNull(Me.CustomerAddAddressTxt) Then
MsgBox "Please don't try to enter an empty record."
Else
MsgBox "(user filled in at least one field)", vbInformation, "Debug Message"
' your existing database code here
End If
...and just expand the initial If statement to include all of the controls you want to check.
Similarly, for required fields, you can check the value of the controls corresponding to those required fields and display a similar message if any of them IsNull().

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.