Query returning value in Access query builder but null in VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iheartvba
    New Member
    • Apr 2007
    • 171

    #1

    Query returning value in Access query builder but null in VBA

    Hi I am running the following query in access query builder. The name of the query is "qryTempRecEFTB ankedMaxDate":

    Code:
    SELECT Max(qryTempRecEFTBanked.Dt) AS MaxOfDt
    FROM qryTempRecEFTBanked;
    it returns a date which exactly what I wanted, but when I run this code in VBA:

    Code:
    strSqlMaxDate = "qryTempRecEFTBankedMaxDate"
        rst.Open strSqlMaxDate, cnn, adOpenDynamic, adLockOptimistic
        With rst
        If rst.EOF And rst.BOF Then MsgBox "The recordset is blank"
       dtBankMax = !MaxOfDt
        End With
    !MaxOfDt returns as Null, even though rst.EOF = false and also rst.BOF = False. I don't know whether it helps but "qryTempRecEFTB ankedMaxDate" is based on the query "qryTempRecEFTB anked" the Sql for "qryTempRecEFTB anked" is as follows:
    Code:
    SELECT Sum(tblBank.Amount) AS SumOfAmount, tblBank.Dt, Val(Right(nz([SerialID],0),1)) AS ConsID, nZ([RecID],0) AS ReconID, tblBank.Desc
    FROM tblBank LEFT JOIN tblReconciled ON tblBank.BankID = tblReconciled.BankID
    GROUP BY tblBank.Dt, Val(Right(nz([SerialID],0),1)), nZ([RecID],0), tblBank.Desc
    HAVING (((Sum(tblBank.Amount))<>0) AND ((tblBank.Dt)>#6/30/2008#) AND ((Val(Right(nz([SerialID],0),1)))=4 Or (Val(Right(nz([SerialID],0),1)))=5 Or (Val(Right(nz([SerialID],0),1)))=7) AND ((nZ([RecID],0))=0) AND ((tblBank.Desc) Like "*HANDYWAY*"))
    ORDER BY tblBank.Dt;

    Many Thanks
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32669

    #2
    Can't see much wrong there :(

    Check out the rst.Open (line #2) for correct parameters, and make sure that rst is defined as DAO.Recordset if you need that or ADODB.Recordset otherwise.

    It could simply be the .Open line not working as expected. Check it out in the debugger (Debugging in VBA).

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Originally posted by iheartvba
      Hi I am running the following query in access query builder. The name of the query is "qryTempRecEFTB ankedMaxDate":

      Code:
      SELECT Max(qryTempRecEFTBanked.Dt) AS MaxOfDt
      FROM qryTempRecEFTBanked;
      it returns a date which exactly what I wanted, but when I run this code in VBA:

      Code:
      strSqlMaxDate = "qryTempRecEFTBankedMaxDate"
          rst.Open strSqlMaxDate, cnn, adOpenDynamic, adLockOptimistic
          With rst
          If rst.EOF And rst.BOF Then MsgBox "The recordset is blank"
         dtBankMax = !MaxOfDt
          End With
      !MaxOfDt returns as Null, even though rst.EOF = false and also rst.BOF = False. I don't know whether it helps but "qryTempRecEFTB ankedMaxDate" is based on the query "qryTempRecEFTB anked" the Sql for "qryTempRecEFTB anked" is as follows:
      Code:
      SELECT Sum(tblBank.Amount) AS SumOfAmount, tblBank.Dt, Val(Right(nz([SerialID],0),1)) AS ConsID, nZ([RecID],0) AS ReconID, tblBank.Desc
      FROM tblBank LEFT JOIN tblReconciled ON tblBank.BankID = tblReconciled.BankID
      GROUP BY tblBank.Dt, Val(Right(nz([SerialID],0),1)), nZ([RecID],0), tblBank.Desc
      HAVING (((Sum(tblBank.Amount))<>0) AND ((tblBank.Dt)>#6/30/2008#) AND ((Val(Right(nz([SerialID],0),1)))=4 Or (Val(Right(nz([SerialID],0),1)))=5 Or (Val(Right(nz([SerialID],0),1)))=7) AND ((nZ([RecID],0))=0) AND ((tblBank.Desc) Like "*HANDYWAY*"))
      ORDER BY tblBank.Dt;

      Many Thanks
      Try:
      Code:
      Dim strSqlMazdate As String
      Dim rst As ADODB.Recordset
      Dim cnn As ADODB.Connection
      
      Set rst = New ADODB.Recordset
      Set cnn = CurrentProject.Connection
      
      strSqlMaxDate = "qryTempRecEFTBankedMaxDate"
      
      With rst
        .Open strSqlMaxDate, cnn, adOpenDynamic, adLockOptimistic
          If .BOF And .EOF Then
            MsgBox "The recordset is blank"
          Else
            MsgBox !MaxOfDt
        End If
      End With
      
      rst.Close
      Set rst = Nothing

      Comment

      • iheartvba
        New Member
        • Apr 2007
        • 171

        #4
        I have already tried these suggetions

        I have tried all of the above suggetions. Here are some snipets of my code (if the following code is run in my opinion the code should still work)

        Code:
        Public cnn As ADODB.Connection
        Public rst As New ADODB.Recordset
        Private Sub cmdMaxDate_Click()
        Set cnn = CurrentProject.Connection
        Dim strSqlMaxDate As String
        strSqlMaxDate = "qryTempRecEFTBankedMaxDate"
            rst.Open strSqlMaxDate, cnn, adOpenDynamic, adLockOptimistic
            With rst
            If rst.EOF And rst.BOF Then MsgBox "Empty Recordset"
            dtBankMax = !maxofdt
            End With
        End Sub
        Last edited by iheartvba; Jan 7 '09, 03:51 AM. Reason: Add End Sub

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Originally posted by iheartvba
          I have tried all of the above suggetions. Here are some snipets of my code (if the following code is run in my opinion the code should still work)

          Code:
          Public cnn As ADODB.Connection
          Public rst As New ADODB.Recordset
          Private Sub cmdMaxDate_Click()
          Set cnn = CurrentProject.Connection
          Dim strSqlMaxDate As String
          strSqlMaxDate = "qryTempRecEFTBankedMaxDate"
              rst.Open strSqlMaxDate, cnn, adOpenDynamic, adLockOptimistic
              With rst
              If rst.EOF And rst.BOF Then MsgBox "Empty Recordset"
              dtBankMax = !maxofdt
              End With
          End Sub
          Code:
          If rst.EOF And rst.BOF Then MsgBox "Empty Recordset"
            'You are attempting to retrieve a Field from a Field in an
            'Empty Recordset, move to Else Clause
          Else
            'You've assigned the Value of !maxofdt to dtBankMax, but where
            'is it Declared, and how is it displayed/utilized?
            dtBankMax = !maxofdt
              MsgBox dtBankMax        'Verify?
          End If

          Comment

          • FishVal
            Recognized Expert Specialist
            • Jun 2007
            • 2656

            #6
            Just a thought.

            Could it be synchronization problem?
            Did you try to use MoveLast method to force record fetch in recordset object?
            Did you try to break the code execution after recordset has been opened and:
            a) run code in step mode?
            b) inspect rst variable in Watch window?

            Regards,
            Fish.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32669

              #7
              Originally posted by NeoPa
              Can't see much wrong there :(

              Check out the rst.Open (line #2) for correct parameters, and make sure that rst is defined as DAO.Recordset if you need that or ADODB.Recordset otherwise.

              It could simply be the .Open line not working as expected. Check it out in the debugger (Debugging in VBA).
              I don't know if you overlooked my earlier post, but checking up from work (where I now have easier access to the information required) it seems that the Open method is a specifically ADODB.Recordset method. This means that unless your rst variable has been defined as ADODB.Recordset (We can't tell as you haven't shared this information) this code will not work (as Access uses DAO generally by default - at least in the older versions). DAO & ADODB do behave differently. ADODB will have different characters for wildcards for instance. This will behave in a different way from the way you would expect Access to behave (For the standard Access ways use DAO).

              When I checked the Help system for the Open method of an ADODB recordset the first parameter (Source) did not list a QueryDef as a possible value. Do you have any reason to suppose this is working as you intend?

              Comment

              • iheartvba
                New Member
                • Apr 2007
                • 171

                #8
                Hi
                Okay there are 2 posts I would like to attention Post 7 and Post 6:

                Post 7:
                "...This means that unless your rst variable has been defined as ADODB.Recordset (We can't tell as you haven't shared this information)... "

                A: Please see line 2 of the code in Post 4, it shows that the rst variable has been defined as New ADODB.Recordset .

                Post 6:
                Q:Could it be synchronization problem?
                Did you try to use MoveLast method to force record fetch in recordset object?

                A: I used the query which "qryTempRecEFTB ankedMaxDate" is based on. The name of the query I used is "qryTempRecEFTB anked" (see post 1 for the Sql) but even before I could use the .movelast function, it was coming up as an empty recordset I.E. rst.bof =True and rst.eof =True.

                Then I tried to use the actual Sql code but it is giving me the error "Query does not include xxx as part of an aggregate function"
                My code is as follows:
                Code:
                Public cnn As ADODB.Connection
                Public rst As New ADODB.Recordset
                Private Sub cmdMaxDate_Click()
                 strSqlMaxDate = "SELECT Sum(tblBank.Amount) AS SumOfAmount, tblBank.Dt, " & _
                "Val(Right(nz([SerialID],0),1)) AS ConsID, nZ([RecID],0) AS ReconID, tblBank.Desc " & _
                "FROM tblBank LEFT JOIN tblReconciled ON tblBank.BankID=tblReconciled.BankID " & _
                "GROUP BY tblBank.Dt, Val(Right(nz([SerialID],0),1)), nZ([RecID],0), tblBank.Desc " & _
                "HAVING ((((Sum(tblBank.Amount)) <> 0) And ((tblBank.Dt) > #6/30/2008#) And " & _ 
                "((Val(Right(nZ([SerialID], 0), 1))) = 4 Or (Val(Right(nZ([SerialID], 0), 1))) = 5 " & _
                "Or (Val(Right(nZ([SerialID], 0), 1))) = 7) And ((nZ([RecID], 0))=0) And ((tblBank.Desc) Like '* HANDYWAY *'))) " & _
                "ORDER BY tblBank.Dt;"
                    rst.Open strSqlMaxDate, cnn, adOpenDynamic, adLockOptimistic
                    With rst
                        If rst.EOF And rst.BOF Then
                        MsgBox "empty recordset"
                        Else
                        dtBankMax = !Dt
                        MsgBox dtBankMax
                        End If
                    End With
                End Sub

                Comment

                • ADezii
                  Recognized Expert Expert
                  • Apr 2006
                  • 8834

                  #9
                  Would it be possible to E-Mail a subset of the Database as an Attachment where we can visually see what is going on? The data would not have to be real, just representative.

                  Comment

                  • iheartvba
                    New Member
                    • Apr 2007
                    • 171

                    #10
                    yes whats your e-mail address?

                    Comment

                    • iheartvba
                      New Member
                      • Apr 2007
                      • 171

                      #11
                      I have found the problem. It doesn't like it when I filter Desc by Like "*HandyWay* ". Thats it, everything else is fine. But I need to have that Filter. :S

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32669

                        #12
                        Originally posted by iheartvba
                        Hi
                        Okay there are 2 posts I would like to attention Post 7 and Post 6:

                        Post 7:
                        "...This means that unless your rst variable has been defined as ADODB.Recordset (We can't tell as you haven't shared this information)... "
                        A: Please see line 2 of the code in Post 4, it shows that the rst variable has been defined as New ADODB.Recordset .
                        I apologise. Clearly it was there by the time I posted if I'd read through your post more carefully. I'd seen that you hadn't replied to my post and skimmed quickly through the other posts. It's actually quite time-consuming trying to work in the absence of direct responses as it's never clear where you are.

                        You may have picked up in my earlier response (post #7) also that one of the things to watch out for when using ADODB (rather than DAO) is the difference of the wildcard characters (which appears to be at the heart of your problem). Check out ANSI Standards in String Comparisons for help with that.

                        Comment

                        • iheartvba
                          New Member
                          • Apr 2007
                          • 171

                          #13
                          use % instead of * as Wild card in ADO

                          Sorry about not replying directly NeoPa, I now understand how it can cause some confustion. I will try to be more carefull next time.

                          You were correct in Post 7 about the wildcards, excuse me for missing that. The Correct code is as follows:

                          Code:
                          Public cnn As ADODB.Connection
                          Public rst As New ADODB.Recordset
                          Private Sub cmdMaxDate_Click() 
                          Dim strSqlMaxDate As String
                          Dim strSqlBank As String
                          strSqlBank = "(SELECT Sum(tblBank.Amount) AS BankedAmount, tblBank.Dt, " & _
                          "Val(Right(nz([SerialID],0),1)) AS ConsID, nZ([RecID],0) AS ReconID, tblBank.Desc " & _
                          "FROM tblBank LEFT JOIN tblReconciled ON tblBank.BankID=tblReconciled.BankID " & _
                          "GROUP BY tblBank.Dt, Val(Right(nz([SerialID],0),1)), nZ([RecID],0), tblBank.Desc, tblBank.Amount)"
                          strSqlMaxDate = " SELECT BankedAmount, Dt, ConsID, ReconID, Desc " & _
                          "FROM " & strSqlBank & " " & _
                          "WHERE BankedAmount <>0 And Dt > #6/30/2008# And ConsID = 4 Or ConsID = 5 or ConsID = 7 " & _
                          "And ReconID = 0 And Desc Like '% HANDYWAY %' " & _
                          "ORDER BY Dt"
                          rst.Open strSqlMaxDate, cnn, adOpenDynamic, adLockOptimistic
                              With rst
                                  dtBankMax = !Dt
                              End With
                          End Sub

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32669

                            #14
                            No worries :)

                            I'm pleased that you got it sorted, and we always appreciate that you post your solutions when you find them. It makes the whole process work so much better for any others that come along later with similar problems. Good for you.

                            PS. I assume that going from "*HANDYWAY* " (No spaces) to "% HANDYWAY %" (Spaces) was a deliberate choice on your part. If not I'm sure that you'll appreciate it's a little different that way.

                            Comment

                            • iheartvba
                              New Member
                              • Apr 2007
                              • 171

                              #15
                              No that is by accident, I't won't make a difference, but thanks for the pick up.
                              Last edited by iheartvba; Jan 9 '09, 03:31 AM. Reason: add text

                              Comment

                              Working...