How to alphabetize fields in Unhide Columns dialog box in Access VBA? - 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

Related

VBA - Select Top of Matrix

I'm using a "worksheet_selectionChange" event to fire a macro whenever a cells within certain ranges are selected.
Public Sub Worksheet_SelectionChange(ByVal Target As Range)
Select Case Target.Cells.Offset(-???,0).Value
Case "LABEL_1"
Tenor = "2W"
Call MyLameMacro()
Case....
End Select
End Sub
Those ranges look like little matricies:
If the user selects any of the cells underneath label, I want VBA to lookup whatever the label is at the top. Using offset would work if I knew exactly how many rows up to Label, but its not constant....
Is there another way to go about that?
Thanks - KC
Barring further information about the layout ... you can use formatting to build your own search algorithm. This will slow down if it has to go through thousands of lines (find another way if your data set is that large).
You'll have to replace "labelColor" and "notLabel" with the background color of the label rows. This is assuming the above picture is accurate, and "Label" is highlighted. To find the value to replace them with, simply select the cell in question, then type "debug.print selection.interior.color" into the immediate window in VBA.
By placing the label value and the origin address in parentheses after your lame macro, you can preserve those values in the macro.
I did not test this. In order to do so I would have to guess at the setup of your workbook ... but an approximation of this code should work for you.
Public Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i As Integer
i = 0
searchLoop:
If i > 100 Then
MsgBox ("Infinite loop. Click OK to stop")
Exit Sub
End If
If Target.Offset([-i], 0).Interior.Color = labelColor Then Call MyLameMacro(Target.Offset([-i], 0), Target.address)
If Target.Offset([-i], 0).Interior.Color = notLabel Then
If Target.Offset([-i], 0).Value = "Value" Then Call MyLameMacro(Target.Offset([-i], [-1]).Value, Target.address)
i = i + 1
GoTo searchLoop
End If
End Sub

Single selection CheckedListBox control

I have a CheckedListBox control.I want to limit it's selection property to one means now a user can select more than one item in the control, need to limit this property to single selection only.
For example, Let's CHKListsolutions has following items
Google
Bing
Yahoo
Normally we can select 3 of them because of an obvious reason.
How to make this CHKListsolutions to select only one item in the list.
for example, I select Google and for some reason I want chnage the selection so I will select Yahoo then my last selection should unchecked and new one should be checked
I have checked in the resource for a property but in vain.
Any help would be very much appreciated
Private Sub CHKListsolutions_MouseClick(sender As Object, e As MouseEventArgs) Handles CHKListsolutions.MouseClick
Dim idx, sidx As Integer
sidx = CHKListsolutions.SelectedIndex
For idx = 0 To CHKListsolutions.Items.Count - 1
If idx <> sidx Then
CHKListsolutions.SetItemChecked(idx, False)
Else
CHKListsolutions.SetItemChecked(sidx, True)
End If
Next
End Sub
In MouseClick event you'll get the currently selected index of the item in the control(sidx) use this sidx to loop through number of items in the control and uncheck checked item that is not equal to the current index using SetItemChecked method
Use Radio Button instead of Checkedlistbox

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)

DevExpress XtraGrid GroupRow, CheckEdit Interactions Possibilities

Currently, I am displaying an XtraGrid that contains Group Rows. I have a "Select All" DevExpress.XtraEditors.CheckEdit control (which is different from this elusive "Select All" check box control I am reading about in the documentation). It is different for a reason: I want the check box to do something other than "Select All" (which comes in only three different varieties according to the DevExpress Docs.).
I want a user to be able to do one of two things with the CheckEdit control. [1] If no Group Rows are expanded, I want to select all Group Rows. [2] If one or more Group Rows are expanded, I only want to select the expanded rows.
Presently, I am able to manipulate the controls to do only one of the two things (see code). My question is twofold: is this possible; and, if so, how would I go about it?
Here is my code that does the second of the two 'things' described above:
'If the CheckEdit control is checked:
xtraGrid.SelectAll()
Dim rowHandles() As Int32 = xtraGrid.GetSelectedRows()
If rowHandles.Count > 0 Then
For Each RowHandle As Int32 In rowHandles
If xtraGrid.IsGroupRow(RowHandle) Then
xtraGrid.UnselectRow(RowHandle)
End If
Next
End If
As you can see, all this really is just a work around. There is also presumably more overhead than needed in my calling .GetSelectedRows(). I am just attempting to sort through the row types in order to keep the row selected or .UnselectRow()
You can use GridView.GetRowExpanded method to check whether a specific group row is expanded. For iterating through visible rows you can use GridView.RowCount property. To get row level just use GridView.GetRowLevel method. Also you need to check wether a row is new item row or is filter row by using GridView.IsNewItemRow method and GridView.IsFilterRow method. For all of this methods you need to get Row handle by using GridView.GetVisibleRowHandle method. When you are working with selection is better to use GridView.BeginSelection method and GridView.EndSelection method outside of your code.
UPDATE: If you want to select hidden rows within a collapsed group then you can use GridView.GetChildRowCount method and GridView.GetChildRowHandle method to get all hidden rows.
Here is some sample code that is performing your two things together:
Private Sub SomeSub
If xtraGrid.GroupCount = 0 Then
xtraGrid.SelectAll()
Exit Sub
End If
xtraGrid.BeginSelection()
xtraGrid.ClearSelection()
Dim isExpanded As Boolean = False
For rowVisibleIndex = 0 To xtraGrid.RowCount - 1
Dim rowHandle As Integer = xtraGrid.GetVisibleRowHandle(rowVisibleIndex)
If xtraGrid.IsNewItemRow(rowHandle) Then
Continue For
End If
Dim level As Integer = xtraGrid.GetRowLevel(rowHandle)
If level = 0 Then
If Not isExpanded Then
isExpanded = xtraGrid.GetRowExpanded(rowHandle)
If isExpanded Then
xtraGrid.ClearSelection()
Else
xtraGrid.SelectRow(rowHandle)
End If
End If
Else
xtraGrid.SelectRow(rowHandle)
'Update: select hidden rows
If xtraGrid.IsGroupRow(rowHandle) And Not xtraGrid.GetRowExpanded(rowHandle) Then
SelectRowHierarchy(rowHandle)
End If
End If
Next
xtraGrid.EndSelection()
End Sub
Private Sub SelectRowHierarchy(rowHandle As Integer)
Dim childCount As Integer = xtraGrid.GetChildRowCount(rowHandle)
For childIndex As Integer = 0 To childCount - 1
Dim childRowHandle As Integer = xtraGrid.GetChildRowHandle(rowHandle, childIndex)
xtraGrid.SelectRow(childRowHandle)
If xtraGrid.IsGroupRow(childRowHandle) Then
SelectRowHierarchy(childRowHandle)
End If
Next
End Sub