Results 1 to 6 of 6
  1. #1
    rajgoyal00 is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    26

    Insert Query, RANDOM number replition problem. Using loop. Not WORKING please help..

    Hi,



    I am trying to insert a random code into [Car_Parts].[Product Code]
    But I want to make sure I don't do a duplicate value.
    Its not working...

    Please tell me what am I doing wrong.. Here's the code.


    Dim Product_Code
    Dim Prod_Code_Check

    Product_Code = "STS" & Round(Rnd * 999999, 0)



    Do Until [Product_Code_Check] <> Product_Code

    Product_Code_Check = [Car_Parts].[Product Code]

    Loop





    PLEASE HELP!!!! Thanks

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Not sure what that loop will do, since you never set Prod_Code_Check. In any case, I'd probably do something like:


    1. Set Variable to random code
    2. Use DCount() to see if that code already exists
    3. If it does, use GoTo to go back to step 1
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    And to add to Paul's response, remember that Rnd() requires Randomize to change the seed or you get the same number all of the time.

  4. #4
    rajgoyal00 is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    26
    Thank You Paul, I was able to find the solution, using your advice.
    Thanks you also for the advice Rural Guy. Am not a pro at this, just learning as I go. Really appreciate all of you good folks on these forums..

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    We were happy to help. I forgot about the need for Randomize. It might help a future searcher if you post the working code.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    rajgoyal00 is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    26
    Thanks again for the help!!!
    I dont know how to use randomize, This is the code Used and its working

    Step1:
    Dim Product_Code
    Dim Prod_Code_Check

    Product_Code = "STS" & Round(Rnd * 999999, 0)
    Prod_Code_Check = DCount("[Product Code]", "[Car_Parts]", "[Car_Parts].[Product Code] = '" & Product_Code & "'")
    If Prod_Code_Check > 0 Then
    GoTo Step1
    End If

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

Similar Threads

  1. Insert a Sequence Number in Query
    By am465 in forum Access
    Replies: 4
    Last Post: 11-12-2013, 02:52 PM
  2. Random Number and Comparison
    By Azgambit in forum Programming
    Replies: 1
    Last Post: 10-22-2013, 04:55 PM
  3. No-So-Random Number
    By oleBucky in forum Programming
    Replies: 8
    Last Post: 11-08-2011, 11:02 AM
  4. Replies: 1
    Last Post: 10-12-2011, 08:07 AM
  5. Query Random Number Column Manipulation
    By gmontano in forum Queries
    Replies: 4
    Last Post: 10-05-2010, 01:36 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