Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281

    Setting the value of a TextBox on a Form

    On to my next challenge...



    I'm trying to set the value of a TextBox on a form in the Form Load event with the following code:

    Code:
    Private Sub Form_Load()
        If Me.List37.ListCount = 0 Then
            Me.Text77.Value = 0
        End If
    End Sub
    Experienced Access coders probably know I'm getting a "You can't assign a value..." error

    The control source event for Text77 is: =
    [List37].
    [ListCount]-1

    Thus, when the form loads the TextBox loads with a value of -1 and I would like it to show 0

    Thank you so much for any help...

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    you dont need the .VALUE
    if List37.ListCount = -1 then text77 = 0

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You can't assign a value to a control that has a calculation in the control source. Use an IIf() in the control source to set a different value when the count is zero.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    Hey Ranman ~

    Okay...So I tried the following and I receive a: "End If Without Block If" Error
    Code:
    Private Sub Form_Load()
        If Me.List37.ListCount = 0 Then Me.Text77 = 0
        End If
    End Sub
    If I set the code up this way I receive the: "You can't assign a Value..." Error
    Code:
    Private Sub Form_Load()
        If Me.List37.ListCount = 0 Then
            Me.Text77 = 0
        End If
    End Sub
    Ugh!

    (And at one point while I was trying to figure this out the above Form Load event caused Access to spew a parameter error on a completely unrelated ComboBox (Combo46)

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    As pbaldy said, cannot set value of testbox that has a calculated expression in ControlSource so use IIf() expression in the ControlSource, no VBA.

    Or don't have anything in the ControlSource property and set value in Form OnCurrent event using If Then Else.

    Recommend the IIf() in ControlSource.

    =IIf(List37.ListCount = 0, 0, List37.ListCount - 1)
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Did you see post 3? You can't do what you're trying to do. You could, if you take the formula out and set the value in code for both possibilities.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Never mind, I'll get out of the way.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    Pbaldy ~

    Like a dream...

    Code:
    =IIf(
    [List37].
    [ListCount]=0,0,
    [List37].
    [ListCount]-1)
    Although with decades of experience coding in the Excel VBA environment it makes no sense why Access can't do this in the Form Load event as long as it works ~ I'm happy

    Thank you again and thank you again Ranman as well...

  9. #9
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    June I didn't see your post...

    You had it right as well...

    Thank You to everyone!!

  10. #10
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    June... I would like to stay with your post for a moment...

    In Excel I would use the Initialize Event to perform this function

    You mention the OnCurrent event and I can't find this... I have OnConnect is that the same thing

    And lastly, which would be the best way to do this, using ControlSource or VBA?

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by RunTime91 View Post
    Pbaldy ~

    Although with decades of experience coding in the Excel VBA environment it makes no sense why Access can't do this in the Form Load event as long as it works ~ I'm happy
    It's not that it can't do it in the load event, it's that you can't set the value of a textbox that has a formula in the control source. If your textbox didn't have that formula, it would have worked.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    Hey Paul...

    I think our posts (3 & 4) may have crossed ~ I did use your suggestion and it worked...June added some context and that was very helpful as well.

    I think Ranman may have missed in my initial post where I had place the formula in the ControlSource, otherwise I believe his suggestion would have worked also.

    On an unrelated note, as I begin my new (employer well compensated suggested) life in Access I know some, perhaps many, of my Excel habits will prove frustrating for me.

    But I promise to never waste anyone's time...even though many of my questions for now will be of the rookie variety - if I am asking them here ~ I truly need help.

    So, if you or anyone here has any suggestions on how I can make the transition from Excel VBA to Access VBA easier...I'm listening...

    Thanks again...

  13. #13
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    The OnCurrent event is the first event listed on property sheet for Access form or report, just above OnLoad.

    I suggested OnCurrent event so the value will recalculate when navigating records but if the combobox listcount is not variable then does not matter. Are combobox and textbox in form Header section?

    Using VBA to set control value will show the same result for all records regardless of event used. Calculation in ControlSource can be record dependent.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    June, Hello

    The form is built in the Detail Section... I see now the OnCurrent Event in the properties list.

    Based on your comments, I think I'm in the right place with using a calculation in the ControlSource as I do want this total to update depending on the # of records shown in the ListBox

    Thanks June...

  15. #15
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    This is a subform?

    Curious, why do you need to show the count of combobox list?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 10-27-2014, 10:33 PM
  2. Replies: 3
    Last Post: 07-09-2014, 02:49 PM
  3. Setting textbox values on a report using vba
    By Markb384 in forum Reports
    Replies: 6
    Last Post: 01-24-2014, 09:54 AM
  4. Setting ubound textbox value in Report
    By Monterey_Manzer in forum Reports
    Replies: 2
    Last Post: 11-01-2013, 10:00 AM
  5. Replies: 3
    Last Post: 06-14-2013, 01:50 PM

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