Results 1 to 8 of 8
  1. #1
    tagteam is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    486

    use vb to create new records based on number range

    I am looking for a way to use vb, or any other way, to allow my users to enter a railcar initial and then a number sequence and add new records to the end of the table.



    For example in a form the user can enter the car initial and the number sequence
    Railcar initial: GATX
    Number sequence : 290001 - 290100

    I would like a way to create a temp table that then has 100 records
    GATX 290001
    GATX 290002
    GATX 290003
    etc etc.

    I can then use this temp table in an append query to add them to my main table.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    This is generating a custom unique identifier - common topic in forum. Search: unique identifier

    Why bother with temp table, write records directly to main table.

    For i = 1 to 100
    CurrentDb.Execute "INSERT INTO maintable(ID) VALUES('" & Me.Initial & Me.Seq & Format(i, "0000") & "')"
    Next
    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
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I would probably use DAO and the .AddNew method

    for i = y to x -1
    rs.addnew
    rs![Field] = "CAIX" & i
    rs.update
    next

    as an example

    edit: I forgot the format thing...

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    I see others have posted, but here is another.
    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : tagteam
    ' Author    : Jack
    ' Date      : 16/04/2014
    ' Purpose   : Routine to create table YourTemTable and to populate it
    'with 100 records like
    'GATX 290001
    'GATX 290002
    'GATX 290003
    '---------------------------------------------------------------------------------------
    '
    Sub tagteam()
              Dim db As DAO.Database
              Dim rs As DAO.Recordset
              Dim iCnt As Integer
              Dim stxt As String
    10        stxt = "GATX"
              Dim sql As String
    20        On Error GoTo 0
    30        sql = "Create TABLE YourTempTable (ID  autoincrement not null Primary Key, RailCarInitial varchar(4), RailCarNumber long);"
    40        Set db = CurrentDb
    45        db.Execute sql, dbFailOnError
    50        On Error GoTo tagteam_Error
    60        Set rs = db.OpenRecordset("YourTempTable")
    70        For i = 290001 To 290100 Step 1
    80            rs.AddNew
    90            rs!RailCarInitial = stxt
    100           rs!RailCarNumber = i
    110           rs.Update
    120       Next i
    130       rs.Close
    140       db.Close
    150       On Error GoTo 0
    160       Exit Sub
    
    tagteam_Error:
    
    170       MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure tagteam of Module UtterAccessRelated"
    End Sub

  5. #5
    tagteam is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    486
    Thanks, can I use a variable that is specified in the form. The number and the car initial will not always be the same and the user will enter them in a form. The car initial will be the same for that use. For example it might be GATX 1-50 and next time it might be UTLX 22456-22480

    Can i do:
    Dim stxt AS string
    stxt = [Forms]![Form]![CarInitial]
    ...

    And then later
    Dim Number as int
    Number = [Forms]![Form]![BeginNumber]
    for i in Number Step 1
    ...

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Still need the start and end for the For loop

    Number is a reserved word. Use something like intNum as variable name.

    For i = intNum To intNum + 200

    1 is default step so don't really need the Step parameter.
    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.

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Plus formatting like June7 mentioned earlier since the car numbers can have leading zeros

  8. #8
    tagteam is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    486
    Thanks, I used the code from orange and changed it to begincarnumber as June7 suggested and it is working like I want.
    THanks

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

Similar Threads

  1. Create records for date range
    By wnicole in forum Access
    Replies: 1
    Last Post: 10-03-2013, 09:02 AM
  2. Display Pass or Fail based on number range
    By aselm01 in forum Forms
    Replies: 10
    Last Post: 09-09-2013, 03:52 PM
  3. Replies: 16
    Last Post: 06-20-2013, 09:25 AM
  4. Replies: 4
    Last Post: 12-14-2010, 07:10 PM
  5. Replies: 3
    Last Post: 08-26-2010, 02:11 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