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

    Inserting multiple same records with different date

    Dear gents,
    Um trying to edit the current form for users which is
    Project ID
    Work items
    Description
    Date
    Hours

    Now if they are having same task to do for 3 days , they will enter the data 3 times exactly , I got an idea to make two field for dates , date from and date to , and what between them will be save automatically with the same data in other fields , can anyone help me to do that task ?


    Thanks

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    the form will have textboxes for all items including, txtStartDate, and txtEndDate.
    click the button to run the query for every date

    Code:
    sub btnInsertDates_click()
    dim vDate
    dim sSql as string
    
    docmd.setwarnings false
    vDate = txtStartDate
    while vDate <= txtEndDate
       sSql = "insert into table ([Project ID], [Work items],Description,Date,Hours) values (" & txtID & ",'" & txtWork & "',#"  & vDate & "#," & txtHrs & ")"
       docmd.runSql sSql
       vDate = DateAdd("d",1,vDate)
    wend
    docmd.setwarnings true
    end sub

  3. #3
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    My solution is very similar to ranman's. But since I have been working on it for a while, I will post it also.
    The key is to create an unbound form, with the following text boxes:
    - txtProjectID (I assumed text)
    - txtWorkItems (assumed text)
    - txtDescription (assumed text)
    - txtStartDate (date)
    - txtEndDate (date)
    - txtHours (assumed number)

    Then I added a command button named "cmdAddRecords".

    You would also need to replace "TableName" with whatever the name of your table is.
    So, here is the code behind the command button that would add all those records.
    Code:
    Private Sub cmdAddRecords_Click()
    
        Dim mySQL As String
        Dim myDate As Date
    
    '   Check to make sure that end date is not before start date
        If Me.txtEndDate < Me.txtStartDate Then
            MsgBox "Please try again!", vbOKOnly, "Invalid Date Entries!"
        Else
    '       Loop through dates
            myDate = Me.txtStartDate
            Do Until myDate > Me.txtEndDate
    '           Build insert SQL code
                mySQL = "INSERT INTO TableName ([Project ID], [Work Items], Description, [Date], Hours) "
                mySQL = mySQL & "VALUES('" & Me.txtProjectID & "','" & Me.txtWorkItems & "','" & Me.txtDescription & "',#" & myDate & "#," & Me.txtHours & ")"
    '           Run query
                'MsgBox mySQL
                DoCmd.SetWarnings False
                DoCmd.RunSQL mySQL
                DoCmd.SetWarnings True
    '           Add one to date
                myDate = myDate + 1
            Loop
            MsgBox "Done!"
        End If
       
    End Sub
    You can add whatever checks and validations you might need. I just added one, to make sure that txtEndDate is not before txtStartDate.

  4. #4
    shod90 is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Jan 2016
    Posts
    134
    thanks for the fast responses , But i think it need little change since i have an autonumber ID in my table .
    I got that error when i turn setwarnings to true ..
    Click image for larger version. 

Name:	Capture.JPG 
Views:	11 
Size:	39.7 KB 
ID:	27621

    Any idea ?

  5. #5
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    If you have an Autonumber field, you will leave it out of the Append Query SQL code (as Access will assign it automatically).

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

Similar Threads

  1. VBA Inserting two new records
    By lzook88 in forum Programming
    Replies: 2
    Last Post: 10-11-2015, 10:52 AM
  2. Replies: 3
    Last Post: 03-11-2014, 07:32 PM
  3. Inserting records in multiple tables
    By Nikos in forum Database Design
    Replies: 8
    Last Post: 02-17-2012, 02:35 PM
  4. Inserting multiple records into a table
    By New2Access in forum Programming
    Replies: 1
    Last Post: 07-07-2011, 09:18 PM
  5. Inserting Multiple values
    By rajath in forum Access
    Replies: 1
    Last Post: 05-14-2010, 04:17 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