Results 1 to 8 of 8
  1. #1
    faythe1215 is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    52

    Accessing, storing, and using data in query fields


    I'm sure this is simpler than I'm making it out to be...

    I have a query a couple of calculated values. I am trying to iterate through the entire query one record at a time. I need to access various bits of the information stored in the query, but I can't seem to get it. I've looked at tutorials on Recordsets and DLookup, but I can't figure it out.

    If I use DLookup, I don't know how to move through the records, but if I use Recordsets, I can't figure out how to access the information in the various fields.

    Query Example
    CountOfItem_ID*
    ItemName
    MeasurementType_ID
    AvgOfRetailPrice*
    3 Box 1 1.99
    1 Coupler 2 .99
    *calculated fields - (idk if that matters, though)

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,633
    Access the bits of information to do what with them?

    Open recordset in VBA then in loop structure, cycle through the records.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    faythe1215 is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    52
    I need to store the info in CountOfItem_ID and MeasurementType_ID as integer variables.

    So, as I understand it, I'm doing this:

    Code:
    Dim CountItem as Integer
    Dim MeasureType as Integer
    
    Dim rs as Recordset
    Set rs = CurrentDb.OpenRecordset "[queryName]"
    
    rs.MoveFirst
    
    While rs.EOF = false
    '    CountItem = (this is where I get stuck, because I can't figure out how to get that info from the field)
    '    MeasureType = 
    rs.MoveNext
    Wend

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I need to store the info
    Store it where?

    Here is an example of ONE way to add the data to a table:
    Code:
    '  Dim CountItem as Integer
    '  Dim MeasureType as Integer
    Dim sSQL As String
    Dim rs as DAO.Recordset
    Dim rt as DAO.Recordset
    
    Set rs = CurrentDb.OpenRecordset ("[queryName]")  'source data
    Set rt = CurrentDb.OpenRecordset ("Metrics")  'target table name 
    
    If Not rs.EOF Then
    rs.MoveFirst
    
    While rs.EOF = false
       rt.Add
       rt!CountItem = rs.CountOfItem_ID
       rt!MeasureType = rs.MeasurementType_ID
       rt.Update
    
    '    CountItem = (this is where I get stuck, because I can't figure out how to get that info from the field)
    '    MeasureType = 
    
    rs.MoveNext
    Wend
    
    End If
    
    'Clean up
    rs.Close
    rt.Close
    Set rs = Nothing
    Set rt = Nothing
    Another method is to create (in code) a SQL append query.

  5. #5
    faythe1215 is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    52
    Sorry, I guess that's where our wires have been getting crossed. I do not need to store those values permanently. I need access them in my VB code, use them, and then I move on to the next record in the query and start again.

    With the code ssanfu provided, I should be able to proceed...

  6. #6
    faythe1215 is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    52
    Ran the below code just to see if I could get the Recordset part working...

    Code:
    Private Sub btnUnitCheck_Click()
        Dim RecSet As DAO.Recordset
        Set RecSet = CurrentDb.OpenRecordset("qryPurchase_AvgPrice")
        
        Dim NumPurchased As Integer
        
        RecSet.MoveFirst
        NumPurchased = RecSet.CountOfItems_ID                  '    Error 
        MsgBox "NumPurchased = " & NumPurchased
        
    End Sub
    Got Compile Error - Method or Data member not found.

  7. #7
    faythe1215 is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    52
    Never mind. I fixed that:

    Code:
    RecSet!CountOfItems_ID       'instead of RecSet.CountOfItems_ID
    I actually have a new problem.

    I created the fields [CountOfItems_ID] and [AvgOfRetailPrice] with the totals option on the query design, one with a count and the other an average. Any time I try to access it with the method above, it tells me that I have a 3265 error. Is there another way to generate this so that the information can be accessed?

    Click image for larger version. 

Name:	Untitled.png 
Views:	9 
Size:	17.9 KB 
ID:	20281

  8. #8
    faythe1215 is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    52
    Ugh, never mind. Typo.

    No more late night working for me. Sorry everyone... I'll just leave this here for people to learn from my *face palm* mistakes.

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

Similar Threads

  1. Accessing related tables in Calc fields
    By DB88 in forum Access
    Replies: 1
    Last Post: 04-23-2014, 10:10 AM
  2. Replies: 4
    Last Post: 10-14-2012, 04:06 PM
  3. Accessing inputted data
    By kp123 in forum Access
    Replies: 9
    Last Post: 12-07-2011, 10:40 AM
  4. Replies: 6
    Last Post: 09-25-2009, 12:40 PM
  5. Accessing subform fields
    By nkenney in forum Forms
    Replies: 1
    Last Post: 04-21-2009, 10:10 PM

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