Urgent

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shailja
    New Member
    • Feb 2007
    • 123

    Urgent

    I have written a query sql = "SELECT Max(No) FROM Temp" in recordset using
    rs.open sql,cn,,,adcmdT ext. How to fetch the result of a recordet. I get wrong answer when i execute following code:

    Code:
    Dim i as Long
    
    sql = ""SELECT Max(No) FROM Temp""
    rs.open sql, cn,,,adcmdText
    i = rs(0)
    MsgBox(i)
    Answer i get is 0 but it should come 4.

    Plz tell me correct code
    Last edited by willakawill; Feb 22 '07, 05:30 PM. Reason: please use code tags when posting code
  • vijaydiwakar
    Contributor
    • Feb 2007
    • 579

    #2
    Originally posted by Shailja
    I have written a query sql = "SELECT Max(No) FROM Temp" in recordset using
    rs.open sql,cn,,,adcmdT ext. How to fetch the result of a recordet. I get wrong answer when i execute following code:

    Dim i as Long

    sql = ""SELECT Max(No) FROM Temp""
    rs.open sql, cn,,,adcmdText
    i = rs(0)
    MsgBox(i)

    Answer i get is 0 but it should come 4.

    Plz tell me correct code
    Hi, Shailaja
    Try this
    Select nvl(max(no),0) from temp -> if ur using oracle
    try to use adcmdDynamic

    Comment

    • Shailja
      New Member
      • Feb 2007
      • 123

      #3
      Originally posted by vijaydiwakar
      Hi, Shailaja
      Try this
      Select nvl(max(no),0) from temp -> if ur using oracle
      try to use adcmdDynamic
      I
      I am not using ORACLE, My Back End is Access.

      Comment

      • hariharanmca
        Top Contributor
        • Dec 2006
        • 1977

        #4
        Originally posted by Shailja
        I
        I am not using ORACLE, My Back End is Access.

        Code:
        sql = ""SELECT Max(No) FROM Temp""
        rs.open sql, cn,,,adcmdText
        i = rs(0)
        MsgBox(i)
        
        for access qry is correct,... Ok just try this
        
        sql = ""SELECT Max(No) FROM Temp""
        rs.open sql, cn,,,adcmdText
        i = rs.field(0)
        or
        i = rs.field(0).value
        MsgBox(i)
        Last edited by willakawill; Feb 22 '07, 05:30 PM. Reason: please use code tags when posting code

        Comment

        • vijaydiwakar
          Contributor
          • Feb 2007
          • 579

          #5
          Originally posted by Shailja
          I
          I am not using ORACLE, My Back End is Access.
          Would u pls give me the tbl structure along with some data and the version of msaccess

          Comment

          • Shailja
            New Member
            • Feb 2007
            • 123

            #6
            Originally posted by vijaydiwakar
            Would u pls give me the tbl structure along with some data and the version of msaccess
            Table: Temp

            No Name
            1 A
            2 B
            3 C

            Comment

            • Shailja
              New Member
              • Feb 2007
              • 123

              #7
              Originally posted by hariharanmca
              sql = ""SELECT Max(No) FROM Temp""
              rs.open sql, cn,,,adcmdText
              i = rs(0)
              MsgBox(i)

              for access qry is correct,... Ok just try this

              sql = ""SELECT Max(No) FROM Temp""
              rs.open sql, cn,,,adcmdText
              i = rs.field(0)
              or
              i = rs.field(0).val ue
              MsgBox(i)
              I have tried both the above code , u have given. But still getting 0 answer.

              Comment

              • vijaydiwakar
                Contributor
                • Feb 2007
                • 579

                #8
                Originally posted by Shailja
                Table: Temp

                No Name
                1 A
                2 B
                3 C
                Shailaja there are so many problems in msaccess, ur facing one of them right now
                the sol is just change the column name from no to myno or what evr u like
                and run the qry
                alsom change the name field name if u will use date then donot use ddate or date as column name
                by

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Personally, I think the problem might be the extra quotes around the SQL. To quote your original code...
                  Code:
                  Dim i as Long
                  
                  sql = "[U]"[/U]SELECT Max(No) FROM Temp[U]"[/U]"
                  rs.open sql, cn,,,adcmdText
                  i = rs(0)
                  MsgBox(i)
                  What about trying this...
                  Code:
                  Dim i as Long
                  
                  sql = "SELECT Max(No) FROM Temp;"
                  rs.open sql, cn,,,adcmdText
                  i = rs(0)
                  MsgBox(i)

                  Comment

                  • vijaydiwakar
                    Contributor
                    • Feb 2007
                    • 579

                    #10
                    Originally posted by Killer42
                    Personally, I think the problem might be the extra quotes around the SQL. To quote your original code...
                    Code:
                    Dim i as Long
                    
                    sql = "[U]"[/U]SELECT Max(No) FROM Temp[U]"[/U]"
                    rs.open sql, cn,,,adcmdText
                    i = rs(0)
                    MsgBox(i)
                    What about trying this...
                    Code:
                    Dim i as Long
                    
                    sql = "SELECT Max(No) FROM Temp;"
                    rs.open sql, cn,,,adcmdText
                    i = rs(0)
                    MsgBox(i)
                    No Boss the problem will be get solved by the suggetion i'v given just try it out
                    bye

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by vijaydiwakar
                      No Boss the problem will be get solved by the suggetion i'v given just try it out
                      No, pick me! :D

                      Seriously, though. Are you thinking fieldname "No" is being interpreted in the context of Yes/No? Have to watch those reserved words, huh.

                      Comment

                      • hariharanmca
                        Top Contributor
                        • Dec 2006
                        • 1977

                        #12
                        Originally posted by Killer42
                        No, pick me! :D

                        Seriously, though. Are you thinking fieldname "No" is being interpreted in the context of Yes/No? Have to watch those reserved words, huh.
                        Yes in MS Access 'No' is reserved word, just we have to try some other

                        Comment

                        • vijaydiwakar
                          Contributor
                          • Feb 2007
                          • 579

                          #13
                          Originally posted by Killer42
                          No, pick me! :D

                          Seriously, though. Are you thinking fieldname "No" is being interpreted in the context of Yes/No? Have to watch those reserved words, huh.
                          Just do the thing i sugested and u will get the ans itself :D

                          Comment

                          • willakawill
                            Top Contributor
                            • Oct 2006
                            • 1646

                            #14
                            To avoid problems with reserved words in sql it is always good practise to write your statements out in full including square brackets, no shortcuts and no fingers crossed :)

                            Code:
                            Dim i As Long
                            Dim sql As String
                            
                            sql = "SELECT Max([Temp].[No]) AS MaxNo FROM [Temp]"
                            rs.open sql, cn,,,adcmdText
                            i = rs("MaxNo")
                            MsgBox("The max no is: " & i, "Maximum Number")

                            Comment

                            Working...