Results 1 to 8 of 8
  1. #1
    Khalil Handal is online now Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    240

    Make a VBA Code shorter or more than one line

    Hi to All,

    I am trying to have this VBA one line of code to be shorter or to put it in more than one line.
    Code:
    ChkoutCopies = DCount("[fkBookId]", "tblBorrowedBooks", "[fkBookId]= Forms!frmBorrowingBooks!cntBorrowedBooksSubForm.Form!txtfkBookId AND DateReturned Is Null AND DateBorrowed Is Not Null")
    It seems I am missing something in my trials.
    Khalil

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Where is the code in relation to your borrowingbooks form?

    also investigate the use of the vba continuation character (the underscore)

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    That looks like it might be a calculated field in a query and not VBA. Is Null is for sql, IsNull() is for VBA?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    IS NULL works in domain aggregate function WHERE argument, no matter where the function is used. The IS NULL is within quotes and processed by the WHERE argument which follows SQL syntax.

    Note use of continuation character (_) and & use when breaking a string. Ampersand could be at beginning of following line if you prefer. Be sure not to lose spaces when breaking strings.
    Code:
    ChkoutCopies = DCount("*", "tblBorrowedBooks", _
                            "[fkBookId]= Forms!frmBorrowingBooks!cntBorrowedBooksSubForm.Form!txtfkBookId " & _
                                                "AND DateReturned Is Null AND DateBorrowed Is Not Null")
    If code is behind subform, instead of embedding full control path, could concatenate.
    Code:
    ChkoutCopies = DCount("*", "tblBorrowedBooks", _
                           "[fkBookId]= " & Me.txtfkBookId  & " AND DateReturned Is Null AND DateBorrowed Is Not Null")

    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.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    I always put the space at the start of the next line

    Easier to spot, instead of having to scroll to the far right to make sure one is there on the previous line.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Good point. Ideally, lines are short enough to not need scrolling but that does not always happen, especially if VBA window width is changed during coding/debugging.

    I do wonder why saving an aggregate value to a field/control with VBA.
    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.

  7. #7
    Khalil Handal is online now Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    240
    Hi to all,

    The text box txtfkBookId is on the subform. The code is also behind an AfterUpdate event of a Date field on the subform. I will be using the: & Me.txtfkBookId.

    The ChkoutCopies is to know how many books are chekced out and to see if we still have copies available; If we still have copies the user can borrow one, else it shows a message that no copies are available and the user has to reserve the book until it is returned to the library.

    Thank you all for the help.
    Solved

    Khalil

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    My point is, probably don't need to save this value to table. Just calculate when needed. Domain aggregate expression can be in textbox ControlSource. Regardless, record would have to first be committed to table if you want it to be included in the aggregate value.
    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.

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

Similar Threads

  1. Make if function shorter
    By stoic_bird in forum Forms
    Replies: 14
    Last Post: 05-28-2022, 05:07 AM
  2. Replies: 24
    Last Post: 09-14-2021, 02:37 PM
  3. Replies: 1
    Last Post: 11-11-2014, 03:54 PM
  4. How to do line by line compare of VB code?
    By Buakaw in forum Access
    Replies: 2
    Last Post: 02-14-2011, 11:46 PM
  5. Ideas on shorter code for cascading updates
    By usmcgrunt in forum Programming
    Replies: 3
    Last Post: 12-03-2010, 12:17 PM

Tags for this Thread

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