Results 1 to 3 of 3
  1. #1
    shod90 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2016
    Posts
    134

    Increment numbers with starting zeros


    dear gents,
    um trying to create a loop that insert into a table some data ( Code of Monitors )
    the code of monitors is (MR-001 , MR-002, MR-003, ...., MR010,MR011, .. Etc ) i have 100 monitors i need to make a loop that insert them all one time
    something like:
    Start Number :
    Charachters before the numbers :
    number of items :

    because also i have PC's and the code is (PC-001,PC-002 ,etc..) that's why i need it flexiable to add whatever characters before then the numbering

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    make a query to pull the monitors,
    then cycle thru naming them:

    Code:
    sSql = "Select * from table where [ItemType]='MONITOR'
    Code:
    Set db = CurrentDb
    Set rst = db.OpenRecordset(sSql)
    i = 1
    With rst
      While Not .EOF
         vName = "MR-" & format(i,"000")
         
         .Edit
           .Fields("CodeName").value = vName
         .Update
         
         .MoveNext
        i = i + 1
      Wend
    End With
    


  3. #3
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Another Subroutine
    Paste the following into a standard module:

    Code:
    Sub AddCodes()
    
        Dim i As Integer
        Dim EquipPrefix As String
        Dim sSQL As String
        Dim YourTableName As String
        Dim YourFieldName As String
    
        '=========================
        'Change these to ythe actual table name
        EquipPrefix = "MR"             '<<<--- Change this to the code you want  (MR, PC, ...)
        YourTableName = "tblAssign"    '<<<--- Change this to the actual TABLE name
        YourFieldName = "DriverName"   '<<<--- Change this to the actual FIELD name
        '=========================
        
        For i = 1 To 100
            sSQL = "INSERT INTO " & YourTableName & " (" & YourFieldName & ")"
            sSQL = sSQL & " VALUES ('" & EquipPrefix & Format(i, "000") & "')"
            'Debug.Print sSQL
            CurrentDb.Execute sSQL, dbFailOnError
        Next
        
        MsgBox "Done"
    End Sub

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

Similar Threads

  1. Increment numbers
    By grkchakri in forum Access
    Replies: 4
    Last Post: 07-22-2018, 04:07 AM
  2. Replies: 5
    Last Post: 12-15-2015, 05:01 AM
  3. Replies: 13
    Last Post: 05-01-2013, 09:01 AM
  4. Replies: 1
    Last Post: 02-23-2012, 11:48 AM
  5. Renumbering Auto-Increment numbers
    By svcghost in forum Database Design
    Replies: 8
    Last Post: 02-19-2011, 08:48 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