Sort Form fields in sequence - vba

thanks in advance for your help.
I have a form with spreadsheet view that has 3 fields I want to use to sort: Customer, amount and currency.
When I do the sorting by hand, I sort the amount ascending, the currency A-Z and the customer A-Z following that specific order (the result of sorting fields one by one is a table structure I like because it is easy to read). I do this by clicking the field's header and activating the sort, one field at a time.
I tried to replicate this sequence by adding a macro to the form that runs when the form loads, however it seems like the sort is manipulating the 3 fields at the same time. The result does not look like the one I get when I manually sort one field at a time.
Private Sub Form_Load()
Me.OrderBy = ""
DoCmd.SetOrderBy "Amount DESC, Currency ASC, Customer ASC"
End Sub
Is there any way to set fields sorts one after the other?
Thanks.

Try this:
Private Sub Form_Load()
Me.OrderBy = "Amount DESC, Currency ASC, Customer ASC"
Me.OrderByOn = True
End Sub

Related

How can i restrict the textbox value to stay same in the access database

My goal is to create an access database form with ordering multiple item at a time. But for this phase my end goal is to find the total cost by multiplying price per unit*quantity. Through my working out i got stucked by the problem of on change event. Basically, whenever i change a the id in the combo box it changes in all the textbox that i programmed.
Here is the scenario:
In this case the price per unit stays 1000 for all of the stock ID, whereas, when i have stock id of 2 i want the price to be 0 and so on.
This is how i programmed the get price per unit value
Private Sub StockIDCombo_Change()
Me.PricePerUnit.Value = Me.StockIDCombo.Column(3)
End Sub
Note that the form is continuous.
Have an additional field in the table to hold the calculation, and bind your textbox to this field.
Is your PricePerUnit control unbound? It should be bounded to some table field as other fields (StockID, Quantity).

automatically fill fields using a drop-down list

I have a combo box which is linked with a table (products) and i need to fill fields automatically when the user select a value.
Datas in the fields are also from the table products.
Thanks
If your combobox row source is
SELECT ProdID, ProdName, ProdColor FROM Products
and column widths 0;3;0 (i.e. only ProdName is visible), you can use the AfterUpdate event to copy data from the combobox columns to other controls.
Private Sub cboProduct_AfterUpdate()
Me!txtID = Me!cboProduct.Column(0)
Me!txtColor = Me!cboProduct.Column(2)
End Sub
Easiest way will be just to create yourself a myComboBox_SelectionChanged() event. Then within your method assign the values where you want them.
Private Sub myComboBox_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles myComboBox.SelectionChanged
// Exact the selection here and put it in your table.
End Sub

Access 2010 VBA to Sort a Report with existing grouping

I have a Access 2010 report with two groups I set up in design. The first group has person data (name, dob, current address, customer points, date customer last placed a order). The second group has for the current person, all of their orders. The form that calls this report has a sort by option. They can sort by the name ascending or decending, the last date they ordered something asc/desc, and how many customer points they have asc/desc. All of these sort options should work on the data in the first/primary report group. Is there any way to call this report and set the orderby in VBA when the report already contains groups? I am passing in args from the form and trying on the load
Me.OrderBy = ArgIn(2)
Me.OrderByOn = True
I have also tried this in the open event. The report opens but ignores the sorting/order by.
Thanks
There are two routes you can go rather than passing arguments:
Create 3 exact same reports but with different sorting (within report design on group, you can sort the underlying items) and have a form trigger button with a drop down or list menu for user to select specific report ordering and then call the particular report.
In VBA, create a dynamic, user-defined querydef (Set qrydef = db.CreateQueryDef("[QUERY NAME]", strSQL) that is called by a form trigger button on a particular SQL statement out of three for each ORDER BY type (last name, date, points). Set the querydef to the report's recordsource.
The best dynamic way to do this is to use .SortOrder and .ControlSource:
Private Sub Report_Open(Cancel As Integer)
' Set SortOrder property to Ascending order (SortOrder = False)
Me.GroupLevel(0).SortOrder = blSortOrder
Me.GroupLevel(0).ControlSource = strSortBy
End Sub
.ControlSource is the field name of your reports table/query to be sorted. The filter hierarchy is addressed like Me.GroupLevel(0), Me.GroupLevel(1), Me.GroupLevel(2), etc..
Pass in this data via Me.OpenArgs.
Me.GroupLevel(0).SortOrder = Split(Me.OpenArgs, "|")(0)
Me.GroupLevel(0).ControlSource = Split(Me.OpenArgs, "|")(1)
Call the report like:
DoCmd.OpenReport "ReportName", acViewPreview, , , acWindowNormal, strSortBy & "|" & blSortOrder
...and make sure the sorted field is unique in its table, if you use grouping. (Notice that Access turns sorting into grouping when you add a header).

vb if condition on items and price from database

I actually have a Access database where i have a table called "tblfreight" and two column is there (items and Price)
And I want to auto show the Price when I choose Items.
There is two Textbox: "txtitems" and "txtprice"
I want to be like this....
If txtitems.text = select from tblfreight and column items Then
txtprice.text = select from tblfreight and column Price
Else txtprice.text=0 (Zero)
You need to query your DB first..
Select * from tblfreight - will get you your data you need.
Then you need to put this data somewhere. You can use a combobox for this. Once you have your combobox filled with your data you then can set the price into a label or textbox easily.
You can use the combobox selected index changed event...
Inside the changed event something like this would work...
textprice.text = Combobox.selecteditem.value
This will give you a great start, that's why this answer just gives you the steps and not the full code.

Count number of rows in a report using VB

I have a VB report which contains a list of locations. I have this list separated into areas and I need to count the total number of rows per area section. I have a groupHeader which contains my area field, then in the group I list the corresponding entries for that area. Then in the groupFooter I would like to give the total number of entries in the group. How can I do this? I have two fields in my groupFooter
groupCount
groupTotalAmount
-this sums up a value I have in each entry. Any guidance would be appreciated.
ewein,
You can get the total records in a group by setting some properties in the designer itself. The simplest example would be using the NorthWind database. You bind your report to the Customers table and want the report to group on countries where each group will contain the cities for that particular country.
From your question I think you are already aware on how to get groups in a report, so I will only talk about getting the record count for each Group. Place a textbox in the GroupFooter. Set its datafield property to "Country" (same as that of groupheader). Now you need to set the following properties for the textbox:
SummaryFunction:- Count
SummaryGroup:- GroupHeader1 (or the groupheader name you have in your report)
SummaryRunning:- Group
SummaryType:- SubTotal
You can also get the same results programmatically by using the following code. Please note that "TextBox1" is the textbox used to display the count and is placed in the GroupFooter section:
Private count As Integer = 0
Public Sub GroupHeader1_Format()
count = 0
End Sub
Public Sub Detail1_Format()
count += 1
End Sub
Public Sub GroupFooter1_Format()
TextBox1.Text = count.ToString()
End Sub
I hope this will help you. You may also want to check the ActiveReports blogs where you can find blogs on various areas of interest.