Results 1 to 9 of 9
  1. #1
    dtours is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2015
    Location
    Guelph, ON
    Posts
    6

    Query with Function very slow

    I have a query that returns room booking details for a particular trip. One of the fields in the query uses a function which returns the last names of the people in each room but this is slowing down the query where it can take up to 12 seconds to open the form (and this is on a fast system). Just wondering if anyone can suggest another way to return the last names that would be fast.



    I can post code if needed.

    Thanks in advance,
    John

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Yes, UDF code can make queries run slow. Why are you using a UDF to get last names on a form? Maybe should use a listbox or subform.
    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.

  3. #3
    dtours is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2015
    Location
    Guelph, ON
    Posts
    6
    Quote Originally Posted by June7 View Post
    Yes, UDF code can make queries run slow. Why are you using a UDF to get last names on a form? Maybe should use a listbox or subform.
    I need to format last names separated by I comma in a datasheet which is already a subform. Don't see how a listbox would solve this issue.
    Attached Thumbnails Attached Thumbnails Rooms.jpg  

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Are you using a UDF because first name field might be Null and don't want comma if there is not first name? Will there always be last name?

    An IIf() expression in query or textbox can handle that and run faster.

    [lastname] & IIf(Not IsNull([firstname]), ", ", "") & [firstname]
    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
    dtours is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2015
    Location
    Guelph, ON
    Posts
    6
    No I am only using last names but there could be as many as 4 people in a room. A comma will only be added if there is more than one person in in a room. The function grabs the RoomID and queries the database to see who is in that room with names separated by a comma. The it does the same for the next record and so on.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Okay, that is concatenating data from multiple related records to display in one field and does require UDF. Normally this is done on a report and not a form. The only way to speed up, as suggested, is with dependent list box or related subform (http://www.fmsinc.com/MicrosoftAcces...edSubforms.asp).
    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
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    struggling to understand what you are trying to achieve. Why not post the sql and the function code you are using at the moment together with what the result should look like. The image in post #3 is presumably not it and I don't see how it can be the source data since there is no room number - or is that the Beds value?

    Other things to check include is your table properly indexed and do you really need to be bringing through data from 9 years ago?

  8. #8
    dtours is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2015
    Location
    Guelph, ON
    Posts
    6
    The RoomID is hidden on the form but it is in the underlying query.Click image for larger version. 

Name:	query.jpg 
Views:	8 
Size:	153.1 KB 
ID:	20294

    Here is the UDF:

    Function GuestNames(lngRoomID As Long) As String

    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    Dim strSQL As String
    Dim strGuestNames As String
    Set dbs = CurrentDb()
    strSQL = "SELECT tblCustomers.LastName " _
    & "FROM (tblCustomers INNER JOIN tblTourBookings " _
    & "ON tblCustomers.CustomerID = tblTourBookings.CustomerID) " _
    & "INNER JOIN tblCustomerRooms " _
    & "ON tblTourBookings.TourBookingID = tblCustomerRooms.TourBookingID " _
    & "WHERE tblCustomerRooms.RoomID = " & lngRoomID & " " _
    & "ORDER BY tblCustomers.LastName;"
    Set rst = dbs.OpenRecordset(strSQL, dbOpenDynaset)
    If Not rst.EOF Then
    rst.MoveFirst
    Do While Not rst.EOF
    strGuestNames = strGuestNames & rst!LastName
    rst.MoveNext
    If Not rst.EOF Then
    strGuestNames = strGuestNames & ", "
    End If
    Loop
    GuestNames = strGuestNames
    Else
    GuestNames = "Enter Name(s)"
    End If

    rst.Close
    Set rst = Nothing
    dbs.Close
    End Function

    It actually works quite well on my laptop but when running the front-end on my laptop connected to the back-end on the network it can take of to 12 seconds to open the form.

  9. #9
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    but when running the front-end on my laptop connected to the back-end on the network it can take of to 12 seconds to open the form
    this is more an issue with your network performance rather than a slow query/function. However it looks to me like your function sql could be improved - I can't see how it is limiting it to checkin/out dates from tblRooms. Also a small improvement may be had by changing dbOpenDynaset to dbOpenSnapshot

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

Similar Threads

  1. Function running slow for different user
    By speja01# in forum Access
    Replies: 4
    Last Post: 11-18-2013, 03:59 PM
  2. Query running Slow
    By mike02 in forum Queries
    Replies: 2
    Last Post: 07-26-2013, 12:47 PM
  3. slow query when asking for sum of minutes
    By sf827 in forum Queries
    Replies: 13
    Last Post: 01-04-2012, 09:00 PM
  4. Query running VERY slow
    By purple_kittykat in forum Queries
    Replies: 4
    Last Post: 07-31-2011, 12:40 AM
  5. Slow query - Help to speed up?
    By jgelpi16 in forum Queries
    Replies: 9
    Last Post: 05-26-2011, 01:23 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