Results 1 to 2 of 2
  1. #1
    frikkie is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2013
    Posts
    5

    Generating Range of numbers

    I want to generate of numbers using a form with the fields "From Number" and "To Number". Each of the generated numbers must then be stored on its own row. I have very little knowledge of vba code

  2. #2
    apr pillai's Avatar
    apr pillai is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2010
    Location
    Alappuzha, India
    Posts
    209
    Sample VBA Code is given below:

    Code:
    Private Sub Command8_Click()
    Dim rst As Recordset
    Dim frmNo, toNo, j, bkmk As String
    
    
    'Read From, To numbers from Text Boxes
    frmNo = Nz(Me![From Number], 0)
    toNo = Nz(Me![To Number], 0)
    
    
    'if any of the text box is empty or 0 value
    'then display a message and terminate the program
    If frmNo = 0 Or toNo = 0 Then
        MsgBox "From/To Number is Zero"
        Exit Sub
    End If
    
    
    'open the recordset assigned to the Form RecordSource
    Set rst = Me.RecordsetClone
    'position on the first record
    rst.MoveFirst
    
    
    'set a loop to run from start number to end number
    'incrementing by one
    For j = frmNo To toNo Step 1
    'update the generated number on the record
      rst.Edit
      rst![numberField] = j
      rst.Update
      'read the bookmark of the current record
      bkmk = rst.Bookmark
      'set this bookmark to the Form Bookmark
      'This action will synchronize the recordsetclone record
      'with the visible record on the form
      Me.Bookmark = bkmk
      'take the next record in the recordsetclone to
      'record the next generated sequence number
      rst.MoveNext
      'If there are less number of records on the recordset
      'than the range of number given to generate then
      'then exit the loop and stop generating next number
      If rst.EOF Then
         Exit For
      End If
    Next
    
    
    'close the recordsetclone
    rst.Close
    
    
    Set rst = Nothing
    
    
    End Sub

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

Similar Threads

  1. Generating sequential numbers automatically
    By Malcolm41 in forum Access
    Replies: 2
    Last Post: 07-15-2012, 08:20 PM
  2. Access generating random numbers
    By dama in forum Forms
    Replies: 1
    Last Post: 02-19-2012, 06:36 PM
  3. Replies: 1
    Last Post: 04-28-2011, 03:32 PM
  4. generating automatic numbers in a database
    By bonbon68 in forum Access
    Replies: 3
    Last Post: 03-16-2011, 10:09 AM
  5. generating random numbers on form
    By anitra in forum Forms
    Replies: 1
    Last Post: 02-14-2006, 10:08 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