Results 1 to 30 of 30

Thread: **RESOLVED**SQL Assistance PLEASE!

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679

    **RESOLVED**SQL Assistance PLEASE!

    The SQL statement below is what I currently have that is working:

    VB Code:
    1. rs.Open "SELECT RepName FROM Main WHERE RepName = '" & txtRepName.Text & "'"
    I need to ammend this statement with another condition...I also need to query by date that is between #5/1/03# and #5/31/03#.

    Something like:

    VB Code:
    1. rs.Open "SELECT RepName FROM Main WHERE DateRef BETWEEN #5/1/03# AND #5/31/03#"
    My problem is , how do I combine both conditions into one statement?

    Thank you.
    Last edited by Salvatore; May 20th, 2003 at 10:22 AM.

  2. #2
    Lively Member mmiill's Avatar
    Join Date
    May 2002
    Location
    SERBIA
    Posts
    73

    !

    what's wrong whit this

    SELECT * FROM * WHERE *='' AND * BETWEEN
    #/1/1/1# AND #1/1/1#



    mm

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    I need the query to be based on a specific name located in the txtRepname textbox as well as to pull only those records that are within certain dates!

    The SQL statement that you provided would give me ALL records that were between certain dates.

  4. #4
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    You can have multiple conditions in a WHERE clause.

    SELECT ... WHERE RepName='name' AND Date BETWEEN ...

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    BriantCVA:

    I added the AND keyword, and now I get a Compile Error on the keyword BETWEEN...

    VB Code:
    1. rs.Open "SELECT RepName FROM Main WHERE _
    2.  RepName = '" & txtRepName.Text & "'" AND DateRef [B]BETWEEN[/B] #5/1/03# AND #5/31/03#"
    I must be missing something....

  6. #6
    Hyperactive Member
    Join Date
    May 2003
    Posts
    401
    visual basic code:--------------------------------------------------------------------------------rs.Open "SELECT RepName FROM Main WHERE _
    RepName = '" & txtRepName.Text & "'" AND DateRef BETWEEN #5/1/03# AND #5/31/03#"--------------------------------------------------------------------------------

    Your code should be something like this:

    rs.Open "SELECT RepName FROM Main WHERE _
    RepName = '" & txtRepName.Text & "' AND DateRef BETWEEN #5/1/03# AND #5/31/03#"

    Hope this works.

    Cheers,
    Aparna

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    I do not see the difference between the code you provided and the one I currently have....

    Please explain the difference.

    Thank you.

  8. #8
    Hyperactive Member
    Join Date
    May 2003
    Posts
    401
    In your earlier code, you wrote:
    txtRepName.Text & "'" AND DateRef BETWEEN #5/1/03# AND #5/31/03#"

    after txtrepname.text u have an &. After that &, you have to open a double quotes, a single quote thats all and the rest of thetext. no need to have the second double quote after the single quote...

    I hope u got it..

    Thanks,
    Aparna

  9. #9
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    what's db are you using?
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    Ok I see what you mean...and it did correct the error, but I am still not getting any results....

    This is what I have now:

    VB Code:
    1. Dim i as Integer
    2. Dim rs as Recordset
    3. Set rs = New Recordset
    4.  
    5. rs.Open "SELECT RepName FROM Main WHERE _
    6. RepName = '" & txtRepName.Text & "' AND DateRef BETWEEN #5/1/03# AND #5/31/03#"
    7. i=rs.RecordCount
    8. txtRefCount.Text = i
    9. rs.Close

    Any ideas????

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    Memnoch1207,

    I'm using Access as the database.

    Is that what you were asking?

  12. #12
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    dont break a VB line in the middle of a string...
    VB Code:
    1. thisworks = "This is some text " & _
    2.   "and some more text"
    3. thisdoesntwork = "This is some text _
    4. and some more text"
    use this:
    VB Code:
    1. rs.Open "SELECT RepName FROM Main WHERE " & _
    2.   "RepName = '" & txtRepName.Text & "' AND DateRef BETWEEN #5/1/03# AND #5/31/03#"
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    i did the break in this forum for the purpose of readability...

    In my project it is done correctly, but yet still not wrking properly!!

  14. #14
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    post code

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    BriantCVA.

    I posted the code earlier, but here it is again:



    vb
    VB Code:
    1. Dim i as Integer
    2. Dim rs as Recordset
    3. Set rs = New Recordset
    4.  
    5. rs.Open "SELECT RepName FROM Main WHERE _
    6. RepName = '" & txtRepName.Text & "' AND DateRef BETWEEN #5/1/03# AND #5/31/03#"
    7. i=rs.RecordCount
    8. txtRefCount.Text = i
    9. rs.Close

  16. #16
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    I don't see you implementing any of the above suggestions.
    Does this work?
    Code:
    rs.Open "SELECT RepName FROM Main WHERE RepName = '" & txtRepName.Text & "'"
    What about this?
    Code:
    rs.Open "SELECT RepName FROM Main WHERE RepName = '" & txtRepName.Text & "' AND DateRef BETWEEN #5/1/03# AND #5/31/03#"

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    Allright, I have implemented each suggestion, the follwoing code is exactly what I have in my VB project:

    VB Code:
    1. Dim i as Integer
    2. Dim rs as Recordset
    3. Set rs = New Recordset
    4.  
    5. rs.Open "SELECT RepName FROM Main WHERE RepName = '" & txtRepName.Text & "' AND DateRef BETWEEN #5/1/03# AND #5/31/03#", MyDataEnvironment.MyConnection
    6. i=rs.RecordCount
    7. txtRefCount.Text = i
    8. rs.Close
    I adjusted the double quote after the txtRepName to a single quote with (1) double quote...this was in accord to the suggestion made by apps_tech.

    Besides that suggestion, I have not received any others..

    I can not understand why it is not pulling the data...because if I seperate the conditions into (2) SQL SELECT statements, then I will receive accurate results, but when I combine it using the AND keyword, then I get nothing!

  18. #18
    Hyperactive Member
    Join Date
    May 2003
    Posts
    401
    did u try to execute ur sql statment in Access and see whether u are getting any results or not?

  19. #19
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    Does the query work (substututing a real RepName for the textbox value) in the query analyzer? That is, is the problem w/ your query or your vb?

  20. #20
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    oh boy... make a .MoveLast on the recordset and *then* try to read the .RecordCount property... that property doesnt get set as long as you stay on top.
    VB Code:
    1. Dim i as Integer
    2. Dim rs as Recordset
    3. Set rs = New Recordset
    4.  
    5. rs.Open "SELECT RepName FROM Main WHERE RepName = '" & txtRepName.Text & "' AND DateRef BETWEEN #5/1/03# AND #5/31/03#", MyDataEnvironment.MyConnection
    6. rs.MoveLast ' <-- this is what i added
    7. i=rs.RecordCount
    8. txtRefCount.Text = i
    9. rs.Close
    your recordset is actually full, but the .RecordCount is not set.
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  21. #21
    Hyperactive Member
    Join Date
    May 2003
    Posts
    401
    good catch radum.....that might help a bit in getting back ur results..

    cheers,
    aparna

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    Randum:

    I took your suggestion which by the way made perfect sense, but I am receiving a Run-time error ' 3021 - "Either BOF or EOF is true, or the current record has been deleted; the operation requested by the application requires a current record."

    Any ideas as to why this message? I have records in the table.

  23. #23

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    I checked to see if the sql statement would work when I subsituted the txtRepName.Text with an actual name of a rep from the table.....

    and the sql worked, but when I place the txtRepName.Text back, then I get no value!

    What does this mean, something wrong with the reference to the textbox value?

  24. #24
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    ... no idea... other than i would suggest to double-check the spelling of the text box's name... what is the name that you substituted in the sql statement? was it numeric?
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  25. #25
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    Could be.

    If you assign your SQL string to a variable and then run a debug.print statement, does it look correct? I.e., does it look the same as if you manually entered the repname in? Also, does the repname have an apostrophe in it?

  26. #26

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    No the Rep name does not have an apostraphe, and it is not numeric....

    It really does not make any sense!!

    It just does not want to read both conditions, and if it does it will either not read the contents of the textbox variable and give 0 value!

    Something is wrong with this SQL statement, even though it looks fine!

    Please guys do not give up on me...I will try the debug print suggestion.

  27. #27
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    you have a few guys here (me included) that scratched their heads and came with the most stupid ideas on how to fix it, just because all of them see the sql statement as being perfect. do me a favour - can you please replace the '#' signs around the dates with single quotes? i really do hope youre querying an access database, and not a ms sql or oracle. also, take off that 'BETWEEN' that i really hate and break it into 2 conditions:
    VB Code:
    1. SELECT RepName FROM Main WHERE RepName = '" & txtRepName.Text & "' AND DateRef > '5/1/03' AND DateRef < '5/31/03'"
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  28. #28
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    Access needs dates in #s so that really shouldn't work - and if it does I'm going to shoot myself. If radums suggestion doesn't work try keeping the double date conditions and swap the single quotes for #s and check that.

  29. #29

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    As you guys were scratching your head and providing some new suggestions, I revisited the SQL and thought that maybe the textbox was not giving the SQL the correct data...so I changed the code to read from a textbox on the main form...

    AND NOW IT WORKS WONDERFULLY!!!

    Thank you both Briantcva & randum!!

    You guys are excellent, keep up the great work

  30. #30
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    VB Code:
    1. Dim strSQL1 As String
    2. Dim strSQL2 As String
    3.  
    4. strSQL1 = "SELECT RepName FROM Main WHERE RepName = '" & txtRepName.Text & "'"
    5.  
    6. strSQL2 = strSQL1 & " AND DateRef BETWEEN #'5/1/03'# AND #'5/31/03'#"

    Jon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width