Results 1 to 9 of 9
  1. #1
    shubhamgandhi is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Jul 2011
    Posts
    22

    Access version syntax help, looping

    Hi I'm writing a sort of complex query in Access 2003 with vba.


    I have only started learning Access a few days ago, but I have some experience in C++, Java, etc.
    My goal is to cross-reference each SKU within each order to see the most common ones sold together.
    This is a test code as I'm figuring out how the language is structured.
    Here is my code:

    Code:
    Sub getSKUS()
    
        Dim db As Database
        Dim rs_data As Recordset
        Dim rs_sku As Recordset
        Dim rs_order As Recordset
        Dim fld As Field
        Dim strSQL As String
        
        'Open connection to current Access database
        Set db = CurrentDb()
        
        Set rs_data = db.OpenRecordset("ORDER_DATA")
        Set rs_sku = db.OpenRecordset("SKUS")
        Set rs_order = db.OpenRecordset("ORDERS")
        
        Dim curSKU As String
        curSKU = "161-0363"
        
        rs_sku.MoveFirst
        Do While Not rs_sku.EOF
            If rs_sku.Fields("SKUS_ORDERED").Value = curSKU Then
            MsgBox "SKU found."
    
        rs_sku.MoveNext
        Loop
    
    End Sub
    All the record_sets are tables containing order info in my database.
    When I try to run it I get the error "Loop without Do".

    Can you help me out?

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Hi

    You may have other errors but this one jumps out at me.
    You have a If/Then without an End If. See added line in red.

    Code:
     
    If rs_sku.Fields("SKUS_ORDERED").Value = curSKU Then
            MsgBox "SKU found."
    End If
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    shubhamgandhi is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Jul 2011
    Posts
    22
    Oh I see! No errors now when I press run!
    This is probably the wrong place to ask this but since I have you:
    How do I actually test this out on my tables?

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Hi

    How do I actually test this out on my tables?
    I'm sorry but I don't understand what you want to test?
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    shubhamgandhi is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Jul 2011
    Posts
    22
    In my code I have:

    MsgBox("SKU found")

    This doesn't pop up when I click the run macro (the play buton) button in the toolbar. I want to test out this code so that the box comes up.
    It should, assuming the code is correct.
    Heres what I changed it to:

    Code:
     
    Function getSKUS()
        Dim db As Database
        Dim rs_data As Recordset
        Dim rs_sku As Recordset
        Dim rs_order As Recordset
        Dim fld As Field
        Dim strSQL As String
        
        'Open connection to current Access database
        Set db = CurrentDb()
        
        Set rs_data = db.OpenRecordset("ORDER_DATA")
        Set rs_sku = db.OpenRecordset("SKUS")
        Set rs_order = db.OpenRecordset("ORDERS")
        
        Dim curSKU As String
        curSKU = "161-0363"
        
        rs_sku.MoveFirst
        Do While Not rs_sku.EOF
            For Each fld In rs_sku.Fields
                If fld.Name = "SKU_ORDERED" Then
                    If fld.Value = curSKU Then
                    MsgBox ("SKU found")
                    
                    End If
                End If
            Next
        rs_sku.MoveNext
        Loop
    End Function

  6. #6
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Hi

    Have you tried placing a breakpoint at the start of the code, run the code and step thru it one line at a time using F8, to see where it goes wrong.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  7. #7
    shubhamgandhi is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Jul 2011
    Posts
    22
    The code is now working fine without errors, but I want to actually call the funciton on my SKU table.
    I'm trying to use a macro to call this module but I cant figure out how it works...

  8. #8
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    I'm sorry, but have to get to bed now (11pm local time) as I have to be up at 4.30am tomorrow. I will try to help you further when I'm available next, if nobody else offers you some help, but that won't be until the day after tomorrow.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  9. #9
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Hi

    Sorry I haven't been available sooner.

    I haven't used macros for years (I prefer VBA code) but I will try to help if I can.
    but I want to actually call the funciton on my SKU table
    I'm sorry, but I don't understand what this means.
    In Acces, code/macros are called/invoked when an "event" occurs, usually conected to a form or report.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

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

Similar Threads

  1. Looping in Access
    By ducthang88 in forum Programming
    Replies: 2
    Last Post: 12-04-2010, 07:43 PM
  2. What version of VB does Access use
    By zapper222 in forum Programming
    Replies: 1
    Last Post: 08-03-2010, 03:54 PM
  3. Looping syntax
    By fadiaccess in forum Access
    Replies: 1
    Last Post: 10-23-2009, 02:57 AM
  4. ms access version combinations
    By marianne in forum Access
    Replies: 1
    Last Post: 08-05-2009, 07:37 AM

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