Results 1 to 8 of 8
  1. #1
    Glenley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    91

    Problem with concatenating in SQL statement

    I'm having some trouble with my code. I bolded and underlined the section in question. We do drug testing and I'm slowly trying to build a random generator form. I'm kind of a noob when it comes to VBA so be easy on me. TIA

    Private Sub btnTest_Click()


    Dim db As Database
    Dim rsC As Recordset
    Dim Cust As Long

    StatusBox = ""
    Cust = Forms!RandomF!CustomerCombo.Column(0)
    Set db = CurrentDb
    Set rsC = db.OpenRecordset("SELECT Rnd([DonorID]*Now()) AS X, DonorFirstName, DonorLastName, OrganizationID " _
    & "FROM DonorT " _
    & "WHERE OrganizationID='" & Cust & "'" ORDER BY X DESC)

    While Not rsC.EOF
    Status rsC!X & ":" & rsC!DonorFirstName & " " & rsC!DonorLastName
    rsC.MoveNext
    Wend

    rsC.Close
    db.Close
    Set rsC = Nothing
    Set db = Nothing



    End Sub

  2. #2
    Glenley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    91
    And YES I indent my code, it just didn't paste the indentation

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Try the following:
    & "WHERE OrganizationID=" & Cust & " ORDER BY X DESC)"

    You don't need quotes with numbers.

  4. #4
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Quote Originally Posted by Glenley View Post
    And YES I indent my code, it just didn't paste the indentation
    That's one reason why we often tell people to use code tags (# on posting toolbar, on the far right side).
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Put all the sql into a string variable. Then you can debug.print it and see what you actually have, not what you think you have.
    When correct, then use that variable n the recordset.

    Plus if you cannot get it correct you can copy and paste here and someone can advise what you have done wrong?

    What is is supposed to do anyway?
    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
    Glenley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    91
    The variable prints correctly (I'm guessing at the terminology). My Organization ID is what it's supposed to be. The problem arises when add the sort to the SQL statement. I tried:
    & "WHERE OrganizationID=" & Cust & " ORDER BY X DESC) but I get a syntax error. If I take the sort completely out, it runs fine

  7. #7
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    If I take the sort completely out, it runs fine
    You can't sort on X, a calculated alias.
    You will have to

    ORDER BY Rnd([DonorID]*Now()) DESC

    However, this might calculate a different number from the previous, which would make the sort useless.

  8. #8
    Glenley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    91
    Thank you, that worked. I'm okay with the sort being changed. It needs to be random. Now I need to look into the Randomize ​function

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

Similar Threads

  1. Replies: 2
    Last Post: 06-23-2017, 05:19 AM
  2. SQL statement problem!
    By AccessPractice in forum Queries
    Replies: 7
    Last Post: 05-18-2016, 12:04 PM
  3. Replies: 11
    Last Post: 04-29-2015, 01:38 PM
  4. IF/THEN statement problem
    By sfgiantsdude in forum Access
    Replies: 5
    Last Post: 01-06-2012, 03:50 PM
  5. Problem With IF Statement
    By MuskokaMad in forum Programming
    Replies: 0
    Last Post: 03-14-2010, 05:26 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