473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Button in Access 2016 to add +1 to last/highest value in a field

3 2Bits
Hi and thank you in advance everyone!
I want to add +1 to a value in a field by clicking on a button in a form.

My table called "Requests" has a field called "Request#". I want to add a button in the form called "TblRequests" so that when it is clicked, it will add +1 to the highest number in the field.

I am using this VBA code (please see below) that works very well but I have to double click on the field in order to add 1 to the greatest value.

Instead, I need to have a button that will do the same thing. Any help if so much appreciated.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Request__DblClick(Cancel As Integer)
  2. Me.Request_ = Nz(DMax("Val([Request#])", "Requests"), 0) + 1
  3. End Sub
Thanks
Jun 25 '20 #1

✓ answered by cactusdata

How about:

Expand|Select|Wrap|Line Numbers
  1. Private Sub AddOneButton_Click()
  2.  
  3.     Me.Request_ = Nz(DMax("Val([Request#])", "Requests"), 0) + 1
  4.  
  5. End Sub

9 2572
cactusdata
214 Expert 128KB
How about:

Expand|Select|Wrap|Line Numbers
  1. Private Sub AddOneButton_Click()
  2.  
  3.     Me.Request_ = Nz(DMax("Val([Request#])", "Requests"), 0) + 1
  4.  
  5. End Sub
Jun 26 '20 #2
NeoPa
32,556 Expert Mod 16PB
Hi Alocin.
CactusData's suggestion will work perfectly except for the name of the CommandButton. If it's important to name it as [TblRequests] then change the first line to :
Expand|Select|Wrap|Line Numbers
  1. Private Sub TblRequests_Click()
However, I'd advise you to use a different name as most people will read that and assume it refers to a Table object rather than a CommandButton. Usually CommandButtons are named starting with "cmd". This is not required but is generally helpful for when others are looking at your project.

It's interesting that you have the internal code logic done perfectly with just a little help needed on how to link it to a CommandButton click. Usually the internal code bit is where people struggle the most. Nice work :-)

NB. I've set the Best Answer for you as I can see it directly answers your question.
Jun 26 '20 #3
Alocin
3 2Bits
Thanks you guys, but not sure why nothing is happening after entering new code.
Here is what i have:
Table is "Requests"
Table field is "Request#"
Form Field I want the number to change is "Request#"
Button I want to click on is "cmdAddNumber" (changed name - thanks NeoPa)

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdAddNumber_Click()
  2.  
  3.     Me.Request_ = Nz(DMax("Val([Request#])", "Requests"), 0) + 1
  4.  
  5. End Sub

thank you for any help
Jun 26 '20 #4
cactusdata
214 Expert 128KB
You wrote Me.Request_ but if it now is Me.Request#, then change it accordingly.
Jun 26 '20 #5
NeoPa
32,556 Expert Mod 16PB
CactusData:
You wrote Me.Request_ but if it now is Me.Request#, then change it accordingly.
I believe the # character is proscribed (Not allowed.) in VBA names so that when it's used VBA transcribes it as an underscore (_).
@Alocin.
You may want to review that too. A hash (#) should be avoided in your table & field names where possible.

Before you do that though, check the properties pane when the new CommandButton (cmdAddNumber) is selected on your form. Is there an entry by On Click that says [Event Procedure]? If not then the code won't be triggered. To fix this go to the associated module, Cut all the code into the Clipboard, then Paste it all in again. This should set the property for you.
Jun 26 '20 #6
cactusdata
214 Expert 128KB
True. Try with:

Expand|Select|Wrap|Line Numbers
  1. Me.[Request#].Value = Nz(DMax("Val([Request#])", "Requests"), 0) + 1
Jun 26 '20 #7
NeoPa
32,556 Expert Mod 16PB
Actually Alocin, what you refer to as a form field is actually a Control rather than a Field. This would also benefit from being named accordingly. As a TextBox control (I suspect) it could be named something like [txtRequest], regardless of the name used in the table for the field.

So, assuming we were also to rename the Table's Field by removing the hash (#) char from the end, you'd end up with the following code :
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdAddNumber_Click()
  2.     Me.txtRequest = Nz(DMax("Val([Request])", "Requests"), 0) + 1
  3. End Sub
Jun 26 '20 #8
Alocin
3 2Bits
Omg you guys I am going to pull my hair! I have spent so much time on one little but obviously big issue to me.

Ok, the suggested code didn't make a difference. Nothing happens when I run it! Sorry, I am sure I am forgetting something.
So to be sure I still had an option, I tried putting in the old (above) double click code on the field "Request#" (as it was working before), now that one is not working either. I restarted Access and nothing changed. I even rebooted my machine, nothing changed. What have I done wrong because still neither is working now. So, I deleted the form and recreated the form using the wizard, as I did on my first form.

The table is still "Requests". The field in the table is still the same "Request#". The new form is still called the same "TblRequest". In form design mode, I select the control "Request#" (NeoPa, thanks for teaching me the proper terminology), I jump over to view the code. With the new form when in VBA I have a new clean object with no code. I paste the below code (see below full code), save the form. Then open the form and I get an error "Run-time error '3464': Data type mismatch in criteria expression". I tried also using code with "...Me.[Request#].Value = Nz(DMax("Val([Request#])", "Requests"), 0) + 1" with no difference in the outcome.


Expand|Select|Wrap|Line Numbers
  1. Private Sub Request__DblClick(Cancel As Integer)
  2.  
  3. Me.Request_ = Nz(DMax("Val([Request#])", "Requests"), 0) + 1
  4.  
  5. End Sub
Still please help!
Thank you.
Jun 26 '20 #9
NeoPa
32,556 Expert Mod 16PB
There is nothing obviously wrong with your code in the context you've described to us. I imagine something is not quite as portrayed but let's see if we can work out what.

Is the [Requests].[Request#] field numeric or is it text (string)?
Is there data in the table already?
If so, what is the largest value currently in the table?
Jun 26 '20 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: shank | last post by:
How do you return the highest value in a recordset of maybe 100 records? Is it necessary to run 2 recordsets? I was hoping it was as simple as Max(), but no luck. thanks
6
by: Ada | last post by:
hello folks, is there a way to retrieve the highest value in the ArrayList? let say i have a value in the array: {1, 4, 15, 3, 7} it should return a value 15 as the result. i've looked at...
7
by: Jan | last post by:
Hi there, Is there a fast way to get the highest value from an array? I've got the array strStorage(intCounter) I tried something but it all and's to nothing If someone good helpme, TIA
3
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i have an int array and was just wondering if there is a way to get the highest value in the array? for instance, int myValues = new int { 0, 1, 2 } highest value is 2. thanks,
1
by: Coll | last post by:
Hi - I'm trying to figure out how to append a record to a table and then open a form and display that record. My thought was to use the autonum primary key field (recordnum) and display the highest...
1
by: CD Tom | last post by:
I'm thinking about updating from Access 2007 to Access 2016 but have run into a problem. I can't find any place to compile my accdb to an accde. I've installed the office 2016 and can bring up my...
2
by: Silver993 | last post by:
I have created Access 2016 Database with custom Icon. I can’t figure out what I am doing wrong. Here is the problem: When I tested the database in my developing Computer, it opens with my custom Icon...
1
by: Murphy | last post by:
I am gettig ready to upgrade from Windows XP / Office 2007 to Windows 10 / Office Professional 2016. Are Access 2007 databases compatible with or convertible to Access 2016?
8
by: MS Access Probz | last post by:
Hi all, I'm new to this forum. My Access skills are above basic, but I seem to be having trouble getting to the next level regardless of how much time I invest in it. I'm using Access 2016. ...
1
by: HalloweenWeed | last post by:
Good day I am new to the forum and still learning MS Access. I have developed a code to allow 'live' 'searh-on-the-fly' queries 'as you type.' However, the problem lies in that when doing a query you...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.