Page 1 of 2 12 LastLast
Results 1 to 40 of 43

Thread: [RESOLVED] More SQL Questions

  1. #1

    Thread Starter
    Lively Member jhize's Avatar
    Join Date
    Dec 2006
    Posts
    87

    Resolved [RESOLVED] More SQL Questions

    In this thread (http://www.vbforums.com/showthread.p...hlight=sql+sum) the following code was posted:

    Code:
    "SELECT type, SUM(amount) AS totals FROM history GROUP BY date, type HAVING date = '" & Combo1.Text & "' ORDER BY date"
    My question is simply this:

    Where does the "..As totals" come from? I need to do something similiar but I'm not sure about this portion of the above code. I need something like this, I think:

    Code:
    "SELECT SUM(Payment) FROM Paydate WHERE CommonPropertyName ='" & Propertyname & "'"
    Last edited by jhize; Nov 27th, 2007 at 01:32 PM.
    The question is not, Can I?
    The question is, How Do I?

    SQL Syntax | More SQL

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Another SQL Select SUM Question

    As Totals is naming the column Sum(amount). That is it, it's just a name
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Another SQL Select SUM Question

    Where does it come from?

    You make it up.

    Example:
    Code:
    SELECT SUM(Payment) AS MyPay FROM etc etc

  4. #4

    Thread Starter
    Lively Member jhize's Avatar
    Join Date
    Dec 2006
    Posts
    87

    Re: Another SQL Select SUM Question

    Ok. So it is not really needed? Or does it make 'totals a variable that I can use later (ex. Text1.text = totals?)

    Yeah, I'm thinking that was a stupid question. But it would be more stupid not asking. .....I think.
    The question is not, Can I?
    The question is, How Do I?

    SQL Syntax | More SQL

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Another SQL Select SUM Question

    It makes it easier to deal with the result as you can take what is stored and use it more easily.

  6. #6

    Thread Starter
    Lively Member jhize's Avatar
    Join Date
    Dec 2006
    Posts
    87

    Re: Another SQL Select SUM Question

    So 'totals' DOES become a variable that I can use?
    The question is not, Can I?
    The question is, How Do I?

    SQL Syntax | More SQL

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Another SQL Select SUM Question

    Not quite a variable, just the name of the field... in your original SQL there is a field called "type", so you can use it with something like rst.Fields("type").Value

    However, SUM(amount) is not a field (it is an expression), and what the 'field' would be called depends on the database system you are using - so it makes things harder. By specifying an alias ("as totals" or "AS MyPay"), you know what it will be called within the recordset (eg: rst.Fields("totals").Value ).
    Quote Originally Posted by jhize
    But it would be more stupid not asking. .....I think.
    Definitely.. we don't learn unless we ask questions, even if they may appear stupid.

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Another SQL Select SUM Question

    And this is not a stupid question.

    I would venture to say that each and every person that has posted in this thread had, at one time in their career, the exact same question. I know I did.

    Moreover, you will have no way of knowing this, but I'll be willing to bet that a lot of people will read your thread and go "Ohhhhhhhh....that is what that means."

  9. #9

    Thread Starter
    Lively Member jhize's Avatar
    Join Date
    Dec 2006
    Posts
    87

    Re: Another SQL Select SUM Question

    Ok. The first code I posted was not mine, the second was.

    I have a field called payment in a table called paydate. I am wanting to return the sum of payments only if the propertycommonname = text1.text on frmMain. I dont have a 'type' field.

    so....

    Code:
    SELECT Sum(payment) as Total FROM Paydate.....
    I believe I might be on the verge of understanding this here.

    On a side note, you guys are awesome !!!
    The question is not, Can I?
    The question is, How Do I?

    SQL Syntax | More SQL

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Another SQL Select SUM Question

    In that case, your SQL statement could be like this:
    Code:
    strSQL = "SELECT Sum(payment) as Total FROM Paydate WHERE propertycommonname = '" & frmMain.text1.text & "'"
    ..and once you have opened the recordset, you can get the value like this:
    Code:
    MsgBox rst.Fields("Total").Value
    ..or like this, which just gets the value of the first field:
    Code:
    MsgBox rst.Fields(0).Value
    (this method doesn't require the alias in the SQL statement, but is more prone to hard-to-find errors)

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Another SQL Select SUM Question

    Would be something like this:

    Code:
    "SELECT SUM(Payment) FROM Paydate WHERE CommonPropertyName ='" & frmmain.text1.text & "'"
    [edit]Or like Si just said!

    btw - that first query in your first post is not a good query. The HAVING clause should be a WHERE clause and the group by is flawed - bad example for you to learn from!
    [/edit]

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Another SQL Select SUM Question

    Assuming PropertyCommonName is a field in the table, just use a WHERE clause in your SELECT statement.
    Code:
    "SELECT Sum(payment) as Total FROM Paydate " & _
    "WHERE Paydate.PropertyCommonName = '" & Text1.Text & "'"
    although I'd probably put Text1.Text into a variable instead.
    Tengo mas preguntas que contestas

  13. #13

    Thread Starter
    Lively Member jhize's Avatar
    Join Date
    Dec 2006
    Posts
    87

    Re: Another SQL Select SUM Question

    Ok. The sql SUM works.

    Now how would I add a second condition in my where clause?

    After the propertyname variable, I need:
    WHERE TransactionType(a field) = TransactionType( a variable). I'm pretty sure I use AND but the punctuation is throwing me off.

    EDIT:
    I currently have this, but it's not right.
    Code:
     sqlTotal = "SELECT SUM(Amount) as Total FROM Transaction WHERE PropertyCommonName ='" & Propertyname & "'" AND "AccountType=' & TransactionType & "'"
    Last edited by jhize; Nov 27th, 2007 at 01:27 AM. Reason: Had to include code
    The question is not, Can I?
    The question is, How Do I?

    SQL Syntax | More SQL

  14. #14
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Another SQL Select SUM Question

    Your quotation marks and spacing are off.

    vb Code:
    1. sqlTotal = "SELECT SUM(Amount) as Total FROM Transaction " & _
    2. "WHERE PropertyCommonName = '" & Propertyname & "' " & _
    3. "AND AccountType = '" & TransactionType & "'"
    assuming AccountType is a text field. If numeric, drop the single quotes around TransactionType.
    Last edited by salvelinus; Nov 27th, 2007 at 06:36 AM.
    Tengo mas preguntas que contestas

  15. #15

    Thread Starter
    Lively Member jhize's Avatar
    Join Date
    Dec 2006
    Posts
    87

    Re: Another SQL Select SUM Question

    Thanks,

    Would you happen to have a good source that shows the 'Rules' of punctuation for these sql statements?

    I keep finding statements like this:
    Code:
    SELECT NAME_LAST, NAME_FIRST 
    FROM INDIVIDUAL 
    WHERE MARITAL_STATUS = 'Married' AND BIRTH_DT <= '1959-12-31';
    and I know that this is not what is needed inside a vb6 app. I'll check the tutorial again but I;m pretty sure this is not there.
    Last edited by jhize; Nov 27th, 2007 at 11:44 AM. Reason: typo
    The question is not, Can I?
    The question is, How Do I?

    SQL Syntax | More SQL

  16. #16
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Another SQL Select SUM Question

    I'm not precisely sure what you mean by "rules of punctuation", but strings need to be encapsulated in single quotes and numbers do not.

  17. #17
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Another SQL Select SUM Question

    It was not a SQL issue.

    You were not building the string properly - that's a VB syntax issue

    you had

    Code:
    & Propertyname & "'" AND "AccountType=' &
    That " quote in from of AccountType doesn't make any sense - as the " quote is a VB delimiter.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  18. #18

    Thread Starter
    Lively Member jhize's Avatar
    Join Date
    Dec 2006
    Posts
    87

    Re: Another SQL Select SUM Question

    OK. Um...I'm confused about the (') and (") delimiters.

    I understand this is a string. So, I understand this first line.
    Code:
    sqlTotal = "SELECT SUM(Amount) as Total FROM Transaction " & _
    I'm CORNfused right here (for example):
    Code:
     '" & Propertyname & "' "
    I understand that the last (") closes the string. However, & Propertyname & is enclosed by a (')(") and then in reverse (")(')...
    So I'm guessing that the (")......(") shows that it is a continuation of the string? and the (').....(') shows that it is not numerical.

    This is what I meant by Rules of Punctuation. In Post #15, this sql statement is "stand-alone?" meaning not assigned as a string? And if it were it would be something like this?

    Code:
    sqlquestion = "SELECT NAME_LAST, NAME_FIRST FROM INDIVIDUAL" & _ 
    "WHERE MARITAL_STATUS = '"& Married & "' & _
    "AND " & BIRTH_DT & " <= #1959-12-31#""
    Last edited by jhize; Nov 27th, 2007 at 12:22 PM.
    The question is not, Can I?
    The question is, How Do I?

    SQL Syntax | More SQL

  19. #19
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Another SQL Select SUM Question

    SQL wants character datatypes delimited by single quotes.

    So - Select * From SomeTable Where Status='XYZ' - is a valid sql statement. Note that I have single-quotes around the XYZ.

    Now if we want to put that into a VB string it would be

    strSQL = "Select * From SomeTable Where Status='XYZ'"

    Now this makes sense - right??

    Now the real issue - if the XYZ is going to come from a string in VB - let's say strStatus - then you need to build the string as:

    strSQL = "Select * From SomeTable Where Status='" & strStatus & "'"

    I simply replaced the XYZ with strStatus. Put it in with the & operator - which is string concatenation. Now I'm taking 2 fixed strings and putting them together with the strStatus variable.

    All along being careful to remember that SQL wants that XYZ value in single quotes.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  20. #20
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Another SQL Select SUM Question

    You're checking that the value of the field in your table named MARITAL_STATUS is equal to whatever value your string variable Married contains. Married might be S, W, D, M, etc. If you put Married within the double quotes like this:
    vb Code:
    1. "WHERE MARITAL_STATUS = 'Married' " & _
    you'll be checking that MARITAL_STATUS is equal to the actual word Married.
    This may be what you want, but I'd normally advise against doing that because then your query will be static - you won't be able to check for Single, Divorced, etc. But it will work.
    You can also use parameters for queries, but the specifics depend on your particular db.
    Tengo mas preguntas que contestas

  21. #21

    Thread Starter
    Lively Member jhize's Avatar
    Join Date
    Dec 2006
    Posts
    87

    Cool Re: More SQL Questions

    That answers my question perfectly. Plus I re-re-re-read the tutorial. Thank you.

    BTW, changed the title of Thread to better reflect the topic.
    The question is not, Can I?
    The question is, How Do I?

    SQL Syntax | More SQL

  22. #22
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Another SQL Select SUM Question

    Post #18 was fairly close jhize - you are missing an " at the end of the second line (before & _ ) and have an extra " at the end of the last line.

    For the actual VB string, simply read the amount of " characters up to that point (an odd number means you are in a string, an even number means you are not).

    Here is the corrected version with the actual VB strings highlighted in yellow:
    Code:
    sqlquestion = "SELECT NAME_LAST, NAME_FIRST FROM INDIVIDUAL" & _ 
    "WHERE MARITAL_STATUS = '" & Married & "'" & _
    "AND " & BIRTH_DT & " <= '1959-12-31'"
    The strings within the SQL statement are indicated with ' as explained by the guys above. As it can be hard to read the mixture of VB and SQL strings (and append the values of the variables), I'd recommend using a little trick that most of us use - once you have the VB string working (the " characters match) don't try to build up the final SQL statement in your head, simply print it to the immediate window, by using: Debug.Print sqlquestion


    One thing that may have confused you (it confuses others!) is the line continuation in VB, which is simply the character _ after a space, and the code continues on the next line.

    The & character joins two strings, so by using & _ you are simply joining a string on one line to the string on the next (just like using "a" & "b" on the same line).

  23. #23

    Thread Starter
    Lively Member jhize's Avatar
    Join Date
    Dec 2006
    Posts
    87

    Thumbs up Re: More SQL Questions

    YES !I get it! I Understand! WooHoo!

    I dont know how you guys are able to answer all of these questions (especially duplicates) but.....WOW! I am so thoroughly impressed. I truly hope I get to where you are one day. Thanks again.
    The question is not, Can I?
    The question is, How Do I?

    SQL Syntax | More SQL

  24. #24
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: [RESOLVED] More SQL Questions

    One thing to note in si's last post is the single quotes surrounding the date. This is correct if your date is actually a string datatype, or if it's a date datatype in a SQL Server or MySql database, but if it's a date datatype in Access, use # signs rather than single quotes, as you indicate in post 18.
    Dates can be confusing (lots of threads lately). There's a good FAQ in the FAQ link at the top of this forum, if you run into problems there.
    Tengo mas preguntas que contestas

  25. #25

    Thread Starter
    Lively Member jhize's Avatar
    Join Date
    Dec 2006
    Posts
    87

    Re: [RESOLVED] More SQL Questions

    Yeah, it's me again.
    I'm having a bit of trouble with sql statement where I'm looking for a date (Today).

    Assuming that the connection is already established and the recordset is already defined - where am I going wrong here?

    The field "Date" is in a table called Transactions, it is a date/time data type.
    Mm/dd/yyyy format.

    I am trying to set a variable that will always be the current date.
    Code:
    Dim Tdate as date
    Tdate = format(Date, "mm/dd/yyyy")
    Now, I know that's probably wrong, so I'll continue...

    Code:
     sql = "SELECT Note FROM Transactions" & _
    "WHERE Date=" ......
    I have tried the (='" & Tdate & "'") approach. No go. I have tried dropping the (') and replacing with (#). Still no go.

    What is it that I am doing wrong here?
    The question is not, Can I?
    The question is, How Do I?

    SQL Syntax | More SQL

  26. #26
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] More SQL Questions

    What is your backend DB?

    Datatime fields can also store a time component. Do you have "times" in those fields as well as dates??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  27. #27
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] More SQL Questions

    There are two problems there... first of all, this is an extremely bad idea, as it gives your program a guaranteed bug:
    Quote Originally Posted by jhize
    Code:
    Dim Tdate as date
    Tdate = format(Date, "mm/dd/yyyy")
    That second line should use just the Date function (which returns the current date) by itself, as using Format converts it into a formatted String - which then needs to be converted back to a Date data type so that it can be stored in your variable, a process that is very prone to errors (you will often find the mm/dd parts swapped around - but not always!). You can then use Format while appending it to your SQL string.

    For more information on why that happens, see the article Why are my dates not working properly from our Classic VB FAQs (in the FAQ forum, which is shown near the top of our home page)


    If you had actually declared Tdate as a String (to hold the String that Format returns), that code would be fine - assuming that the database system you are using wants dates to be specified in that format (most do).

    You can see the usual formats wanted (and a reminder to never store the results of the Format function to a Date) in the article How do I use values (numbers, strings, dates) in SQL statements from our Database Development FAQs/Tutorials (at the top of this forum)


    The other issue is here:
    Code:
     sql = "SELECT Note FROM Transactions" & _
    "WHERE Date=" ......
    Having a field called Date is asking for problems, as it is a Reserved Word, and so will cause many problems in your queries. If you have any choice at all, change the name of that field - it will make working with this much easier.



    If you need any more assistance after this, it would be a very good idea to answer szlamany's questions.

  28. #28

    Thread Starter
    Lively Member jhize's Avatar
    Join Date
    Dec 2006
    Posts
    87

    Re: [RESOLVED] More SQL Questions

    I must have missed that tutorial. I'll get on it asap. As far as my backend db it's Access 2003 and there is no time component just mm/dd/yyyy.

    I will be changing the db field to Tdate

    and as far as I understand, I'll be doing something like this:

    Code:
    dim Tdate as Date
    Tdate = Date
    Dim sql as String
    sql = "Select Note From Transactions Where Tdate = '" & _
    "Format(Date,"mm/dd/yyyy") & "'"
    This will look for any entries in the Transaction Table for Today's date. Moving on...

    I will be needing to incorporate Text fields that hold a user entered date. The values of which will become variables. I will need to run a query for Transaction BETWEEN Date1 and Date2.

    I do see this at http://www.vbforums.com/showthread.php?t=489286 and this should help quite a bit in getting the user input date to a textbox and then to a variable:

    Code:
    'This example is assumes that the text was entered in the format of mm/dd/yyyy
    Dim MyDate as Date
    Dim TempArray() as String
    
        'Separate the items by the delimiter (in this example, "/") 
      TempArray() = Split(Text1.Text, "/")
    
        'Note: you should check here that the values are valid 
        '(such as the month is a whole number, between 1 and 12)
    
        'Place each item in the relevant part of DateSerial to build the date
      MyDate = DateSerial(TempArray(2), TempArray(0), TempArray(1))
    The question is not, Can I?
    The question is, How Do I?

    SQL Syntax | More SQL

  29. #29
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] More SQL Questions

    That sounds OK, apart from using a Textbox to input a Date - as you are making assumptions about the way the user formats the date, and it isn't as easy for the user to use (or for you to validate) as a proper date based control, like the DateTimePicker.

    For more information on that, see the article What controls can I use to input a date/time?

  30. #30
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: [RESOLVED] More SQL Questions

    Also, if you're using Access, Date datatypes are surrounded by # signs, similar to the way strings are surrounded by single quotes. But if your date is not a Date datatype but rather a string, back to single quotes.
    Tengo mas preguntas que contestas

  31. #31

    Thread Starter
    Lively Member jhize's Avatar
    Join Date
    Dec 2006
    Posts
    87

    Re: [RESOLVED] More SQL Questions

    WoW! Even with the tutorial and your help I struggled on this one BUT! I got it.

    Code:
    Dim sql As String
    Dim Tdate As Date
    Tdate = Date
    sql = "Select Note From Transactions Where Tdate = #" & Tdate & "#"
    rst.Open sql, 
    Note.Text = rst!Note
    So Next on my list is attempting a sql BETWEEN.

    By the way, I use a datepicker (Calendar1) to populate textboxes formatted the way I want them (not the way the user wants it). So far, it works good.

    AND ! How do you Highlight a piece of code in this forum? I've seen it before and can't find it now.
    The question is not, Can I?
    The question is, How Do I?

    SQL Syntax | More SQL

  32. #32
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] More SQL Questions

    That isn't quite right I'm afraid.. while you can't store a Formatted date back into a Date variable, you need to use a Formatted date within an SQL statement - as shown in the "How do I use values" article.

    This means you should have something like this:
    Code:
    Dim Tdate As String
    Tdate = format(Date, "mm/dd/yyyy")
    sql = "Select Note From Transactions Where Tdate = #" & Tdate & "#"
    or this:
    Code:
    Dim Tdate As Date
    Tdate = Date
    sql = "Select Note From Transactions Where Tdate = #" & format(Tdate, "mm/dd/yyyy") & "#"
    By the way, I use a datepicker (Calendar1) to populate textboxes formatted the way I want them (not the way the user wants it). So far, it works good.
    Why do you want to put a Date value into a TextBox (as a string), rather than keep it in the DateTimePicker (as a date)?
    AND ! How do you Highlight a piece of code in this forum? I've seen it before and can't find it now.
    That depends what you mean by highlight.. if you mean the yellow background like I used in post #22, it is [hl] text here [/hl]

  33. #33

    Thread Starter
    Lively Member jhize's Avatar
    Join Date
    Dec 2006
    Posts
    87

    Re: [RESOLVED] More SQL Questions

    Thanks Si,

    The code I posted worked so I dodnt see a problem with using it. HOWEVER, I changed it to your first example and that works too. So! I'll be using yours. Thanks Again.
    The question is not, Can I?
    The question is, How Do I?

    SQL Syntax | More SQL

  34. #34
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] More SQL Questions

    The problem with your version is that it needs luck - it will only work properly if the computer your program is running on is set to use the US (or ISO) styles for the short-date format (the implicit CStr in yours uses the PC's short-date format).

    By explicitly formatting the string that is created from the date, you can be sure that it will work, no matter what the PC settings are.

  35. #35
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] More SQL Questions

    You had this:

    Code:
    Dim Tdate As Date
    Tdate = Date
    sql = "Select Note From Transactions Where Tdate = #" & Tdate & "#"
    Problem I see is that TDate is a datatype of Date.

    And you are concatenating it with a string into a string variable.

    That is bad form.

    Si's suggestion to use format is the proper way to turn a datatype of Date into a string.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  36. #36

    Thread Starter
    Lively Member jhize's Avatar
    Join Date
    Dec 2006
    Posts
    87

    Re: [RESOLVED] More SQL Questions

    And Now, that makes perfect sense. I knew there was a reason you suggested your code in favor of mine. Thanks a million to you both.
    The question is not, Can I?
    The question is, How Do I?

    SQL Syntax | More SQL

  37. #37

    Thread Starter
    Lively Member jhize's Avatar
    Join Date
    Dec 2006
    Posts
    87

    Re: [RESOLVED] More SQL Questions

    OK, the above code works perfectly. However, If the sql finds no match it returns an error "Either EOF or BOF is flase or the record has been deleted". I think that is the way it's worded.

    The problem is....There will be times when the sql will find no matches for today's date. I added an error handler but that did not work they way I expected.

    How would you code say, if there are no matches then return a msgbox?
    Something like this but I know this is wrong.
    Code:
    ...
    rst.Open sql, 
    if rst!Note <> "" then
    Note.Text = rst!Note
    else
    Msgbox "No Match."
    end if
    The highlighted code is where Im having the most difficulty. How can I capture if the field is empty?
    The question is not, Can I?
    The question is, How Do I?

    SQL Syntax | More SQL

  38. #38
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] More SQL Questions

    First off, to see if there are any records, check the EOF property just after opening the recordset (and BOF too if you do it later), eg:
    Code:
    rst.Open sql, ...
    If rst.EOF Then   'or:  If (rst.BOF And rst.EOF) Then
      Msgbox "no records"
    Else
      '... code to work with the records
      rst.Close
    End If
    Set rst = Nothing
    You should perform this kind of check every time you open a recordset, as otherwise you are likely to get the error you saw.


    Note that you don't want to "capture if the field is empty", as the problem is that there are no records - and thus no fields (each record contains one or more fields).

    Just for info... To check if a field is empty, it depends on what you mean by empty (an empty string? Null? either?). These are the usual methods:
    Code:
    if rst.Fields("Note").Value <> "" then  'check if it is an empty string
    if IsNull(rst.Fields("Note").Value) then  'check if it is Null
    if rst.Fields("Note").Value & "" <> "" then  'check if it is an empty string or Null
    Last edited by si_the_geek; Dec 14th, 2007 at 06:24 PM. Reason: (typo!)

  39. #39
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] More SQL Questions

    Si - I'm guessing that this is a typo

    if rst.Fields("Note").Value & "" then 'check if it is an empty string or Null

    should be

    if rst.Fields("Note").Value & "" <> "" then 'check if it is an empty string or Null

    Right?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  40. #40
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] More SQL Questions

    That's right.

    Thanks for pointing it out.

Page 1 of 2 12 LastLast

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