Results 1 to 10 of 10
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672

    VBA To Set Value Of TextBox That Has Control Source

    I am trying to use this VBA to add the next system generated userID from table tblUsers to my form. tbox1 has a control source set, but I want to populate it with the below syntax, altho I get an error. What is causing this error? This is not an autonumber field, or a PK field. And if I display the DMAX() value in a MsgBox it shows exactly as it should.



    And I am calling this from the vBA syntax ONLoad() for the form.
    Code:
    Me!tbox1.Value = DMax("userID", "tblUsers") + 1
    You can't assign a value to this object

  2. #2
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    It is bound to the table so the value must come from the table. Are you trying to add a new record?

  3. #3
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Hmm, I just tried it and it works, so not sure what the problem is.

  4. #4
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by aytee111 View Post
    It is bound to the table so the value must come from the table. Are you trying to add a new record?
    Yes-wanting to add a new record. A button is pressed from my main form, and the main form is closed and this second form is opened, and I use this in the OnLoad() event.

  5. #5
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    What is the control source? If you remove this line of code does the rest of the form work as expected?

  6. #6
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    The controlsouruce is tblusers.userID - so that the table is updated with the id once the save button has been hit. If I comment out this line of code, yes the form works as expected EXCEPT the userID textbox being populated.

  7. #7
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Is tblUser the record source for your form?

  8. #8
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Yes, it is.

  9. #9
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    I am out of ideas! Could you post your db, or just this part?

  10. #10
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    It seems to me that you are trying to use a function that returns the maximum value in a domain and add 1 to it, all the while binding the control to that domain. When you think about it that way, I don't believe it's possible to have both. The solution may be that the control can't be bound to the field while at the same time, be calculated.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Textbox Control Source
    By asmores in forum Access
    Replies: 5
    Last Post: 02-19-2015, 09:59 AM
  2. Change a Control Source of a textbox VBA
    By WickidWe in forum Forms
    Replies: 1
    Last Post: 01-05-2014, 03:01 PM
  3. Textbox Control Source As Field Input
    By Jester0001 in forum Forms
    Replies: 4
    Last Post: 03-02-2012, 10:50 AM
  4. Replies: 17
    Last Post: 02-08-2012, 10:06 AM
  5. Textbox Control Source As An Expression help!
    By emilyrogers in forum Forms
    Replies: 11
    Last Post: 02-11-2011, 07:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums