Results 1 to 8 of 8
  1. #1
    jgelpi16 is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544

    Question Do While loop

    I have the below do while loop to identify the value in a table's field. Everything seems to be going well except for some reason my variable always returns the value of the last field in the table. It will loop through the correct number of times for the number of records in the table, but it always returns the same value when I step through the code. Any suggestions?

    Code:
        Set db = Access.Application.CurrentDb
        Set rst = db.OpenRecordset("DenialReason")
        With rst
            '.MoveLast
            .MoveFirst
            Do While .EOF = False
                varOfferIDCheck = OfferID
                '!Rec_Group = AssignGroup(!Rec_Type)
                .MoveNext
            Loop
        End With
    I am aware I have ".MoveLast" commented out. I tried it with it not commented out and the same result occured.

  2. #2
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    The data in the variable will be overwritten each time through the loop so you will only end up with the last value encountered in the loop. You have to do something with the variable before moving to the next record in the recordset.

    Can you explain in words what you want to do?

  3. #3
    jgelpi16 is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    What I am looking to do is check to see if a value is in the table in the field/column "OfferID". If the value is there I want to delete it, if it is not there I want to proceed.

  4. #4
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    You want to clear the field OfferID if there is something in it (i.e. change the value to null) ? You can do that with an update query. I assume that you don't want to delete the entire record

    If you want to clear the field only if there is a specific value in the field, then you can do that with an update query also.

    Can you be a little more specific?

  5. #5
    jgelpi16 is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    Yes, sorry. I am storing a number (needs to be unique) and a description in a table. Whenever the user clicks the "save" button I want to check to see if that number (text box on the form) is in the table. If the value is in the table I want to delete the number and the description associated with it. I then want to insert the new value and description based upon the option box selection. The description is obtained from the option box label caption. Here is some pseudo code:

    If text1.value is in the table then
    delete text1.value from table and delete optionboxlabel1.caption from table
    append text1.value and optionboxlabel2.caption
    else
    append text1.value and optionboxlabel2.caption
    end if

  6. #6
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    I want to check to see if that number (text box on the form) is in the table. If the value is in the table I want to delete the number and the description associated with it.
    Why remove the number in the table with the one from the form if they are the same? Just update the description field with the new value from the caption using an update query. Here is the code (not tested)

    Dim mySQL as string

    IF DCount("*","tablename","youruniquenumberfield=" & me.textboxnamewithuniquenumber)>0 THEN
    mySQL="Update your tablename SET fieldholdingthecaptiontext='" & me.optionboxlabel2.caption & "' WHERE youruniquenumberfield=" & me.textboxnamewithuniquenumber
    currentdb.execute mySQL, dbfailonerror
    END IF

  7. #7
    jgelpi16 is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    Because I like to make it harder on myself....That makes perfect sense. Thank you for the help!

  8. #8
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    You're welcome. I'm guilty of making things harder for myself too!

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

Similar Threads

  1. VBA Loop
    By DreamOn in forum Programming
    Replies: 4
    Last Post: 06-25-2010, 03:35 AM
  2. Loop through Records and Make ID
    By rob4465 in forum Programming
    Replies: 3
    Last Post: 01-14-2010, 10:46 AM
  3. For Each LOOP statement
    By zambam737 in forum Programming
    Replies: 3
    Last Post: 10-26-2009, 09:59 PM
  4. concatenate string using loop
    By nengster in forum Programming
    Replies: 0
    Last Post: 02-23-2009, 08:05 PM
  5. Loop a table to use field in query
    By jdubp in forum Programming
    Replies: 0
    Last Post: 03-04-2008, 11:48 AM

Tags for this Thread

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