Sequence Number in ms access - ms-access

I want to create sequence number in my order form that after save button it generate a Order No in Order No field of table, as I m not much knowledge in VBA but got from internet some codes which i try to implement but its not working
The code is given below please see reply if there is any edit or where i m mistaked
Table:
ID | date | party Name | Order No| Item | qty | Rate | Amount |
Private Sub save_Click()
If Me.orderno = Null Then
Me.orderno = Nz(DMax([Order No], Order), 0) + 1
End If
End Sub

If the name of the table is 'Order', the following should work:
If IsNull(Me.orderno) Then
Me.orderno = Nz(DMax("[Order No]", "Order"), 0) + 1
End If
A few other comments:
Date is a reserved word and should not be used as a field name, so best to change it to avoid problems. http://office.microsoft.com/en-us/access-help/access-2007-reserved-words-and-symbols-HA010030643.aspx
It is also poor practice to use spaces in field names.
The problems here were quite obvious, however, for future reference, you should state what error you are receiving and which line it occurs on. 'Not working' does not provide any clues.
Add the line
DoCmd.RunCommand acCmdSaveRecord
at the end, to insure that the record is saved.
Make sure that the code is actually running and that orderno is bound to the field orderno.
If it still doesn't appear to work, step through he code to see what is happening.
As for the comment about autonumber, and autonumber is fine if you don't care about gaps in the numbering sequence, which will eventually occur using an autonumber.

Related

MS Access order table by order in which records were entered

I have a table in Access that I use as a progress tracker/to do list, with one field containing the date (short text) and what I did that day (long text). An example would be like this
date | progress
----------------
6/20 | did item1
| tomorrow do item2 and item3
6/21 | long text I continue in the next line for visibilty
| continued
| to do tomorrow
6/22 | item6 completed
etc. I enter these things manually. the past 3 weeks or so that I have been updating this table, it opens in the same order every time -- the order in which I created the records. Recently, the table opened in a completely random order, and continues to open in that new order.
I know now that it would have been good to create an autonumber field and order it by that, or have the date field's default value =Now(). I ahve far to many fields to make a new ID field and manually number each record in the order I created them.
Is it at all possible to force the table to order the records in either the order I created the records, or at least the previous configuration (which was just ordered by the time created)?
Also, is there a better way to be doing this...? I want to just have a record for other people who will work with this in the future. In addition, I am new to SQL, and the Access SQl has weird/unique ways of doing things, so for some queries I know I may need in the future I keep a table with the query name and some documentation for what exactly it does and some notes about the syntax (the SQL editor does not allow for -- comments). Is there a better way to do this, too?
Thanks for any help!
one field containing the date (short text)
Dates should never be stored as anything else than date values. No exceptions.
So change the data type of the field to Date, sort on this, and your troubles are gone.
To order Null dates last:
Order By Abs([DateField] Is Null), [DateField]

Access VBA - getting value of current record

I'm a very new user of Access 2016 - I have a form populated by a query, and I want to create a hyperlink out of one of the text boxes. The link should include the combined values of the workday and taskID fields for the specific record the user clicks on.
My problem is that I can't figure out how to get the value of the current record. The control source for these columns are the values themselves, retrieved via the query populating the form's record source. The following code creates the link I want, but it returns the same workday and taskID values every time, regardless of what I click on, and I can't figure out how to make it respect the current record. Any help would be greatly appreciated!
Private Sub workday_Click()
Dim link As String
Dim workday as String
Dim taskID as string
workday = Format(Me!workday, "yyyy-mm-dd")
taskID = Me!taskID
link = "http://myurl.com/" & workday & "/" & taskID
Application.FollowHyperlink (link)
End Sub
EDIT - hope this helps explain a little better
The main table driving this has workID (PK), Workday, taskID, StatusID, DateLastUpdated, UpdatedByUserID. The query used for my form selects all from this table and joins in metadata tables for Task, Status and User. There are multiple records with the same workday value and the same taskID, but never the same combination of the two. The form has a few filters in the header, and the bottom section is a datasheet. In the datasheet I have a combo box used to update StatusID, all other fields are disabled and locked. My hyperlink click event is on the Workday field, and is working in that it creates the appropriate URL, it's just populated with the same taskID and workday values, eg even if I click on the row for "2016-07-12" "456", I still return "2016-07-11" & "123"
Here's a simplified version of what the data looks like. My goal is that when the user clicks on a workday, they are taken to a hyperlink made up of both the workday and TaskID of the row clicked on.
Workday TaskID
2016-07-11 123
2016-07-11 456
2016-07-11 789
2016-07-12 123
2016-07-12 456
2016-07-12 789
2016-07-13 123
2016-07-13 456
2016-07-13 789
I think I actually solved it - the problem was that since almost all fields were locked and disabled, the current record wasn't technically the row I was clicking on. I set Enabled = Yes for the Workday column, and now it's working as expected.

MySQL Query - IF or CASE with AND & Exceptions (for calculations)

SQL Query - extract cell value for calculations as below
Info: Server version: 5.1.39
Php: 5.4
MySQL / phpMyAdmin
Server: Apache
Code is run via: Server SQL Query (copy & paste in the phpMyAdmin) or in MySQL Workbench or using a custom shopping cart manager.
Exports to: Excel (.csv then to .xlsx for sales reports)
Other: I do use a number of tables for referencing
I am trying to write a string (so far WHEN & IF) to do the following:
T5.coupon_code = XYZABC
AND T3.products_id=14 then the value is $5.00
as Ded_Promo
T5.coupon_code = XYZABC
AND (T3.products_id=Anything else) then the value is $0.00
as Ded_Promo
(see table lines 1 & 2 for example).
code tried:
(IF(T5.coupon_code=XYZABC And T3.products_id=14,'5.00','0.00')) As Ded_promo2,
Result: Error Code: 1054. Unknown column 'XYZABC' in 'field list'
The coupon_code value is legit, when I run a query on the coupon_code the code that I am wanting to query shows as being used. (XYZABC is substituted for my real code).
Also tried:
CASE When (T5.coupon_code = XYZABC) then '5.00'
end As Ded_Promo,
Same error as above.
I then need to have the code further expanded to show:
If code & product match = $5.00 As Ded_Promo (as above)
But if code is present (say ID: ABC123DEF), then 'value' (as listed) As Disc_CoupVal
Ideally I need to make sure that the Promo discount doesn't get attributed to other products in the order, whilst the front end calculates it as 1 sale. My reports require that I keep discount coupons and special promotions connected to ONLY the product they are discounting. Commissions rely on this.
However I still need to see all other coupons listed and attribute them across an entire order of 1 or more products.
There will never be more than 1 coupon_code on any given order, the store doesn't allow it.
From above:
orderid | couponcode | ProdID | Discount
21 | XYZABC | 14 | 5.00
21 | XYZABC | 12 | 0.00
36 | ABC123DEF | 3 | 2.50
(IF(T5.coupon_code=XYZABC And T3.products_id=14,'5.00','0.00')) As Ded_promo2
This is causing an error because you are using XYZABC as a column name. Use it as a string:
(IF(T5.coupon_code='XYZABC' And T3.products_id=14,'5.00','0.00')) As Ded_promo2
Edit:
In response to your comments, you can include these in the same column but it's a bit messier:
(IF(
T5.coupon_code='XYZABC' And T3.products_id=14,
'5.00',
(IF(
T5.coupon_code='AnyotherCoupon' And T3.products_id='AnyotherID',
'CouponValue',
'0.00'))
)) As Ded_promo2,
So the above will show 5 if the code is XYZABC and product id is 14.
Otherwise if code is AnyotherCoupon and id is AnyotherID it will show CouponValue.
If neither of these are the case then it will show 0.00.
One thing to note that if there is a lot of these it would be better to store the promotion amount in the database (perhaps as a coupon table?). This keeps the SQL neater and allows you to more easily add and change coupons.
(IF(T5.coupon_code='XYZABC' And T3.products_id=14,'5.00','0.00')) As Ded_promo2,
Try putting single quotes around the XYZABC. This makes the term a literal character string, instead of a named data item in SQL.

Using "totals" functions on an already grouped query

I am having troubles using the aggregate functions when using features like group by in a query. Instead of my aggregate functions applying to the entire query recordset, they only apply to select groups determined by the nature of the query. For example:
Person Date Able
-----------------------------
A 21/05/13 0
B 21/05/13 -1
C 21/05/13 -1
D 21/05/13 0
(grouped by Person, Date, Able)
When applying aggregate functions:
Person Date Able Max(Able) Min(Date)
----------------------------------------------------
A 21/05/13 0 0 21/05/13
B 22/05/13 -1 -1 22/05/13
C 23/05/13 -1 -1 23/05/13
D 24/05/13 0 0 24/05/13
The aggregate functions are made entirely redundant unless the data is completely ungrouped. So far, I have been getting around it using:
1) Using another query to reference the initial query and determine the true aggregate values.
2) Have the form call this second query using the d functions (dlookup,dcount etc.)
In my particular scenario, I have a list (very similar to above) that needs to be presented in a certain order (ranked based on ID). However, I am using an expression in the query to define a different type of ranking. The idea is to show (using conditional formatting) the first record in this new rank. Illustrated below
Person Date ID CalculatedRank
--------------------------------------------
A 21/05/13 1 4
B 21/05/13 2 2
C 21/05/13 3 3
D 21/05/13 4 1
Ideally I would like to have another column that determines which one is first which could be easily acheived by:
first: [CalculatedRank] = Min( [CalculatedRank] )
But as described above, Min() is not giving me 1, it is giving me it on a per row basis (the minimimum isn't always 1 so I can't set this arbitrarily).
Right now I am using a separate query to reference this first query and I sort that based on the calculated rank. Conditional formatting can then use dlookup to determine whether it is first or not from the second query. However, everytime the form refreshes, or a requery is called, every single row's conditional formatting triggers another dlookup, which then references the first query recalculating the new rank, for every row!
As you can imagine, the delay is noticeable causing the cursor to be idle for >5seconds. I am not too sure about the internal mechanisms of access, but using the inbuilt debugger, a requery on a recordset of 4 rows caused my CalculateRank() function to be called 12 times, purely through the conditional formatting calling the second query.
In summary, I have pretty much narrowed it down to requiring a separate query (and therefore dlookup) to properly use the aggregate functions. If I was able to keep everything in one query, the conditional formatting wouldn't need to use dlookup on another query to determine its status.
I am sure I am not the only one that has had problems with this and was wondering if any solutions exist where I can avoid all the stacked querying.
As always, any help is much appreciated!
Wow, I see what you mean! For my table [Table1]
Person Date ID
------ ---------- --
A 2013-05-21 1
B 2013-05-21 2
C 2013-05-21 3
D 2013-05-21 4
and my query [qryTable1Ranked]
SELECT Table1.*, CalculateRank([ID]) AS CalculatedRank
FROM Table1;
which uses the following function in a standard VBA Module
Public Function CalculateRank(ID As Long) As Long
Dim r As Long
Select Case ID
Case 1
r = 4
Case 4
r = 1
Case Else
r = ID
End Select
CalculateRank = r
Debug.Print "x"
End Function
and returns
Person Date ID CalculatedRank
------ ---------- -- --------------
A 2013-05-21 1 4
B 2013-05-21 2 2
C 2013-05-21 3 3
D 2013-05-21 4 1
when I just double-click the query to open it in Datasheet View my ranking function gets called 4 times, once for each row.
If I create a continuous form based on that query and open the form my function gets called 4 times. Then if I add conditional formatting on the [CalculatedRank] text box using Value = DMin("CalculatedRank", "qryTable1Ranked") then my function gets called 32 times!
I found that I can cut that by half (to 16 times) if I add an invisible unbound textbox named [txtMinCalculatedRank], use the following code behind the form...
Option Compare Database
Option Explicit
Private Sub Form_Load()
UpdateMinCalculatedRank
End Sub
Private Sub UpdateMinCalculatedRank()
Me.txtMinCalculatedRank.Value = DMin("CalculatedRank", "qryTable1Ranked")
End Sub
...and change the Conditional Formatting rule to Value = [txtMinCalculatedRank].
I found that I could cut that by half again (to 8 times) if I changed the Record Source of the Form from qryTable1Ranked to Table1 (the base table) and changed the Control Source of the [CalculatedRank] text box to =CalculateRank([ID]) (still using the tricks from the previous tweak).
I think that's probably as good as it gets without going so far as to create a temporary table, or persisting the CalculatedRank (and perhaps an "IsMin" flag) in the base table.

Move data from field to field in MS Access

In MS Access I have a table like this:
TABLE USER
Id (int)
Status (text)
OldStatus (text)
I would like to create a MS Access Query which, when executed, moves the content of Status field to the top of OldStatus, and adds the current year. For example:
Before execution:
ID STATUS OLDSTATUS
1 Very good [2010] Excelent
[2009] Very bad
2 Excelent [2010] Bad
[2009] Good
After execution:
ID STATUS OLDSTATUS
1 [2011] Very good
[2010] Excelent
[2009] Very bad
2 [2011] Excelent
[2010] Bad
[2009] Good
How can I do that? THANK YOU!!
Didn't know the name of your table, so you'll have to substitute. The goal here is to maintain the old status while concatenating the current status. I used a space (" ") to put between, but you can use any character you want.
Update Status_Table
Set OLDSTATUS = OLDSTATUS & " " & STATUS
For what it is worth, there is a better way. Create a StatusHistory table:
StatusHistoryID
, StatusID
, Status
, Status_Date
You would have more flexibility here. Reports could be set for a date range or a particular year. You're table would be very hard to run a report on the status for 2010.