VBA: how to verify a bound textbox in a subform - vba

I'm using ms-access, and I have a form that lists ingredients in the subform, along with the percentage of the ingredient in the product (as decimals eg. .4, .6 with 1 being 100%). I have a bound textbox that shows the total sum of the "Percentage" column (=Sum[Percentage]) the name of this textbox is Sum1.
What I want is for the main form to not allow you to continue until the bound textbox equals to 1. I found the subform event onExit (accessed in the main form) to be sufficient. As in, when you're in the main form and click in the subform, you shouldn't be able to click outside the subform until that textbox equals the sum of 1.
I can't figure out the vba code for it though. If I put the code like this:
Option Compare Database
Private Sub I_O_Subform_Exit(Cancel As Integer)
If Sum1 <> 1 Then
Cancel = True
MsgBox "Total Percentage must equal to 1"
End If
End Sub
-then once I go into the subform, I can't exit at all, even if the textbox does equal to 1.
I've tried doing it like this:
Private Sub I_O_Subform_Exit(Cancel As Integer)
If Forms!Outputs.I_O_Subform.Sum1 <> 1 Then
Cancel = True
MsgBox "Total Percentage must equal to 1"
End If
End Sub
but I just get Error "438": Object doesn't support this body or method.
and if I do
Private Sub I_O_Subform_Exit(Cancel As Integer)
If I_O_Subform.Sum1 <> 1 Then
Cancel = True
MsgBox "Total Percentage must equal to 1"
End If
End Sub
then it also doesn't understand.
can anyone help?
thanks

Try this reference:
If Me!I_O_Subform.Form!Sum1.Value <> 1 Then
where I_O_Subform must be the name of the subform control (which may differ from the name of the subform).

Related

How to make textbox inputs numbers with 2 decimal places and then check their sum?

struggling with something in VBA and can't find a clear answer online. Quite new to this. Basically, for the user form below (linked image due to my level), I would like to make all the textbox inputs be numbers (2 decimal places) and then when you click "Next" it checks that all the values sum to 100. If not, error message appears. I've tried lots of different ways of doing this and can't get it to work. I would like for all textbox inputs to be numbers (2dp), and then all the numbers to just add and then check this sum. For each textbox, I've done the following Sub Routine (variable name changes each time for name of textbox):
Private Sub BinQnt_AfterUpdate()
On Error Resume Next
Dim BinQnt As Single
BinQnt = Format(BinQnt, "percent")
End Sub
For the Next button I have done the following:
Private Sub MaterialsData_Next_Click()
'Check mix design = 100%.
'If = 100%, input data to database.
tot = BinQnt + FillQnt + FineQnt + CoarQnt + RAPQnt + CRQnt 'Names of each text box.
If tot = 100 Then
'Code here for inserting values into database. Omitted to save space and confusion.
'Go to next page.
BaseUserForm.MultiPage1.Value = 2
'If doesn't = 100%, then show error MsgBox
Else
MsgBox "Error, please check mixture design sums to 100%. Currently at " & tot & "."
End If
Screenshot of userform.

How to alphabetize fields in Unhide Columns dialog box in Access VBA?

I have a ton of columns in this table and I want them to be alphabetized so they are easier to find.
I remember seeing how to do this in a Youtube video but I can't find it for the life of me. Below is an example of the code I am using in multiple datasheet type forms. I'm not sure what needs to be added in to make these field lists alphabetize
Private Sub showHideColumns_Click()
frmInventoryListSubform.SetFocus
DoCmd.RunCommand acCmdUnhideColumns
End Sub
The "columns", the controls, have both a name and, optionally, a caption.
So, in your form, you can run this code to list these:
Private Sub ListColumns()
Dim Control As Control
Dim Index As Long
For Index = 0 To Me.Controls.Count - 1
Set Control = Me.Controls(Index)
If Control.ControlType <> acLabel Then
Debug.Print Index, Control.Name, Control.Properties("DatasheetCaption").Value
End If
Next
End Sub
May return a result like this:
0 StipendNo Student Number
2 PayNo Pay Number
4 PayDate
6 PayAmount

Want a list box to show 1-10, but what is shown in the drop-down is dependent on another list item

I want to create a Combo box (Combo Box B) on an Access form containing numbers 1-10. In other words, the drop-down shows numbers 1-10 sequentially.
However, what is shown in the drop-down is dependent on Combo box A.
If x shows in Combo Box A, items 1-10 should show in Combo Box B.
If y shows in Combo Box A, numbers 1-5 should only show in Combo Box B, or at minimum prevent someone from selecting 6 or larger.
If z shows in Combo Box A, nothing should be selectable in Combo Box B.
My coding skills are rusty as I've not done much in over 10 years. Is this something easily achievable in Access, or will I need some VBA to assist?
Assuming that your question isn't just a contrived example, and that you really want to display the integers 1-10 and 1-5 in your combobox, then in it's simplest form, you could use the following function evaluated on the After Update event of your combobox 'A' and on the On Load event of your form:
Function UpdateComboRowSource(cmbCom As ComboBox, ByVal strVal As String)
cmbCom.RowSourceType = "Value List"
Select Case LCase(strVal)
Case "x": cmbCom.RowSource = "1;2;3;4;5;6;7;8;9;10"
Case "y": cmbCom.RowSource = "1;2;3;4;5"
Case Else: cmbCom.RowSource = ""
End Select
End Function
Private Sub Form_Load()
UpdateComboRowSource ComboB, ComboA.Value
End Sub
Private Sub ComboA_AfterUpdate()
UpdateComboRowSource ComboB, ComboA.Value
End Sub

How can I sum value in 8 textboxes to the a single textbox?

I can't find a way to do this. I think it's a very basic question, but i'm just new with excel VBA and can't find the right formula.
Recently i understand that to do the sum, i have to change each of the textboxes into integer first, then i can add it to a single textbox, so i'm converting each value into integer while i sum them one by one to a text box.
I have tried this code.
Private Sub txtTotal_Change()
txtTotal.Value = CInt(txtKas) + CInt(txtInvestasi) + CInt(txtDanaTerbatas) + CInt(txtBruto) + ...
End Sub
How can i sum this multiple textboxes (8 textboxes) into a single textbox?
You need to add Change Events on your text boxes that are user changeable.
Lets say I have below UserForm and TextBox1 to TextBox3:
TextBox3 will be the Sum of TextBox1 and TextBox2.
Right click UserForm1 under Project and select View Code, then put in the TextBoxesSum Sub (I use Double type for accepting decimals):
Private Sub TextBoxesSum()
Dim Total As Double
Total = 0
If Len(TextBox1.Value) > 0 Then Total = Total + CDbl(TextBox1.Value)
If Len(TextBox2.Value) > 0 Then Total = Total + CDbl(TextBox2.Value)
' Add more for the rest of your text boxes
TextBox3.Value = Total
End Sub
To be safe and smart, you should also put in key checking from user input. Due to the amount of text boxes you have it is better to have a Function to handle it:
Private Function NumericOnly(ByVal KeyAscii As MSForms.ReturnInteger) As MSForms.ReturnInteger
Dim Key As MSForms.ReturnInteger
Select Case KeyAscii
Case 46, 48 To 57 ' Accept only decimal "." and numbers [0-9]
Set Key = KeyAscii
Case Else
KeyAscii = 0 ' Minor bug earlier
Set Key = KeyAscii
End Select
Set NumericOnly = Key
End Function
Now back to the UserForm1 object, right click TextBox1 and View Code, put in:
Private Sub TextBox1_Change()
TextBoxesSum
End Sub
Also do checking on the KeyPress event:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
KeyAscii = NumericOnly(KeyAscii)
End Sub
Now copy above 2 set of TextBox Sub and paste below, then replace the string before underscore to match the name of the text boxes you have.
Sample output:
You (probably) do not want to sum the values from your textboxes when someone tries to change the value in the totals box - you want to sum them when a value in any of the other eight boxes change.
The simplest way to do this is probably to create a simple macro to sum the values of the textboxes, and then call that from the change-events of the 8 which the user may change.
The macro to sum the textboxes and put them into the totals box
Private Sub sum_boxes()
txtTotal = CLng(txtKas.Value) + CLng(Investasi.Value) + CLng(DanaTerbatas.Value)
End Sub
What a change event will look like
Private Sub txtKas_Change()
Call sum_boxes
End Sub
You need to make one change-event for each of the eight boxes, as mentioned previously.
On a completely different note, using a textbox to store the total may be a bad idea, as you don't want your users to change what's in it. One option is to lock the textbox, as PatricK suggests, another is to use a different object to hold the number, e.g. a label, and just change its caption to be similar to whatever your total is at the moment.

How to count number of records in a section in ms-access report?

On my report I have sections that are summarized at the bottom. Some of the sections have only one record though. In that case, I don't want to show the Total. So, basically, I am looking for something like my code below (which doesn't work because RecordCount is not Sections property). Any solutions?
Private Sub prv_subtotal_Format(Cancel As Integer, FormatCount As Integer)
If Me.Section("prv_subtotal").RecordCount = 1 Then Cancel = True
End Sub
Put a hidden textbox in the detail section and set the controlsource to =1 and RunningSum to OverGroup.
Then change your code to
Cancel = (me!hiddentextbox = 1)