Share via

VBA query

Anonymous
2024-02-23T10:48:12+00:00

Can anyone provide me with the vba to populate a control with the maximum value of another control +1

Microsoft 365 and Office | Access | For home | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. ScottGem 68,810 Reputation points Volunteer Moderator
    2024-02-23T14:37:41+00:00

    Thank you,

    I changed my code to the following, but I do not think the word query is correct

    A query refers to a SQL statement that extracts data from a table or combination of tables. VBA code refers to programming code that can automate a task. VBA code can include a query being executed within a code module.

    A control contains a single value. A control cannot have a Max value, A Field that a control is bound to can have a Max value. Using the proper terms can help us help you.

    Frankly, this is not diffilcult

    Me.controlname = Nz(DMax("[fieldname]","tablename"),0)+1

    Me.Dirty = False

    That is all that is needed. You might need to use criteria in the DMax if you want the max within a group of data. For exmple:

    Me.controlname = Nz(DMax("[fieldname]","tablename", "Year(Date() = " & Year(datefield)),0)+1

    Will get the max value within the current year.

    The Me.Dirty = False commits the current record so that another user doesn't generate the same number.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Anonymous
    2024-02-23T12:05:10+00:00

    Hi,

    I changed the code to

    maxVal = DLookup("MaxOfOurRef", "Query1")

    My query finds the max of field OurRef but when you run the query the column heading changes to MaxOfOurRef.

    Now getting run time error 6 overflow on that line

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2024-02-23T11:54:51+00:00

    Hi Storming,

    It seems like you are using a query to retrieve the maximum value for the control "OurRef" and then populating another control with the incremented value. Your use of the word "query" is not incorrect; however, it might be a bit confusing since you're not directly executing a query here but rather accessing the result of a query.

    Your code can be improved a bit. Instead of 'Query("Query1"). Controls("OurRef"). Value', you should be using 'DLookup' to retrieve the maximum value from the query result. Here's how you can modify your code:

    DoCmd.OpenForm "Form", , , , acFormAdd

    Dim maxVal As Integer Dim newMaxVal As Integer

    ' Retrieve the maximum value using DLookup maxVal = DLookup("MaxValue", "Query1")

    ' If the value is Null, initialize maxVal to 0 If IsNull(maxVal) Then maxVal = 0 End If

    ' Calculate the new value by adding 1 to the maximum value newMaxVal = maxVal + 1

    ' Populate the control "OurRef" with the new value Forms("Form"). Controls("OurRef"). Value = newMaxVal

    DoCmd.Close acForm, "Main menu"

    In this code snippet, "MaxValue" is the field name you expect to retrieve from "Query1" that holds the maximum value. Adjust it according to your actual field name. This code will handle the scenario where the query result may return 'Null', initializing 'maxVal' to 0 in such cases.

    I hope this information helps! If I can be of help with anything else, please let me know. I will be glad to answer any other questions that you might have.

    Give back to the Community. Help the next person who has this issue by indicating if this reply solved your problem. Click Yes or No below.

    Warm Regards, Utkarsh

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2024-02-23T11:45:25+00:00

    Thank you,

    I changed my code to the following, but I do not think the word query is correct

    DoCmd.OpenForm "Form", , , , acFormAdd

    Dim maxVal As Integer

    Dim newMaxVal As Integer

    ' Assuming TextBox1 is the control you want to get the maximum value from

    maxVal = Val(Query("Query1").Controls("OurRef").Value) 
    

    ' Calculate the new value by adding 1 to the maximum value

    newMaxVal = maxVal + 1 
    

    ' Assuming TextBox2 is the control you want to populate with the new value

    Forms("Form").Controls("OurRef").Value = newMaxVal 
    

    DoCmd.Close acForm, "Main menu"

    My query obtains the max val for the control OurRef

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2024-02-23T11:22:30+00:00

    Hi Storming,

    Thank you for reaching out to this forum. I'm Utkarsh and as a fellow Microsoft user, I'm here and ready to help you today. I am a user like you, this is a user forum and not Microsoft directly.

    Do not worry, I will help you sort this issue, rest assured that I will do my best to assist you.

    If you're working with VBA in a Microsoft application like Access or Excel, and you want to populate a control (let's say a textbox) with the maximum value of another control (let's say another textbox) plus 1, you can achieve this with some VBA code. Here's an example function you can use:

    Function PopulateControlWithMaxPlusOne() Dim maxVal As Integer Dim newMaxVal As Integer

    ' Assuming TextBox1 is the control you want to get the maximum value from maxVal = Val(Forms("YourFormName"). Controls("TextBox1"). Value)

    ' Calculate the new value by adding 1 to the maximum value newMaxVal = maxVal + 1

    ' Assuming TextBox2 is the control you want to populate with the new value Forms("YourFormName"). Controls("TextBox2"). Value = newMaxVal End Function

    Replace "YourFormName", "TextBox1", and "TextBox2" with the actual names of your form and controls.

    You can call this function whenever you need to populate the control with the updated value. For example, you might call it in response to a button click or in the appropriate event handler for your form.

    I hope this information helps! If I can be of help with anything else, please let me know. I will be glad to answer any other questions that you might have.

    Give back to the Community. Help the next person who has this issue by indicating if this reply solved your problem. Click Yes or No below.

    Warm Regards, Utkarsh

    Was this answer helpful?

    0 comments No comments