Access VBA OpenForm Grouping and Sorting - sorting

I have a form that is used for data entry. We have to go back through and add data to these records. Is there a way to pull up the form that groups the records by field "A" and sorts by field "B"? This would essentially order the forms A1-1, A1-2, etc, making adding data easier.
Right now I am using DoCmd.OpenForm to only display records with certain values in certain fields. Do I just need to modify this a bit?
Thanks for the help!
[Edit]
I would like this to load the form on button click so I have
Private Sub btnDataEntry_Click()
DoCmd.OpenForm "Data Sheet", acNormal, , , acFormEdit, , OpenArgs:="MapNumber"
End Sub
Then as suggested
Private Sub Form_Load()
If Not IsNull(Me.OpenArgs) Then
Main.OrderBy = Me.OpenArgs
Main.OrderByOn = True
End If
End Sub
This is not working for me. If possible I would also like it to group all map numbers together and then have all Item numbers ascending. So there could be 10 entries with map number 1 and item numbers 1-10.

OpenForm doesn't include an option to specify the sort order. However you could use its OpenArgs option to pass in sort information, then apply that during form load.
Private Sub Form_Load()
If Not IsNull(Me.OpenArgs) Then
Me.OrderBy = Me.OpenArgs
Me.OrderByOn = True
End If
End Sub
Then to open YourForm sorted by a field named id in ascending order ...
DoCmd.OpenForm "YourForm", OpenArgs:="id"
Include DESC for descending order ...
DoCmd.OpenForm "YourForm", OpenArgs:="id DESC"
Use this version of Form_Load to troubleshoot why the form opens without the sorting you expect.
Private Sub Form_Load()
MsgBox "Me.OpenArgs: " & Nz(Me.OpenArgs, "Null")
    If Not IsNull(Me.OpenArgs) Then
        Me.OrderBy = Me.OpenArgs
        Me.OrderByOn = True
    End If
MsgBox "Me.OrderBy : '" & Me.OrderBy & "'"
MsgBox "Me.OrderByOn: " & Me.OrderByOn
End Sub

Related

vb 6.0 delete row from listview1

I just joined the site, I apologize in advance for the wrong English, I used google translate,
I used listview1 in vb 6, there is no problem with adding and deleting by clicking
What I want to do is to remove the number I wrote in the txtsearch.Text box on the form from the list, not index, it will only remove what I wrote in the txtsearch.Text box.
it should be like in the picture
i tried this but it only deletes as index it doesn't delete the line i wrote
Private Sub Command2_Click()
If ListView1.ListItems.Count <= 0 Then MsgBox "Nothing to remove", vbInformation, "": Exit Sub
ListView1.SelectedItem = ListView1.ListItems(Val(txtsearch.Text))
If vbYes = MsgBox("Are you sure you want to delete the selected item?", vbQuestion + vbYesNo, "") Then
ListView1.ListItems.Remove (ListView1.SelectedItem.Index)
End If
End Sub
thank you for your help
To select a desired row via the SelectedItem property, the Set keyword must be used.
Alternatively, the Selected property for the desired row can be set to True in order to select that row.
Try this:
Private Sub Command2_Click()
If ListView1.ListItems.Count <= 0 Then
MsgBox "Nothing to remove", vbInformation, ""
Exit Sub
End If
Set ListView1.SelectedItem = ListView1.ListItems(Val(txtsearch.Text))
' Or the following will also work
'ListView1.ListItems(Val(txtsearch.Text)).Selected = True
If vbYes = MsgBox("Are you sure you want to delete the selected item?", vbQuestion + vbYesNo, "") Then
ListView1.ListItems.Remove (ListView1.SelectedItem.Index)
End If
End Sub

Linking a report to a subform

I have a main report (Projects Overview) which I am trying to create an OnClick event which will take me from the report to the field where that piece of information is entered on a form (LiveJobs).
My problem is that there is a subform (Estimate Items Subform) where the order items are entered. Then there is a subform on that (Production Subform) which is where the components that make up the 'Item' are entered. So a 'Desk' is ordered under 'Items' and then the components of the desk - drawer boxes, top, privacy panel are all entered on the Production Subform so they can all be tracked and monitored for production. As they are being produced there is time scheduled for each of these items in a time slot corresponding to a particular week.
In the report I want to be able to click on the time scheduled for any component and link back to the form and the corresponding week where it is scheduled and move hours around within the order form. My code will currently take me to the correct job but it wont get me to the correct 'layer' of the first subform and then to the correct layer of the component. Lets say for example the 3rd item in an order and then the 2 component of that Item.
Below is my code as it sits currently which only goes as far as trying to get to the correct item on the first subform. I figured if I could figure that out I could use the same logic to get to the correct component. This code results in a "Runtime Error '13' Type Mismatch"...I have been going round and round with this for days... Thanks in advance for any and all help.
Private Sub Estimated_hours_for_current_week_Click()
Dim strWhere As String
Dim DocName As String
DocName = "LiveJobs"
strWhere = "[Job Number]=" & "'" & Me![Job Number] & "'"
DoCmd.OpenForm DocName, acNormal, , strWhere
Forms![LiveJobs].[Estimate Items Subform].SetFocus
'find the Item in the item subform
Dim dbs As DAO.Database
Dim RstItem As DAO.Recordset
Dim strItemCriteria As Integer
Set dbs = CurrentDb
Set RstItem = dbs.OpenRecordset("Estimate Items Subform table", dbOpenDynaset)
strItemCriteria = "[Estimate Item subform table ID] = '" & Me.Estimate_Item_subform_table_ID & "'"
With RstItem
RstItem.MoveLast
DoEvents
RstItem.FindFirst strItemCriteria
Debug.Print (strItemCriteria)
If .NoMatch Then
MsgBox "No Match Found"
End If
End With
Set rs = Nothing
End Sub
I figured out the code. Here it is for reference.
Private Sub Estimated_hours_for_current_week_Click()
Dim frm1 As Form
Dim frm2 As Form
Dim rst1 As DAO.Recordset
Dim rst2 As DAO.Recordset
DoCmd.OpenForm "LiveJobs", _
WhereCondition:="[Job Number]=" & "'" & Me![Job Number] & "'"
Set frm1 = Forms!LiveJobs.Estimate_Items_Subform.Form
Set frm2 = Forms!LiveJobs.Estimate_Items_Subform!ProductionComponentSubform.Form
Set rst1 = frm1.Recordset.Clone
Set rst2 = frm2.Recordset.Clone
With rst1
.FindFirst "[Estimate Item subform table ID] =" & Me.Estimate_Item_subform_table_ID
If .NoMatch Then
MsgBox "Item not found"
Else
frm1.Bookmark = rst1.Bookmark
End If
End With
With rst2
.FindFirst "[Estimate details ID]=" & Me.Estimate_details_ID
If .NoMatch Then
MsgBox "project not found"
Else
frm2.Bookmark = rst2.Bookmark
Forms![LiveJobs].SetFocus
Forms![LiveJobs]![Estimate Items Subform].SetFocus
Forms![LiveJobs]![Estimate Items Subform]![ProductionComponentSubform].Form![Estimated
hours for current week].SetFocus
End If
End With
End Sub

Converting VBA code to google sheets script for auto sort

I am looking for assistance on a script for google sheets for an auto sort function that runs when I update the sheet with new information. The sheet name is "Summary All Events". I have data starting in Row 4, Columns A:J. I would like to auto sort any data from A4:J1000 by column 1 (A) in ascending order. Can anyone help me?
Here is the script I am running in VBA for Excel. But now I need to convert it into a script for Google Sheets.
Public Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Column = 1 Then
Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Range("A4:J" & lastRow).Sort Key1:=Range("A4:A" & lastRow), Order1:=xlAscending, Header:=xlNo
End If
End Sub
Try the following code:
function onEdit(e) {
var sheet = e.source.getActiveSheet();
if (sheet.getName() == 'Sheet1') {
var r = e.range;
if (r.columnStart == 1 && r.rowStart >= 4)
sheet.getRange('A4:J').sort({column:1,ascending:true});
}
};
Now, whenever any user will edit any cell in range "A4:A", the above code will auto sort the range A4:J by column A in ascending order.

Toggle sort order with a command button

I'm using
DoCmd.SetOrderBy
for sorting a form by a specific field. But right now it sorts ascending or descending if I use DESC keyword. How can I do a check to see what sorting is active and do the opposite? If that field is ordered ascending and I click the button, the order become descending and vice versa.
You can get the current sort order by doing in the form code
debug.print me.orderby
It will come out something like
[FORM].[COLUMN] Desc
So then do an if statement
if me.orderby = [FORM].[COLUMN] Desc then
docmd.setorderby "[COLUMN] ASC"
else
docmd.setorderby "[COLUMN] Desc"
end if
Try that out. Changing the FORM and COLUMN to match yours of course
As #Sam suggested, check the value of the Me.OrderBy property to decide whether you want ascending or descending for the new sort order.
The following code sample assumes you want an ascending sort if there is no current sort (i.e. Me.OrderBy is an empty string).
Otherwise check whether Me.OrderBy is Like "* DESC". Note, if the current sort order is ascending, don't assume ASC will be present in Me.OrderBy. And don't assume the column name piece of Me.OrderBy will be present as [FORM].[COLUMN].
Dim strOrderBy As String
Dim strDirection As String
strOrderBy = Me.OrderBy
If Len(strOrderBy) = 0 Then
strDirection = "ASC"
Else
If strOrderBy Like "* DESC" Then
strDirection = "ASC"
Else
strDirection = "DESC"
End If ' Like
End If ' Len(strOrderBy)
DoCmd.SetOrderBy "[YourColumnName] " & strDirection

Show/Hide ColumnGroups with a mix of hidden and displayed columns

I've got a RadGrid with programmatically generated columns, including a variable number of column groups. Each of the column groups contains four columns, two that are displayed and two that are not.
Above the RadGrid I have a series of checkboxes, one for each column group. The goal is to have the user check or uncheck the boxes depending on what columns they wish to see. Currently I am setting the columns' Display properties when a checkbox's checked value is changed by using radgrid.Columns.FindByUniqueName("columnName").Display. However the column group itself cannot be accessed this way. As it is, the column groups disappear when all their child columns disappear, but they do not reappear when their child columns are displayed, causing the headers to become misaligned.
Relevant code:
'Defining the grid
Dim radgrid As RadGrid
radgrid = New RadGrid()
Dim i as Integer = 0
For Each r As DataRow In subTable.Rows
Dim colGroup As GridColumnGroup = New GridGroupColumn()
radgrid.MasterTableView.ColumnGroups.Add(colGroup)
colGroup.HeaderText = r.Item("Name")
colGroup.Name = "colGroup" & i
Dim colID As GridBoundColumn = New GridBoundColumn()
radgrid.MasterTableView.Columns.Add(colID)
colID.ColumnGroupName = "colGroup" & i
colID.DataField = "id" & i
colID.HeaderText = "ID"
colID.UniqueName = "id" & i
colID.Display = False
Dim colScore As GridBoundColumn = New GridBoundColumn()
radgrid.MasterTableView.Columns.Add(colScore)
colScore.ColumnGroupName = "colGroup" & i
colScore.DataField = "score" & i
colScore.HeaderText = "Score"
colScore.UniqueName = "score" & i
i += 1
Next
'Checkbox CheckedChanged sub
If selectedButton.Checked = True Then
radgrid.Columns.FindByUniqueName("colScore" & selectedButton.Value.ToString()).Display = True
Else
radgrid.Columns.FindByUniqueName("colScore" & selectedButton.Value.ToString()).Display = False
End If
Ideally I'd like to do something like radgrid.MasterTableView.ColumnGroups.FindGroupByName("colGroup" & selectedButton.Value.ToString()).Display = False but there is no Display property for that and the Visible property is ReadOnly. I've also tried re-setting the ColumnGroupName of the columns after setting them to display, but that also does not work.
I'm guessing the fact that I have hidden columns is what's making this more complicated than it should be, but is there a way to get this to work with hidden columns?
The solution actually turned up when I was working on a somewhat unrelated issue.
Calling radgrid.Rebind() after changing the value of the Display property causes the group column headers to disappear and reappear as desired.
Protected Sub button_CheckChanged()
If selectedButton.Checked = True Then
radgrid.Columns.FindByUniqueName("colScore" & selectedButton.Value.ToString()).Display = True
Else
radgrid.Columns.FindByUniqueName("colScore" & selectedButton.Value.ToString()).Display = False
End If
radgrid.Rebind()
End Sub

Resources